mardi 23 juin 2015

Insert multiple checkbox values from html table rows into mysql table

I am trying to insert checkbox values from multiple rows into a mysql table. It will apply the changes only to the most recent row added. And all new changes to previous rows also get applied to the most recent row. Any help is appreciated. Thank you.

<?php 
//database connection code
$query3 = "SELECT * FROM Teaching_Prep WHERE ID='$user' order by ContentPrepYeardesc";
$result3=mysqli_query($con, $query3);
$preprow = mysqli_fetch_array($result3);
?> 

<form action="main.php" method="post">
<table> 
<?php

$transactions_prep = mysqli_query($con, "SELECT * FROM Teaching_Prep WHERE ID='$user' order by ContentPrepYear desc");

while($preprow = mysqli_fetch_array($transactions_prep))
{                     

?>                       

<tr><td>
<input type="checkbox" name="algIinclusion[]" value="1" <?php if ($preprow['Alg_I_Inclusion']=="1") echo 'checked="checked"'; ?> AlgebraIInclusion
&nbsp; &nbsp;<input type="checkbox" name="preAlgebra8[]" value="1" <?php if ($preprow['PreAlg_8th']=="1") echo 'checked="checked"'; ?> >Pre Algebra 8th
</td></tr>

<?php                      
  }//while
?>  

<input type="submit" value="Update" />

</form>  

<?php 

if ($_SERVER["REQUEST_METHOD"] == "POST"){ 
$transactions1 = mysqli_query($con, "SELECT * FROM Teaching_Prep WHERE ID='$user'");

    $count_prep=mysqli_num_rows($transactions1);

    for($i=0;$i<$count_prep;$i++)
    {
        $Alg_I_Inclusion=$_POST['algIinclusion'][$i];
        $PreAlg_8th=$_POST['preAlgebra8'][$i];

        $sql4 = "update Teaching_Prep set Alg_I_Inclusion='$Alg_I_Inclusion', PreAlg_8th='$PreAlg_8th' where ID='$user' and ContentPrepYear='$ContentPrepYearValue'";
        if (!mysqli_query($con, $sql4)) 
            {
                echo "Failed SQL4";
                die('Error: ' . mysqli_error($con));
            }  
}//for

mysqli_close($con);

?>




Aucun commentaire:

Enregistrer un commentaire