gpt4 book ai didi

php - 使用复选框从 mysql 中删除多行?

转载 作者:行者123 更新时间:2023-11-29 15:00:17 25 4
gpt4 key购买 nike

如果存在重复的问题,我深表歉意。我试图找到并能在这里找到任何可以解决我的问题的东西..

我使用表单获取输入并在mysql数据库中更新它,然后检索html表单中的记录,并定义了通过超链接单独删除记录的代码。但是我想做更多,我想使用复选框删除多条记录。

我的代码是这样的。

<?php
//include connection string
include('connection.php');
?>
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="post"/>
Username : <input type="text" name="user"/><br />
Password : <input type="password" name="pass"/><br />
<input type="submit" name="submit" value="Send"/>
</form>
<?php
// query to insert into database
if(isset($_POST['user']) && isset($_POST['pass'])) {
$user = empty($_POST['user']) ? die(mysql_error()) : mysql_escape_string($_POST['user']);
$pass = empty($_POST['pass']) ? die(mysql_error()) : sha1(mysql_escape_string($_POST['pass']));
$query = "INSERT INTO users(name, pass) VALUES ('$user', '$pass')";
$result = mysql_query($query) or die(mysql_error());
}
if(isset($_GET['id'])) {
//query to delete the records
$query = "DELETE FROM users WHERE id = " . intval($_GET['id']);
$result = mysql_query($query);
}
//query to retrieve records
$query = "SELECT * FROM users";
$result = mysql_query($query);
if(mysql_num_rows($result) > 0 ) {
echo "<table cellpadding=10 border=1>";
while ($row = mysql_fetch_row($result)) {
echo "<tr>";
echo "<td>" . $row[0] . "</td>";
echo "<td>" . $row[1] . "</td>";
echo "<td>" . $row[2] . "</td>";
echo "<td><a href = " . $_SERVER['PHP_SELF'] . "?id=" .$row[0] . ">delete</a>";
echo "</tr>";
}
echo "</table>";
}
?>

我想让你知道,我是编程世界的新手,我不太确定 html 复选框的工作原理以及如何使用它来删除多条记录。我想知道我必须为此编写哪些额外的代码,如果有人简要地向我解释这些额外的代码,我将不胜感激。

谢谢你..

最佳答案

这可能是另一种形式的好时机:

<?php
// query to insert into database ...
// ... etc...

if(isset($_POST["formDeleteSelected"])) {
//query to delete the records
$query = "DELETE FROM users WHERE id IN (" . implode(", ",$_POST["rowid"]) . ")";
$result = mysql_query($query);
header("Location: mycode.php"); // just so 'refresh' doesn't try to run delete again
exit();
}
?>

<form action="<?php $_SERVER['PHP_SELF'] ?>" method="post">

<?php
//query to retrieve records
$query = "SELECT * FROM users";
$result = mysql_query($query);
if(mysql_num_rows($result) > 0 ) {
echo "<table cellpadding=10 border=1>";
while ($row = mysql_fetch_row($result)) {
echo "<tr>";
echo "<td><input type="checkbox" name="rowid[]" value=\"" . $row[0] . "\" /></td>";
echo "<td>" . $row[0] . "</td>";
echo "<td>" . $row[1] . "</td>";
echo "<td>" . $row[2] . "</td>";
echo "</tr>";
}
echo "</table>";
}
?>

<input type="submit" name="formDeleteSelected" text="Delete Selected" />
</form>

或者类似的东西(我实际上没有尝试过该代码,所以可能有错字)。另请注意,您应该确保清理所有表单/获取 SQL 注入(inject)的输入(其他 Stack Overflow 问题中有大量相关信息)。

关于php - 使用复选框从 mysql 中删除多行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3391245/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com