Page 1 of 1
INSERT statement failing
Posted: Mon Sep 13, 2010 11:36 pm
by vfm
Hi,
I have the following PHP code:
Code: Select all
$que="INSERT INTO products ('id', 'name', 'price', 'description', 'colour', 'details', 'designer', 'photo1', 'photo2', 'photo3', 'photo4', 'units_s', 'units_m', 'units_l', 'units_xl') VALUES ($id, $name, $price, $description, $colour, $details, $designer, $photo1, $photo2, $photo3, $photo4, $units_s, $units_m, $units_l, $units_xl)";
which is not giving me an error but doesn't seem to actually insert anything into the database. Is there something I'm missing here or a way I can debug what's going on?
Cheers,
Kieran.
Re: INSERT statement failing
Posted: Mon Sep 13, 2010 11:41 pm
by iijb
Hi,
Try this
Code: Select all
$que="INSERT INTO products ('id', 'name', 'price', 'description', 'colour', 'details', 'designer', 'photo1', 'photo2', 'photo3', 'photo4', 'units_s', 'units_m', 'units_l', 'units_xl') VALUES ('$id', '$name', '$price','$description', '$colour', '$details', '$designer', '$photo1', '$photo2', '$photo3', '$photo4', '$units_s','$units_m', '$units_l', '$units_xl')";
Regards
iijb
Re: INSERT statement failing
Posted: Tue Sep 14, 2010 1:22 am
by vfm
Hi,
Still no luck. I copied your line into my code and nothing happened. Same problem!
Cheers,
Kieran.
Re: INSERT statement failing
Posted: Tue Sep 14, 2010 5:13 am
by jazzercising011
if that's the case then we need to see more of the code like what you are doing with this statement because your original post of the statement appears to be correct.
Re: INSERT statement failing
Posted: Thu Sep 16, 2010 3:11 am
by vfm
Hi Prensil,
I've attached the db structure as an image and you can find below my php code.
Code: Select all
$que="INSERT INTO products ('id', 'name', 'price', 'description', 'colour', 'details', 'designer', 'photo1', 'photo2', 'photo3', 'photo4', 'units_s', 'units_m', 'units_l', 'units_xl') VALUES ('$id', '$name', '$price','$description', '$colour', '$details', '$designer', '$photo1', '$photo2', '$photo3', '$photo4', '$units_s','$units_m', '$units_l', '$units_xl')";
THanks in advance!
Re: INSERT statement failing
Posted: Thu Sep 16, 2010 10:43 am
by stuartr
As prensil says, you should try echo'ing the query that SQL will be using when it is called.
Can you also attached the part of the code that is actually calling the SQL query - I would expect to see something like:
Code: Select all
$que="INSERT INTO products ('id', 'name', 'price', 'description', 'colour', 'details', 'designer', 'photo1', 'photo2', 'photo3', 'photo4', 'units_s', 'units_m', 'units_l', 'units_xl') VALUES ($id, $name, $price, $description, $colour, $details, $designer, $photo1, $photo2, $photo3, $photo4, $units_s, $units_m, $units_l, $units_xl)";
mysql_select_db($database_conn1, $conn1);
$Result1 = mysql_query($que, $conn1) or die(mysql_error());
Re: INSERT statement failing
Posted: Thu Sep 16, 2010 4:05 pm
by John Cartwright
I couple things that immediately jump out, you are not supposed to wrap db/table/column/alias names with single quotes, and instead you should be using backticks.
Secondly, unless your values are all numeric, you will need to wrap your values in single quotes (properly escaped I might add).
Code: Select all
$que= "
INSERT INTO products
(`id`, `name`, `price`, `description`, ... etc ... )
VALUES
(
'". (int)$id ."',
'". mysql_real_escape_string($name) ."',
'". mysql_real_escape_string($price) ."',
'". mysql_real_escape_string($description) ."',
... etc ...
)
";
Re: INSERT statement failing
Posted: Thu Sep 16, 2010 6:18 pm
by vfm
Hi,
I echoed the query and it looks like this:
INSERT INTO products ('id', 'name', 'price', 'description', 'colour', 'details', 'designer', 'photo1', 'photo2', 'photo3', 'photo4', 'units_s', 'units_m', 'units_l', 'units_xl') VALUES ('5', 'asdlk', '109', 'a neat black shirt', 'black', 'cool as bro', 'kd', '', '', '', '', '2', '3', '3', '0')
John, I'm not sure what you mean in your example. I'm very new to this so didnt quite catch your drift. In the example above, where would i use backticks and how where would i wrap it in single quotes with escapes?
Thanks!
Re: INSERT statement failing
Posted: Thu Sep 16, 2010 6:29 pm
by John Cartwright
Simply put:
Database names, table names, column names, and aliases, if optionally quoted, need to be quoted using backticks (the following character) `
Values (integers are the exception) must be quoted using single quotes. (the following character) '
Refer to my previous example on how to apply it to your query.
Re: INSERT statement failing
Posted: Thu Sep 16, 2010 6:56 pm
by vfm
Hi John,
That seems to have done the trick! Thanks for your assistance
