猿问

PHP MySQL 使用复选框更新多行

我想用 PHP 更新 MySQL 中的检查值


<?php


require "../../../../config.php";



if (isset($_POST['btn-upload'])) {

    try {


        $connection = new PDO($dsn, $username, $password, $options);


        $status = $_POST['status'];

        $ck_id = $_POST['ck_id'];


        for ($i = 0; $i < sizeof($ck_id); $i++) {

            $sql = "UPDATE form_eg208 SET status=:status where ck_id IN (:ck_id)";

            $statement = $connection->prepare($sql);

            $statement->bindParam(':status', $status[$i]);

            $statement->bindParam(':ck_id', $ck_id[$i]);


            $statement->execute();

        }


    } catch (PDOException $error) {

        echo $sql . "<br>" . $error->getMessage();

    }



    if ($statement->rowcount() >= 0) {


        echo '<div class="alert alert-success alert-dismissible" id="flash- 

                msg">

    <a href="#" class="close" data-dismiss="alert" aria-label="close">&times; 

                    </a>

                  <h4> <strong>Success!</strong> Insert Record  

                 Successfully</h4>

                  </div>';


    } else {

        echo '<div class="alert alert-danger alert-dismissible" id="flash- 

                   msg">

    <a href="#" class="close" data-dismiss="alert" aria-label="close">&times; 

                         </a>

                 <h4> <strong>Failed!</strong> Duplicate BGLPARTNO</h4>

                    </div>';

    }

}


?>

我的 html 代码是


<?php

$id = $_REQUEST['cid'];


try {


    $connection = new PDO($dsn, $username, $password, $options);

    $j = 1;

    $sql = "CALL view_eg208 (:bglpartno)";

    $statement = $connection->prepare($sql);

    $statement->bindParam(':bglpartno', $id);

    $statement->execute();


    $result = $statement->fetchAll();

} catch (PDOException $error) {

    echo $sql . "<br>" . $error->getMessage();


}

当我更新所有值时,它可以正常工作,但是当单个值时,它将第一个框的值更新到我选择的那个框中。请帮我解决这个问题


胡子哥哥
浏览 119回答 1
1回答

莫回无

请像这样输入您的复选框,以便您获得status正确的ck_id索引:if ($result && $statement->rowCount() > 0) {&nbsp;&nbsp; &nbsp; foreach ($result as $i => $row) { ?>&nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="checkbox" id="ck_id[]"class="filled-in chk-col-blue"&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name="ck_id[<?php echo $i ?>]" value="<?php echo escape ($row["ck_id"]);?>"/>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </td>&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td style="color:black"><?php echo escape&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ($row["activity_name"]);?></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="form-group has-danger"&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; style="margin-bottom: 0;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="text" id="status[]" name="status[<?php echo $i ?>]"&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; maxlength="100" class="form-control" value="<?&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; php echo escape ($row["status"]);?>" autocomplete="off"&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; required="required" >&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="invalid-feedback">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Please provide a Inputs.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </td>&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp;&nbsp; &nbsp; <?php&nbsp; &nbsp; }}也尝试使用foreach而不是for循环:&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; $connection = new PDO($dsn, $username, $password, $options);&nbsp; &nbsp; &nbsp; &nbsp; $status = $_POST['status'];&nbsp; &nbsp; &nbsp; &nbsp; $ck_id = $_POST['ck_id'];&nbsp; &nbsp; &nbsp; &nbsp; // for ($i = 0; $i < sizeof($ck_id); $i++) {&nbsp; &nbsp; &nbsp; &nbsp; foreach ($ck_id as $i => $value) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $sql = "UPDATE form_eg208 SET status=:status where ck_id IN (:ck_id)";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $statement = $connection->prepare($sql);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $statement->bindParam(':status', $status[$i]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $statement->bindParam(':ck_id', $ck_id[$i]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $statement->execute();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; } catch (PDOException $error) {&nbsp; &nbsp; &nbsp; &nbsp; echo $sql . "<br>" . $error->getMessage();&nbsp; &nbsp; }
随时随地看视频慕课网APP
我要回答