Reputation: 13
How can I insert data into a column of a table (out of 2 tables) such that user id matches in both of them? table 1 is users
and table 2 is comments
Here is the query I am using:
$query='INSERT INTO comments VALUES('','','$comment','','') WHERE users.id=comments.id';
but it says an error:
Parse error: syntax error, unexpected '','' (T_CONSTANT_ENCAPSED_STRING) in F:\xampp\htdocs\my\lecture1.php on line 263
where line 263 is the query
Upvotes: 1
Views: 43
Reputation: 27874
INSERT
can only include new records. To modify the value of a field in an existing record, use UPDATE
.
For instance:
UPDATE comments SET comment = 'Blah, blah blah.' WHERE id = 1;
You can cross tables while using UPDATE
, however I don't see any reason to do that in this case.
You can also use INCLUDE
with ON DUPLICATE KEY
trigger to create the record if it doesn't exist or update an existing one with the same query. The ON DUPLICATE KEY
trigger is triggered when the insertion of the new record is impossible because the UNIQUE
or PRIMARY KEY
specified is already present in the table. Example:
INSERT INTO comments (id, comment) VALUES (1, 'Comment') ON DUPLICATE KEY UPDATE comment = 'Comment';
Upvotes: 1