带复选框的多用户删除不起作用
我想一次从带有复选框的数据库表中删除多个用户。但我的代码无法正常工作 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>
I want to delete multiple users from db table with checkboxes at once. But my code doesn't work correctly
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);
}
?>
Please help! It doesn't work
HTML version
<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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里有一些问题..
1) 您的表单
method="POST"
。在您的delete.php 中,您使用的是$_GET['checkbox']。
请改用
$_POST['checkbox']
。 ($_GET['delete']
需要为$_POST['delete']
)。2) 您正在回显 PHP 代码:
您需要先结束 echo 语句,然后再继续 PHP 代码:
3 & 4) 在输入元素中的值周围加上引号,如下所示:
如果您要像这样回显,请转义双引号,因为它位于 echo 语句中:
5) 您的
$_POST
清理就是从db.php
中清空多维 $_POST['checkbox'] 数组。当我从 db.php 中删除它时
,您的 print_r($_POST) 应该显示您的值。假设其他一切都正常,那应该可行。但是,您需要重新考虑如何清理 POST 变量。
There are a few things wrong here..
1) Your form
method="POST"
. In your delete.php, you are using$_GET['checkbox'].
Use
$_POST['checkbox']
instead. ($_GET['delete']
needs to be$_POST['delete']
).2) You're echoing PHP code:
You need to end your echo statement before continuing with PHP code:
3 & 4) Put quotes around your values in your input element, like so:
And if you're going to echo like that, then escape your double quotes since it is in an echo statement:
5) Your
$_POST
sanitizing is what is emptying your multi-dimensional $_POST['checkbox'] array fromdb.php
.When I remove this from
db.php
Then your
print_r($_POST)
should display your values. Assuming everything else is alright, that should work. However, you'll need to reconsider how you sanitize your POST variables.