jeudi 20 août 2015

How to loop through arrays to insert into database

I have three arrays and I want to add the arrays in position [0], then [1], then [2] etc of the arrays so I can insert the values for the three fields into my database.

Form:

<td class="paid">
    <input type="hidden" value="1" name="paid[<?php echo $res['name'];?>]">
    <input type="checkbox" value="0" name="paid[<?php echo $res['name'];?>]"

        <?php 
            if($res["paid"]==0)
            {
            echo "checked";
            }               
        ?>>

</td>
<td class="active">
     <input type="hidden" value="1" name="active[<?php echo $res['name'];?>]">
    <input type="checkbox" value="0" name="active[<?php echo $res['name'];?>]" 
        <?php 
            if($res["active"]==0)
            {
                echo "checked";
            }        
        ?> >
</td>


    <input type="hidden" name="ID[<?php echo $res['name'];?>]" value="<?php echo $res['ID']; ?>">


</tr>
<?php } ?>
<tr>
<td>
<input type="submit" name="submit" value="Update">
</td>
</tr>

</table>
</form>

</body>
</html>

php:

$paid=$_POST['paid'];
$active=$_POST['active'];


foreach($_POST as $key=>$value) 
{
    $ID=$ID[$key];
    $paid=$paid[$key];
    $active=$active[$key];

$up=updatePayment($paid,$active,$ID);
$r = mysqli_query($mysqli,$up); 
echo "Information stored successfully";

}

?>

my function:

function updatePayment($paid,$active,$ID)
{
    $uc="UPDATE
            `company`
        SET
            `paid`='$paid',
            `active`='$active'
        WHERE
            `ID`='$ID'";

    return $uc;
}

Right now its not inserting anything, I can see the arrays have the proper data in them with var_dump($_POST); but I cant seem to get it to insert into the database. Ive tried some for loops, foreach but not sure I'm doing it right.

Any ideas?




Aucun commentaire:

Enregistrer un commentaire