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 (....
Related
INSERT INTO service (col1, col2, col3) VALUES ('test', '2001-01-01', 'test');
SET #last_id = LAST_INSERT_ID();
INSERT INTO artikel_service (service_id) VALUES (#last_id);
The above query works great in phpmyadmin but not in PHP (XAMPP), why is that?
You need to get last inserted id with this query separately: SHOW TABLE STATUS LIKE 'service' and then in the result of above query you will get last inserted id (minus(-) 1) value in column 'Auto_increment'.
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.
I want to insert the current date into the table from my php page but i don't know how to do so. I would also like to know if i am constructing the table right wrt the data type being used. Its a normal "date" data type. Thank you!!
You can do this a variety of ways like:
INSERT INTO tablename (datecolname) VALUES (CURRENT_DATE)
INSERT INTO tablename (datecolname) VALUES (CURRENT_DATE())
INSERT INTO tablename (datecolname) VALUES (NOW())
INSERT INTO tablename (datecolname) VALUES (CUR_DATE())
INSERT INTO tablename (datecolname) VALUES (CURRENT_TIMESTAMP())
INSERT INTO tablename (datecolname) VALUES (CURRENT_TIMESTAMP)
See Mysql's Date and Time functions for more
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'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());