0

I have read some other posts like mine, but none of them solve my problem, I have two tables, user and purchase, When a user signup, I want to add the same user_id to the purchase table as well. I try to right a php code for that like this:

$query = "CREATE TRIGGER `purchase_insert` AFTER INSERT ON `user` FOR EACH ROW BEGIN INSERT INTO purchase (user_id) VALUES (NEW.user_id)";

$result = mysqli_query($connection, $query);
if($result){echo "<br>TRIGGER Success!";} else {die("<br>Database query failed. " . mysqli_error($connection));}

this is the error I get:

Database query failed. 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 '' at line 1

I'm sorry if the question is repetitious.

cool
  • 710
  • 1
  • 6
  • 18

1 Answers1

0

It should be back tick not single quotes

CREATE TRIGGER `purchase_insert` AFTER INSERT ON `user` FOR EACH ROW BEGIN INSERT INTO purchase (user_id) VALUES (NEW.user_id)

UPDATE

Try this

DELIMITER$$ 
CREATE TRIGGER `purchase_insert` 
    AFTER INSERT ON `user`
    FOR EACH ROW 
BEGIN
    INSERT INTO purchase (user_id) VALUES (NEW.user_id);
END$$
DELIMITER ;
Tamil
  • 1,193
  • 9
  • 24
  • `END` not needed? – Riad Sep 07 '16 at 11:44
  • I copied the exact same code , now the error changes to `Database query failed. 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 '' at line 1` – cool Sep 07 '16 at 11:50
  • @coolester man : See my updated answer! – Tamil Sep 07 '16 at 12:34
  • @Tamil you mean I put All of that in the query variable and run it? It gave an error, again. but thank you. – cool Sep 07 '16 at 13:13