I’m trying to get some array data to update via a form and for some reason, although it doesn’t throw an error, it doesn’t update- it’s as if the whole code is ignored.
The code below is part of a form, and shows the existing data for the record.
$getmotdetails = $mysqli->query('SELECT * FROM mothistorytable WHERE carid='.$refnumber);
while ($row = $getmotdetails->fetch_assoc()) {
$numberofrows = $getmotdetails->num_rows;
$motcarid = $row["carid"];
$motdate = $row["motdate"];
$motodometer = $row["motodometer"];
$customorder = $row["CustomOrder"];
$id = $row["id"];
$output = "";
for ($i=0; $i<$numberofrows; $i++) {
$output = ' <div class="addnewcar-wide"><input type="hidden" name="_ID['.$i.']" value='.$id.' /><label for="_MotDate">Date</label><input name="_MotDate['.$i.']" type="text" size="10" value="'.$motdate.'" /><label for="_MotOdometer">Odometer</label><input name="_MotOdometer['.$i.']" type="text" size="20" value="'.$motodometer.'" /></div>';
}
echo $output;
$count++ ;
}
Below is the code that should make any updates to the fields in the form:
$motdate = $_POST['_MotDate'];
$motodometer = $_POST['_MotOdometer'];
$id = $_POST['_ID'];
for ($i=0; $i<count($motdate); $i++) {
if ($motdate[$i]!="" && $motodometer[$i]!="") {
$updatemotdetails = $mysqli->query('UPDATE mothistorytable SET motdate = "'.$motdate.'", motodometer = "'.$motodometer.'" WHERE `id` = '.$id);
}
}
If I echo the $motdate, $motodometer and $id variables I get nothing, so it’s as if the form just isn’t sending anything through (but I don’t get any error). But the form displays the existing values ok in the inputs.
The weird thing is that a similar query, for adding some new values from blank inputs, works fine:
echo '<p><strong>Add any new MOT details required in the slots below.</strong></p>';
$newoutput = "";
$desired_row_count = 20;
for ($i=0; $i<$desired_row_count; $i++) {
$newoutput .= ' <div class="addnewcar-wide"><label for="_NewMotDate">Date</label><input name="_NewMotDate['.$i.']" type="text" size="10" /><label for="_NewMotOdometer">Odometer</label><input name="_NewMotOdometer['.$i.']" type="text" size="20" /></div>';
}
echo $newoutput;
And then:
//MOT ADD NEW RECORDS FORM
$newmotdate = $_POST['_NewMotDate'];
$newmotodometer = $_POST['_NewMotOdometer'];
echo $newmotdate;
for ($i=0; $i<count($newmotdate); $i++) {
if ($newmotdate[$i]!="" && $newmotodometer[$i]!="") {
$addmotdetails = $mysqli->query('INSERT INTO mothistorytable (`carid`,`motdate`,`motodometer`) VALUES ('.$carid.',"'.$newmotdate[$i].'","'.$newmotodometer[$i].'")');
}
}