INSERT statement failing

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
vfm
Forum Commoner
Posts: 32
Joined: Tue Mar 30, 2010 10:47 pm

INSERT statement failing

Post 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.
User avatar
iijb
Forum Commoner
Posts: 43
Joined: Wed Nov 26, 2008 11:34 pm

Re: INSERT statement failing

Post 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
vfm
Forum Commoner
Posts: 32
Joined: Tue Mar 30, 2010 10:47 pm

Re: INSERT statement failing

Post by vfm »

Hi,

Still no luck. I copied your line into my code and nothing happened. Same problem!

Cheers,
Kieran.
jazzercising011
Forum Newbie
Posts: 9
Joined: Tue Sep 14, 2010 4:47 am

Re: INSERT statement failing

Post 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.
vfm
Forum Commoner
Posts: 32
Joined: Tue Mar 30, 2010 10:47 pm

Re: INSERT statement failing

Post 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!
Attachments
db.GIF
db.GIF (16.38 KiB) Viewed 314 times
stuartr
Forum Newbie
Posts: 13
Joined: Sun Sep 12, 2010 11:11 am

Re: INSERT statement failing

Post 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());
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: INSERT statement failing

Post 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 ...
      )
";
vfm
Forum Commoner
Posts: 32
Joined: Tue Mar 30, 2010 10:47 pm

Re: INSERT statement failing

Post 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!
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: INSERT statement failing

Post 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.
vfm
Forum Commoner
Posts: 32
Joined: Tue Mar 30, 2010 10:47 pm

Re: INSERT statement failing

Post by vfm »

Hi John,

That seems to have done the trick! Thanks for your assistance :)
Post Reply