I have the following database structure.
ID | Name | Marks
I am trying to insert records in my PHP script. If the record doesn’t exist, I’m creating a new record. If it does exist, I’m concatenating the marks column with the new marks.
$strSQL = "Replace into student set ID = '".$id."',
name = '".$name."', marks= CONCAT_WS(',',Marks,'".$marks."')";
The query is running fine but concat is not working. The old record is being overwritten by the new one. I tried CONCAT too.
Thanks
The proper way to handle this is with a separate normalized table holding student ids and marks. Use
INSERT IGNOREto insert the student info in to thestudenttable, and then insert the marks into themarkstable, multiple rows per student. This also allows you to store a date along with each mark added to the table, or other information (like what course it belongs to). Note that I wouldn’t usually recommend usingINSERT IGNOREto create a row, but instead check first in code if the row exists and only insert if it doesn’t.