I’m trying to update multiple rows in a mysqli table from an HTML form. The data seems to be getting from the form to my "update database" page. But it’s not going into the database.
Here’s the relevant part of the form:
for($i=0;$i<$rowcount;$i++)
{
$row = mysqli_fetch_array($result);
echo "<tr>
<td> $row[SubFirstName] $row[SubLastName] </td>
<td> $row[PerfFirstName] $row[PerfLastName] </td>
<td style='display:none'><input type='text' class='input' name='PerformerID[]' value= '$row[PerformerID]' /> Years</td>
<td><input type='text' class='input' size= '5' name='GKYears[]' value= '$row[GKYears]' /> Years</td>
</tr>";
}
And here’s the code to insert the values into the database:
for($i=0;$i<$count;$i++)
{
mysqli_query($con, "UPDATE Performers SET
GKYears = '$_POST[GKYears][$i]'
WHERE PerformerID = '$_POST[PerformerID][$i]'");
}
When I do a var_dump of the POST data, it all seems to be there. Can someone explain how to fix this, and why it’s wrong? I’ve got other more complex variants of the same issue for the other pages.
echo mysqli_error($con);and you will see syntax errors. Those complex variables need to be{}wrapped as in'{$_POST['GKYears'][$i]}'to work correctly in that string. But, back up and read this thoroughly - your code is highly vulnerable to SQL injection. You need to be using prepared statements in MySQLi to secure your code against tampering.