Still getting the hang of PHP and would like some guidance on the best way to make this code loop. I have got the php code that generates the html code working, it creates a checkbox for each entry in my MySQLi database using a WHILE command. I need to do something similar with this code.
mysqli_select_db($conn,'database_1')or die("Connection Failed");
$check_0 = $_POST['checkbox0'];
$query_0 = "UPDATE test2 SET state = '$check_0' WHERE id = '0'";
mysqli_query($conn, $query_0);
$check_1 = $_POST['checkbox1'];
$query_1 = "UPDATE test2 SET state = '$check_1' WHERE id = '1'";
mysqli_query($conn, $query_1);
$check_2 = $_POST['checkbox2'];
$query_2 = "UPDATE test2 SET state = '$check_2' WHERE id = '2'";
mysqli_query($conn, $query_2);
mysqli_close($conn);
As you can see from the code, I just need to have the numbers update, so that the next lines of code should have;
$check_3, 'checkbox3', $query_3, & id = '3'
If I could do something like;
$check_x where id = x that would be ideal, and then loop while id has a value. Thanks for any help.
EDIT 1
Here is the checkbox PHP that works. The code does 3 things; create a checkbox, send checkbox check status to the above code, and apply the state of the MySQLi database to the checkbox.
echo(string) "<form action='includes/checkbox.inc.php' method='post'>";
while($row_3 = mysqli_fetch_array($result_4, MYSQLI_ASSOC)) {
echo "<input type='hidden' name='checkbox";
echo $row_3['id'];
echo "' value='0'><input type='checkbox' name='checkbox";
echo $row_3['id'];
echo "' value='1'";
/*Below section for check status*/
if ($result_4 = $sql_3) {
mysqli_data_seek($result_4, $row_3['id']);
$row_4 = mysqli_fetch_row($result_4);
}
if ($row_4 [0]=="1") {
echo "checked";
} else {
echo " ";
}
/*Above section for check status*/
echo "> Item ";
echo $row_3['id'];
echo "<br>";
}
echo "<input type='submit' name='Submit' value='Submit'>";
echo "</form>";
?>