Php 是否使用复选框从mysql中删除多行?

Php 是否使用复选框从mysql中删除多行?,php,mysql,Php,Mysql,如果这个问题存在重复,我想道歉。我试着在这里找到任何可以解决我问题的东西 我使用一个表单获取输入并在mysql数据库中更新它,然后以html表单检索记录,并定义了通过超链接分别删除记录的代码。然而,我想做更多的事情,我想使用复选框删除多个记录 我的代码是这样的 <?php //include connection string include('connection.php'); ?> <form action="<?php $_SERVER['PHP_SELF'] ?&

如果这个问题存在重复,我想道歉。我试着在这里找到任何可以解决我问题的东西

我使用一个表单获取输入并在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>";
}
?>


首先,您需要一个复选框和要删除的id:

<input id="delete" type="checkbox" name="delete" /><label for="delete">Delete user</label>
<input type="hidden" name="user_id" value="12345" />

这不是最优雅的解决方案,但非常简单,可以与您的代码一起使用。

尝试使用ID列表进行SQL查询

... WHERE id=$sentIds[0] OR id=$sentIds[1] OR ...
或者使用set操作

 ... WHERE id IN ($i1,$i2 ... );

您确实必须在表单中发送ID才能使其工作,但您知道;)

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

<?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>

<?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>