samedi 5 septembre 2015

checkbox value in a post array not in correct mysql row

Hi i am trying to convert a input form field into a checkbox. in the input field i enter either 1 or 0 this works fine and stores correctly in MySQL database. I am trying to change the input field to a checkbox so checked = 1 and unchecked =0 .

When i change the input into a checkbox value it does not work correctly. The values get stored in the wrong rows in mysql. Could someone help please

<?php include('config.php'); ?>
<?php       $mm = "SELECT * FROM test_mysql";
    $result = mysqli_query($mysqli, $mm) or die('-1'.mysqli_error());
 $count = $result->num_rows;
printf("Result set has %d rows.\n", $count);
// Count table rows 

?>

<table width="500" border="0" cellspacing="1" cellpadding="0">
<form  method="post" action="">
<tr> 
<td>
<table width="500" border="0" cellspacing="1" cellpadding="0">

<tr>
<td align="center"><strong>Id</strong></td>
<td align="center"><strong>Name</strong></td>
<td align="center"><strong>Lastname</strong></td>
<td align="center"><strong>Paid</strong></td>
</tr>

<?php
        while ($rows = mysqli_fetch_assoc($result)) {
            $checked = $rows['paid'];
?>

<tr>
<td align="center">
<input name="id[]" type="text" id="id" value="<? echo $rows['id']; ?>">
</td>
<td align="center">
<input name="name[]" type="text" id="name" value="<? echo $rows['name']; ?>">
</td>
<td align="center">
<input name="lastname[]" type="text" id="lastname" value="<? echo $rows['lastname']; ?>">
</td>
<td align="center">
<?php /*?><input name="paid[]" type="text" id="paid" value="<? echo $rows['paid']; ?>"><?php */?>
</td>
<td align="center">
<input type="hidden" name="paid[]" value="0" />
<input type="checkbox" name="paid[]" <?php if ($checked == 1 ) echo ' checked' ?>/>

</td>
</tr>
<?php } ?>
<tr>
<td colspan="4" align="center"> <button type="input" name="submit" value="editTask" class="btn btn-success btn-lg btn-icon"><i class="fa fa-check-square-o"></i>submit</button></td>
</tr>
</table>
</td>
</tr>
</form>
</table>
        <?php

// Check if button name "Submit" is active, do this 
if (isset($_POST['submit']) && $_POST['submit'] == 'editTask'){
    $entryId = $mysqli->real_escape_string($_POST['id']);

    foreach($_POST['id'] as $key=>$id) {

        $name = $_POST['name'][$key];
        $lastname = $_POST['lastname'][$key];
        $paid = $_POST['paid'][$key];
$stmt = $mysqli->prepare("UPDATE test_mysql SET name=?, lastname=?, paid=? WHERE id = ?");
$stmt->bind_param('ssss', $name, $lastname, $paid,$id);
$stmt->execute();

}
$stmt->close();
header("location:page.php");
}
?>

Every work fine until i change the input paid into a checkbox. What am I doing wrong. I have commented the input paid out as you can see in the code and added a hidden input called name=paid[] and checkbox name=paid[]

<input type="hidden" name="paid[]" value="0" />
<input type="checkbox" name="paid[]" <?php if ($checked == 1 ) echo ' checked' ?>/>

thanks jon




Aucun commentaire:

Enregistrer un commentaire