user3417953
user3417953

Reputation: 123

php getting insert errors

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

Upvotes: 0

Views: 74

Answers (2)

domdomcodecode
domdomcodecode

Reputation: 2453

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]";

Upvotes: 0

gbestard
gbestard

Reputation: 1177

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

Upvotes: 1

Related Questions