如何处理来自共享相同值的多个复选框的数据?

z31licg0  于 2021-06-17  发布在  Mysql
关注(0)|答案(1)|浏览(292)

所以我有一个代码,它应该显示一个mysql表中的数据,每一行的末尾都包含一个复选框,如果选中,它的目的是在提交后将整行传输到另一个表中。代码运行得很好,但是不管您选择了多少行,它总是只移动一行,我这里的问题是如何进一步编码它,以便它可以一次命中所有选定的行
经过大量的研究,我不知何故得出结论,我应该使用foreach循环(我不熟悉),当我用这个替换$rowid时

foreach ($_POST['checks'] as $check){
        $rowid = $check;
    }

$rowid释放它的原始值,整个函数停止工作
我的工作代码只移动一行:

<?php 
$mysqli = new mysqli('localhost', 'root', 'password', 'orders');

$query = "SELECT * FROM received ORDER BY date";
$result = mysqli_query($mysqli,$query);
?>

<table>
<tr>
    <th>ID</th>
    <th>Client Name</th>
    <th>Contact Info</th>
    <th>Address</th>
    <th>Order Type</th>
    <th>Amount</th>
    <th>Date Ordered</th>
    <th>Complete Order</th>
</tr>
<form action='#' method='POST'>
<?php
if(mysqli_num_rows($result)>0){
    while($row = $result->fetch_assoc()){
        echo'
        <br />
        <tr>
            <td>'.$row["id"].'</td>
            <td>'.$row["clientname"].'</td>
            <td>'.$row["contactinfo"].'</td>
            <td>'.$row["address"].'</td>
            <td>'.$row["ordertype"].'</td>
            <td>'.$row["amount"].'</td>
            <td>'.$row["date"].'</td>
            <td><input type="checkbox" name="checks" value='.$row["id"].' /></td>
        </tr>';
    }
}
?>
</table>
<button name="complete" class="regbutton">Complete Orders</button>
</form>

<?php 
if (isset($_POST['complete'])){
    $rowid = $_POST['checks'];
        if(isset($_POST['checks'])){
            $query = "INSERT INTO delivered SELECT * FROM received WHERE id = '$rowid'";
            $query_run = mysqli_query($mysqli, $query);
            $query = "DELETE FROM received WHERE id = '$rowid'";
            $query_run = mysqli_query($mysqli, $query);
            echo "<script>alert('Eureka!');</script>";
        }
        else{
        echo "<script>alert('Nothing is checked!');</script>"; 
        }
}
?>

我希望$check变量能够显示所选行的确切id,并希望移动所选的每一行,但它没有这样做
我还试着把整个if(isset)($\u post['支票。。循环中的语句..其中id='$check。。但这两种方法都行不通

r8xiu3jd

r8xiu3jd1#

更改复选框的名称,使其成为如下数组

<input type="checkbox" name="checks[]" value='.$row["id"].' />

然后添加要在循环内执行的语句,同时更改where条件的变量,该变量表示选中的checkbox/s值(从$rowid到$check)

foreach ($_POST['checks'] as $check){

        if(isset($_POST['checks'])){
            $query = "INSERT INTO delivered SELECT * FROM received WHERE id = '$check'";
            $query_run = mysqli_query($mysqli, $query);
            $query = "DELETE FROM received WHERE id = '$check'";
            $query_run = mysqli_query($mysqli, $query);
            echo "<script>alert('Eureka!');</script>";
        }
        else{
        echo "<script>alert('Nothing is checked!');</script>"; 
        }
    }

答案取自问题的评论

相关问题