I have a table named user_data which contains 5 rows-id(primary key),name,address,phone,sex.When I try to insert values into the table via this query
mysqli_query($con,"INSERT INTO user_data VALUES ('Peter_malik', 'Griffin door',35897,'male')");
it doesnt work.But When I tried this one,it works.
mysqli_query($con,"INSERT INTO user_data (name,address,phone,sex) VALUES ('Peter_Gregory', 'Griffin door',35897,'male')");
I didnt understand what is the real issue behind this.I am using PHP 5.4.7 and XAMPP 1.8.1.
instead of this:
mysqli_query($con,"INSERT INTO user_data VALUES ('Peter_malik', 'Griffin door',35897,'male')");
Use this when the field is set as NOT NULL
mysqli_query($con,"INSERT INTO user_data VALUES (NULL, 'Peter_malik', 'Griffin door',35897,'male')");
Or use this when the field is set as NULL
mysqli_query($con,"INSERT INTO user_data VALUES (0, 'Peter_malik', 'Griffin door',35897,'male')")
See the mysql manual: http://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html
It does not insert in the first query since it assumes the first value i.e. 'Peter_malik' is for your primary key and it fails.
When you specify the column names in the query it knows which value is for which column.
I addition in the first case i.e.
'Peter_malik', 'Griffin door',35897,'male'
will refer to
id(pk),name,address,phone
So u need to pass the first value as NULL so that id gets auto incremented. So the first query should be as
VALUES (NULL, 'Peter_malik', 'Griffin door',35897,'male')
If your table has 5 columns id, name, address, phone and sex your staement has to provide 5 values, one for each column. Since your stament provides only 4 values, you need a column list to tell MySQL which values you do provide.
If your id column is a auto_increment column, you can provide null in your values clause.
So you have to say:
INSERT INTO user_data VALUES (NULL, 'Peter_malik', 'Griffin door',35897,'male');
OR
INSERT INTO user_data (name,address,phone,sex) VALUES
('Peter_malik', 'Griffin door',35897,'male')
You need to include the id column on your insert statement:
mysqli_query($con,"INSERT INTO user_data VALUES (null, 'Peter_malik', 'Griffin door',35897,'male')");
, provided that your id field is set to autoincrement! ;)
If you provide the ID also in your first query, it will work without any problem.
Assuming that id=1
mysqli_query($con,"INSERT INTO user_data VALUES (1,'Peter_malik', 'Griffin door',35897,'male')");
As others said when you have not explicitly mentioned your column names in a query, you have to provide all the values.
when you are trying the following query then...
mysqli_query($con,"INSERT INTO user_data VALUES ('Peter_malik', 'Griffin door',35897,'male')");
In this query you only inserting 4 values in the table but the table has 5 field so it is causing problem because in the above query the value is inserting form first field & the sequence or datatype mismatching in the database because of this this query causing problem .
If u want insert wuthout specifing field then you may use the following query....
mysqli_query($con,"INSERT INTO user_data VALUES (0,'Peter_malik', 'Griffin door',35897,'male')");<br><br>
It Will work properly.
and in the your second query you also specifying the field name & corresponding their values so that's query not causing any problem.
Related
I am using simple MySQL code to insert data from a form with auto_increment id, it works fine on local host but not on server.
this is the code I am using
$myvar=$_POST['var'];
//Insert data into mysql table
$sql="INSERT INTO mytable(id,myvar) VALUES ('','$myvar')";
in local host phpmyadmin data is inserted in table but on server phpmyadmin data is not inserted in table. In my localhost I am using XAMPP whereas phpmyadmin on IIS server.
What setting should I do in phpmyadmin on server so that id is automatically inserted with auto increment, I know I can use mysql_insert_id() to insert id but in lots of file i have used this type of code which I cannot change. I want the same setting as XAMPP that can take this type of code easily.
You as inserting '' into auto-increment id. just insert other fields and MySQL handles the auto-increment column.
$sql="INSERT INTO mytable(id,myvar) VALUES ('','$myvar')";
should be
$sql="INSERT INTO mytable(myvar) VALUES ('$myvar')";
if still you are getting the error
first check, in the server your id column must marked as auto_increment.data type should be int.
if it is ok then
insert into mytable(myvar) values('test');
and try to use prepared statements to avoid the sql injection. Thanks.
hope this may usefull for you.
If you want the last of all the rows in the table, then this is finally the time where MAX(id) is the right answer! :) kind of.
SELECT fields FROM mytable BY id DESC LIMIT 1;
will get the last id then increment it to 1 // add this value to id in insert statement
or
if you have a autoincrement column change it to
$sql="INSERT INTO mytable(id,myvar) VALUES ('','$myvar')";
to
$sql="INSERT INTO mytable(myvar) VALUES ('$myvar')";
auto increment inserts id itself so you don't need to pass it through code
$sql="INSERT INTO mytable(myvar) VALUES ('$myvar')";
or you can do this
$sql="INSERT INTO mytable(id,myvar) VALUES (NULL,'$myvar')";
if you want want to manually insert id for some reason
$sql="INSERT INTO mytable(id,myvar) VALUES (10,'$myvar')";
First check your table Structure Property by applying query:
desc mytable (or) show create table mytable.
Check auto increment property been set for column id.
If yes means don't need to pass id in insert query.
Just write a query as :-
$sql="INSERT INTO mytable(myvar) VALUES ('$myvar')";
So, I was wondering if you were to launch this query:
INSERT INTO users (name, gender, location) VALUES ('Ricky-Bobby', 'm', 'Daytona Beach, FL');
And suppose that users has another auto-incrementing columns for the primary key, how would you be able to get the value of that primary key without launching another query?
Codeigniter simplifies the MySQL query SELECT LAST_INSERT_ID() through the function $this->db->insert_id().
Although this is a query, it's a very light one that won't be a performance issue. One thing to have in note is this:
If you want the ID on the row you inserted; $this->db->insert_id()
If you want the auto_increment value, that is the next value that an INSERT will have as id, simply $this->db->insert_id() + 1
codeigniter makes this available with $this->db->last_insert_id()
http://ellislab.com/codeigniter/user-guide/database/helpers.html
You can combine LAST_INSERT_ID() with your query:
INSERT INTO users (name, gender, location) VALUES ('Ricky-Bobby', 'm', 'Daytona Beach, FL');
SELECT LAST_INSERT_ID();
You could use this if you are using PDO
http://php.net/manual/en/pdo.lastinsertid.php
I am doing an ajax to write records into the db. The table has id,fullname. The id is set to primary and auto increment. Now without the id (8 in the below statement) the record is not written.
Below is the insert statement
$sql =
"INSERT INTO user
VALUES ('8','".$_POST['name']."','test4','test5','test6')";
Is it possible to write without the id?
Place NULL instead of '8', that will tell MYSQL to do default auto increment:
$sql="insert into user values (NULL,'".$_POST['name']."','test4','test5','test6')";
Other possibility is to rewrite your query to this from:
$sql="insert into user (field1, field2, field3, field4) values ('".$_POST['name']."','test4','test5','test6')";
In this case you didnt specify id as a column to be inserted, so MySQL will again do the default auto increment
You can also omit the ID column in the query so that It automatically increments the ID Value
Yes it's totally possible but you need to specify which columns you are inserting into... for example:
$sql="insert into user (username, column2, column3, column4) values ('".$_POST['name']."','test4','test5','test6')";
Try
$sql="insert into user values (NULL,'".$_POST['name']."','test4','test5','test6')";
or
$sql="insert into user(`field1`, `field2`, `field3`, `field4`) values ('".$_POST['name']."','test4','test5','test6')";
You can use DEFAULT or NULL instead of '8'
insert into user(DEFAULT, column1, column2, column3) VALUES (....
I'm creating a registration and I'm using just straight PHP not JavaScript to send my form to the MySQL database, so everything is working fine, no syntax error or anything but I fill out all my information and click 'Register' and it returns a message saying 'Column count doesn't match value count at row 1'.
I'm only 14 so this is pretty confusing for me does anyone have a solution?
This is my INSERT INTO code:
$sql = mysql_query("INSERT INTO users(firstname, lastname, email, password, day, month, year, gender)
VALUES('$firstname','$lastname','$email','$db_password','$day','$month','$year')")
or die (mysql_error());
You are trying to insert 7 values into 8 columns - you are missing the insertion of gender.
The correct code would be:
$sql = mysql_query("INSERT INTO users(firstname, lastname, email, password, day, month, year, gender)
VALUES('$firstname','$lastname','$email','$db_password','$day','$month','$year', '$gender')")
or die (mysql_error());
By the way, if you are not already doing it, I highly recommend escaping the strings first, before passing them to the query like so:
$firstname=mysql_real_escape_string($firstname)
You should do this with all variables above. Here you can find more about the escape function.
With your code there, I see you forget to insert $gender.
When inserting data into a MySQL table, you will have to specify which data goes into which column. You do this by specifying the column names before the VALUES part:
INSERT INTO tblA (col1, col2) VALUES ('value1','value2');
If you omit that information, MySQL will expect all columns:
If your table is like this:
CREATE TABLE tblA (
col1 INT,
col2 INT
);
You can insert information like this:
INSERT INTO tblA VALUES ('value1', 'value2');
If you omit column names and do no specify values for all columns, MySQL will give the "Column count doesn't match value count at row" error will occur, as MySQL doesn't know what to put in the missing columns. With the table structure as above,
INSERT INTO tblA VALUES ('value1');
will result in that error.
In non-strict mode, MySQL will insert default values for omitted column names.
You have missed a value for gender column (the last one)
You are completely missing the gender value:
$sql = mysql_query("INSERT INTO users(firstname, lastname, email, password, day, month, year, gender)
VALUES('$firstname','$lastname','$email','$db_password','$day','$month','$year', 'gender goes here')")
or die (mysql_error());
I need to insert a long row with 32 fields into a MySQL table.
I'd like to do something like this:
$sql="insert into tblname values (... 32 fields ...)";
Obviously, it works fine if the fields are in the same order as the MySQL table fields. But, my table has an auto-increment id as it's first field.
What I want is to fill in all table names but the first (id) one.
Suggestions?
Just use NULL as your first value, the autoincrement field will still work as expected:
INSERT INTO tblname VALUES (NULL, ... 32 Fields ... )
Insert NULL into the auto-increment field.
I recommend that unless this is a hack script, you use field names. The rationale is that your code will break if you ever add a field to the table or change their order.
Instead, be explicit with field names, and it will go much better in the future.
Use NULL or 0 to insert an auto-incremented value as shown below:
-- Here
INSERT INTO tblname VALUES (NULL, ... 32 Fields ... )
-- Here
INSERT INTO tblname VALUES (0, ... 32 Fields ... )
We should omit any column values when we try without column name in insert query,
Advise if above information is wrong.
Here's a simple shortcut that I've used:
$fieldlist=$vallist='';
foreach ($_POST as $key => $value) {
$fieldlist.=$key.',';
$vallist.='\''.urlencode($value).'\','; }
$fieldlist=substr($fieldlist, 0, -1);
$vallist=substr($vallist, 0, -1);
$sql='INSERT INTO customer_info ('.$fieldlist.') VALUES ('.$vallist.')';
Please note that this code would be vulnerable to SQL Injection and should be modified to accommodate PDO's, but I felt this simplified script would more directly answer the question with regards to the originally posted code.