简体   繁体   中英

How to get value of an input if checkbox in that row is clicked in PHP

In my code I have it so that it will show a row with mysql and at the end I have an input to put in a date and then insert it into the database. I cannot figure out how to make it so that it updates each input individually with each checked box row.

$i = 0;
while($row = $result->fetch_assoc()) {
   echo  "<div class='row'>
    <div class='col-md-10 col-offset-1'>
    <tr>
    <td>
    <input type='checkbox' name='chk1[$i]' value='".$row['customerid']."' /> " .$row['firstname']." ".$row['lastname']. 
    "</td>
    <td>"  .
    $row['phonenumber'].
    "</td> 
    <td>" .$row['brand'].
    "</td>
    <td>" .$row['stock'].
    "</td>
    <td>" .$row['shoename'].
    "</td>
    <td>" .$row['size'].$row['width']. "</td>"; 
    echo "<td><input name='backorder' type='date'/></td>
    <td id='comments'>" .$row['comment']. "</td> 
    </div>
    </div>";
$cid[$i] = $row['customerid'];
$i++;
}

So when I update with this below it updates everything fine but gives all rows in the database the same value for the backorder.

if(isset($_POST['chk1'])) {
    foreach($_POST['chk1'] as $cid[$i]) {
        $chk1[] = array($cid[$i]);
        if(!empty($chk1)) {

            //include("email.php");
            $sql = "SELECT * FROM shoes WHERE customerid =" .$cid[$i];
            $result = $conn->query($sql);

            if ($result->num_rows > 0) {
                // output data of each row
                while($row = $result->fetch_assoc()) {
                    $texting = $row['texting'];
                    $name = $row['firstname'];
                    $email = $row['email'];
                    $provider = $row['provider'];
                    $brand = $row['brand'];
                    //$sql must be in this loop in order to execute all checkboxes.

                    $sql = "UPDATE shoes SET ordered=CURDATE(), backorder='backorder' WHERE customerid=".$cid[$i];

Your SQL sends a static string value for the update of that field.

$sql = "UPDATE shoes SET ordered=CURDATE(), backorder='backorder' WHERE customerid=".$cid[$i]; Needs to be updated to use something variable for the value or the string "backorder" will become the value for anything updated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM