I have a table with rows in an SQL database that I want to be able to change the values for. I have built a form that has a select option built in so a row of data can be changed in the database. I want the ability to be able to change multiple rows at the same time in a database by just submitting one form. This is my code to create the form and the table with php. I can figure out how to get all the values over to the updatedb.php page, but from there I don't know how to update multiple rows in the database.
<form action="updatedb.php" method="POST">
<tbody>
<?php
$servername = "localhost";
$username = "******";
$password = "******";
$dbname = "database";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM table WHERE value = '$value'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$test = $row["value3"];
$test2 =$row["value5"];
if ($row["status"] == "Value1") { $status1 = 'selected'; } else { $status1 = ''; }
if ($row["status"] == "Value2") { $status2 = 'selected'; } else { $status2 = ''; }
if ($row["status"] == "Value3") { $status3 = 'selected'; } else { $status3 = ''; }
if ($row["status"] == "Value4") { $status4 = 'selected'; } else { $status4 = ''; }
if ($row["status"] == "Value5") { $status5 = 'selected'; } else { $status5 = ''; }
//This is the table that is created
echo "
<tr><td>" . $row["value"] . $row["value2"] . "</td>
<td>" . $row["value3"] . "</td>
<td>" . $row["value4"] . "</td>
<td>" . $row["value5"] . "</td>
<td>" . $row["value6"] . "</td>
<td>" . $row["value7"] . "</td>
<td>
<select name='status[]' required>
<option $status1 value='Status1'>Status1</option>
<option $status2 value='Status2'>Status2</option>
<option $status3 value='Status3'>Status3</option>
<option $status4 value='Status4'>Status4</option>
<option $status5 value='Status5'>Status5</option>
</select>
</td>
<td>" . $row["value8"] . "</td></tr>";
echo "
<input name='send[]' value='$test' style='display: none' />".
"<input name='send2[]' value='$test2' style='display: none' />";
}
} else {
//No data to show
}
$conn->close();
?>
</tbody>
<button type="submit">Submit</button>
</form>