我想一次从复选框中删除db表中的多个用户。但我的代码无法正常工作 的的index.php
<?php
require 'db.php';
$query="SELECT * FROM usr_table";
$result = $db->query($query) or die(mysqli_error($mysqli));
$num=mysqli_num_rows($result) or die(mysqli_errno());
if ($result) {
echo '
<form method="post" action="delete.php">
<table id="list">
while ($row = $result->fetch_object()) {
$id = $row->id;
$fullname = $row->fullname;
$dob = $row->dob;
$phone= $row->phone;
$adress= $row -> adress;
$school = $row->school;
echo '<tr>
<td>'.$id.'</td>
<td>'.$fullname.'</td>
<td>'.$dob.'</td>
<td>'.$phone.'</td>
<td>'.$adress.'</td>
<td>'.$school.'</td>
<td><input type="checkbox" name="checkbox[]" id="checkbox[]" method="post" value=$id />
</tr>';
}
// when the loop is complete, close off the list.
echo "</table><p><input id='delete' type='submit' class='button' name='delete' value='Delete Selected Items'/></p></form>";
}
?>
delete.php
<?php
require 'db.php';
$delete=$_GET['delete'];
if($delete) // from button name="delete"
{
$checkbox = $_GET['checkbox']; //from name="checkbox[]"
$countCheck = count($_GET['checkbox']);
for($i=0;$i<$countCheck;$i++)
{
$del_id = $checkbox[$i];
$sql = "DELETE from usr_table where id = '$del_id'";
$result = $db->query($sql) or die(mysqli_error($db));
}
if($result)
{
header('Location: index.php');
}
else
{
echo "Error: ".mysqli_error($db);
}
}
?>
db.php中
<?php
$db = new mysqli('localhost', 'user' ,'pass', 'table') or die(mysqli_errno());
$db->query("SET names UTF8") or die(mysqli_errno());
foreach ($_POST as $key => $value) {
$_POST[$key] = mysqli_real_escape_string($value);
}
?>
请帮忙!它不起作用 HTML版本
<form method="post" action="delete.php">
<table id="list">
<thead>
<tr>
<th width="5%">ID</th>
<th width="35%">Ad və soyad</th>
<th width="15%">Təvəllüd</th>
<th width="15%">Telefon</th>
<th width="20%">Ünvan</th>
<th width="5%">Məktəb</th>
<th width="5%">#</th>
</tr>
</thead><tr>
<td>20</td>
<td>Tural Teyyuboglu</td>
<td>1992-06-09</td>
<td>4940301</td>
<td>Baki Yasamal Zergerpalan</td>
<td>189</td>
<td><input type="checkbox" name="checkbox[]" id="checkbox[]" method="post" value="20 "/>
</tr><tr>
<td>22</td>
<td></td>
<td>1992-06-09</td>
<td>0</td>
<td></td>
<td></td>
<td><input type="checkbox" name="checkbox[]" id="checkbox[]" method="post" value="22 "/>
</tr><tr>
<td>23</td>
<td>Tural Turik</td>
<td>1992-06-09</td>
<td>4940301</td>
<td>Bakı Yasamal Zərgerpalan</td>
<td>189</td>
<td><input type="checkbox" name="checkbox[]" id="checkbox[]" method="post" value="23 "/>
</tr><tr>
<td>24</td>
<td>Tural Əliyev</td>
<td>1992-06-09</td>
<td>4940301</td>
<td>Bakı Y </td>
<td></td>
<td><input type="checkbox" name="checkbox[]" id="checkbox[]" method="post" value="24 "/>
</tr><tr>
<td>25</td>
<td>Zammbbb</td>
<td>1992-06-09</td>
<td>4940301</td>
<td>Bakı Uadnf` skbnfias</td>
<td>189</td>
<td><input type="checkbox" name="checkbox[]" id="checkbox[]" method="post" value="25 "/>
</tr><tr>
<td>26</td>
<td>Tural Əliyev</td>
<td>1992-06-09</td>
<td>0</td>
<td> </td>
<td></td>
<td><input type="checkbox" name="checkbox[]" id="checkbox[]" method="post" value="26 "/>
</tr></table><p><input id='delete' type='submit' class='button' name='delete' value='Delete Selected Items'/></p></form> </td>
</tr>
</table>
答案 0 :(得分:2)
这里有一些问题......
1)您的表单method="POST"
。在您的delete.php中,您正在使用$_GET['checkbox'].
请改用$_POST['checkbox']
。 ($_GET['delete']
必须为$_POST['delete']
)。
2)你正在回应PHP代码:
if ($result) {
echo '
<form method="post" action="delete.php">
<table id="list">
while ($row = $result->fetch_object()) {
$id = $row->id;
$fullname = $row->fullname
在继续使用PHP代码之前,您需要结束echo语句:
if ($result) {
echo<<<formheader
<form method="post" action="delete.php">
<table id="list">
formheader;
while ($row = $result->fetch_object()) {
$id = $row->id;
$fullname = $row->fullname
3&amp; 4)在输入元素中的引号周围加上引号,如下所示:
<input type="checkbox" name="checkbox[]" id="checkbox" value="$id" />
如果你要像那样回应,那么就转义你的双引号,因为它在一个echo语句中:
<input type=\"checkbox\" name=\"checkbox[]\" id=\"checkbox\" value=\"$id\" />
5)您的$_POST
清理是从db.php
清空您的多维$ _POST ['复选框']数组的内容。
当我从db.php
foreach ($_POST as $key => $value) {
$_POST[$key] = mysqli_real_escape_string($value);
}
然后您的print_r($_POST)
应该显示您的值。假设其他一切都没问题,那应该可行。但是,您需要重新考虑如何清理POST变量。