I am using this code to enter in to a MySQL database:
function add_content($p) {
$name = mysql_real_escape_string($p['name']);
$price = mysql_real_escape_string($p['price']);
$sql = "INSERT INTO products VALUES (null, '$name', '$price')";
$res = mysql_query($sql) or die(mysql_error());
But I get this error when the page processes the data. And nothing appears in the MySQL database.
It says in the browser:
Column count doesn’t match value count at row 1
What does this mean?
Following from my first post I have just added “(id, name, price)” so the code now reads:
$sql = “INSERT INTO products (id, name, price) VALUES (null, ‘$name’, ‘$price’)”;
$res = mysql_query($sql) or die(mysql_error());
Why did the author who provided the code not include this reference to the corresponding fields in the database. Are these essential?
It is now working but I would like to get the code done professionally.
Matt.
SpikeZ
3
If the id column is auto-increment then you dont need to include it in your field list.
The way you have it now is pretty much how I would do it:
"INSERT INTO products ( name, price) VALUES ( '$name', '$price')";