0

I'm getting insert into mysql errors, my code is involved with array, so this part of code is not using array but the codes is comes with array for other features to use.

when I tried to insert new records in mysql , the insert code is giving me errors...

the errors said..

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''product_name') VALUES ('Array' where product_id='3246')' at line 1 ....

this for ....

$query="INSERT INTO product ('product_name') VALUES ('".$product_name."' where product_id='$product_id[$i]') ";

and then remove ' ' ... like this...

$query="INSERT INTO product (product_name) VALUES ('".$product_name."' where product_id='$product_id[$i]') ";

the new errors said ..

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where product_id='3246')' at line 1

and then i removed ' ' where it said where product_id = .... like this.

$query="INSERT INTO product (product_name) VALUES ('".$product_name."' where product_id=$product_id[$i]) ";

and now new errors ...

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where product_id=3246)' at line 1

what did i do wrong and how to solve that?

AM

3
  • Why do you concatenate $product_name but not $product_id[$i]? Commented Apr 8, 2014 at 14:13
  • 1
    where condition is NOT allowed when inserting. Commented Apr 8, 2014 at 14:17
  • Why would you have a WHERE condition in your INSERT statement? Commented Apr 8, 2014 at 14:17

2 Answers 2

1

Like you said your sql syntax is wrong.

You should use

$query="INSERT INTO product (product_name,product_id) VALUES ('$product_name', '$product_id[$i]') ";

to insert a new record

or

$query = "UPDATE product SET product_name = '$product_name' WHERE product_id='$product_id[$id]'";

to update one already existing

Sign up to request clarification or add additional context in comments.

3 Comments

there's a ' more before $product_id[$id]
I have tried both code from user name gbestard is not working due to the first insert code said "Duplicate entry '3246' for key 'PRIMARY'" the second one is giving me same old errors. so is not working any other idea! Also .. for user name Token, my case i relate to copy the records and insert as new records. so this must be using Insert instead of UPDATE.
If you just want the modufy an existing record the first query will fail, but the second should work, what error do you get?
0

You can't be having a WHERE condition in your INSERT statement. Maybe you're trying to UPDATE existing records instead?

$query="UPDATE product SET product_name = '".$product_name."'
WHERE product_id=$product_id[$i]";

5 Comments

So i changed insert code like this ... $query="INSERT INTO product (product_name, product_category) VALUES ('".$product_name.",".$product_category."') "; but the errors said Column count doesn't match value count at row 1 ..... does this error mean the column name are not match is that correct!
name and category are strings, so you need to add some single quotes to wrap around them. You're using variables to hold the names, but when you're constructing the SQL queries, they also need text wrapped in quotes.
you mean like this ... $query="INSERT INTO product (product_name) VALUES ('.$product_name[i].') "; is where i put '.$product_name[i].' is this correct!
Yeah, I think you get the picture :)
You need to be more specific, plus you've already accepted an answer. What's the issue now?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.