所以我有一个代码,该代码应该显示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($ _ POST ['checks ..语句放入循环..WHERE id ='$ check ..,但是这两种方法都不起作用
答案 0 :(得分:0)
更改复选框的名称,以便它可以变成这样的数组
<input type="checkbox" name="checks[]" value='.$row["id"].' />
然后添加要在循环内执行的语句,还更改代表所选复选框值的WHERE条件变量(从$ 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>";
}
}
从问题评论中得到的答案