I'm trying to insert data from a select multiple form field into a MySQL db.
Each insert row needs to have the same reference field,($_POST['progID'] in the code below. The select array comes from $_POST['lessonID']. So if the array is filename1, filename5, filename 6, etc. they would be entered into distinct rows, each row having the same $progID. Below is my code sample. (I haven't done any sanitization, etc. of the data yet.)
if(isset($_POST["submit"])){
$progID=$_POST['progID'];
$lessonIDs=$_POST['lessonID'];
$comments=$_POST['comments'];
foreach ($lessonIDs as $value){
$lessonIDs= $value;
}
$query = "INSERT INTO lesson_school_lessonstocourses SET
date=current_timestamp(),
progID='" . str_replace("'", "'", $progID) . "',
lessonID='" . str_replace("'", "'", $value ) . "',
comments = '" . str_replace("'", "'", $comments ) . "'
";
if (mysqli_query($link,$query)) {
$last_id = mysqli_insert_id($link);
echo "New record created successfully<br>";
}
else {
echo("<p>Error adding lesson: " .
mysqli_error($link) . "</p>");
}
I expect to see many rows with the same progID, with each row having a distinct lessonID. All I get now is only one row entered with the proper progID (instead of many rows), but with the lessonID column is "0". I'm not getting any on-screen errors.