table

I need

UPDATE Marks SET mark='$mark' WHERE studentid='$students[id]' AND lessonid='$lessonid' 

and if there are no entries in the Marks table, then add

Reported as a duplicate by participants of Visman , zRrr , aleksandr barakin , user194374, Nick Volynkin Jun 10 '16 at 19:03 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • ON DUPLICATE KEY there is no key here or I don’t understand how to apply - Ivan Mikoryak
  • @Visman Not really. Read the first comment of the author. - E_p
  • @E_p, let's compare our two identical answers to these different questions? ;) - Visman

2 answers 2

If correctly understood:

Add key to studentid and lessonid

ALTER TABLE table_name ADD UNIQUE unique_index ( studentid , lessonid );

and then ON DUPLICATE KEY

Another option is to first select

  • thanks what you need - Ivan Mikoryak

First you need to add an index:

 ALTER TABLE `Marks` ADD UNIQUE INDEX (`studentid`, `lessonid`); 

Then you can use ON DUPLICATE KEY UPDATE

 INSERT INTO `Marks` (`studentid`, `lessonid`, `mark`) VALUE ( '$students[id]', '$lessonid', '$mark' ) ON DUPLICATE KEY UPDATE mark = '$mark'; 
  • And why leave the answer after the exact same was adopted? At the same time, no new / useful information has been added. - E_p
  • Wrote part of the answer, walked away. Came, finished and sent. Before this answer did not see. This happens. I hope not really hurt you with this) - mr.Mouse