I am new to PHP Programming.
I am working on a PHP project, where I am populating a dynamic table from mysql database with a checkbox field with each row.
<?php
$sql = "select * from caseinstruction where fir_nos='$fir_nos'";
$result = $con->query($sql);
if ($result->num_rows > 0) {
// output data of each row
$i = 1;
while($row = $result->fetch_assoc()) {
echo "<tr><td class='text-center'>". $i++ ."</td>
<td><input type='text' value='{$row['Instructions']}' class='form-control input-sm' style='border:none'></td>
<td class='text-center form-group'><input type='checkbox' name='foo[]' value='1'></td>
</tr>\n";
}
}
?>
Its working perfectly. The problem arises when I want to update the same mysql table with the checked value. Suppose if a user select a check box then it will reflect 1 in the mysql table and if not selected any option then it will reflect 0 in mysql table.
I tied to run the foreach command it populates the value as I expect but when I run the update command it just reflect 0. Please help me.
<?php
require('../php-includes/connect.php');
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
$fir_nos = $_POST['fir_nos'];
$cins = $_POST['foo'];
echo '<pre>';
print_r($cins);
echo '</pre>'
foreach ($$cins as $value) {
$sql="update mytable set report02='$value'";
$result = $con->query($sql);
}
}
I am missing something on this. Please Help me. thanking you all in advance.
# | Instruction | value |
-------------------------
1 | abc | 1 | //checked
2 | cdf | 0 | //unckecked
3 | efg | 1 | //checked
-------------------------