-1

我试图让用户检查要删除的项目。当用户勾选一项或多项并单击删除按钮时,这些数据将从数据库中删除。我还添加了一个搜索框来搜索 DVD。搜索框有效,但删除无效。这就是它在浏览器中的样子。

PHP 删除

我的 PHP 看起来是这样的(我拿出了搜索代码):

<form action="" method="post">
<p><input type="text" name="search"> <input type="submit" value="Search"></p>
<p><input type="submit" name="deleting" value="Delete"></p>
</form>
<?php   
    $link = mysqli_connect( $host, $user, $password, $dbname);
    if (!$link) {
        die('Could not connect: ' . mysqli_connect_error());
    }
    echo 'Connected successfully<br/>';

//searching code goes here 

if (isset ($_POST['deleting']) && isset ($_POST['deleteThese']) )
{
$deleteThese = implode(",", $_POST['deleteThese']); 
$queryTwo = "DELETE FROM `$dbname`.`dvds` WHERE `dvds`.`DvdID` IN ($deleteThese)"; 
$resultTwo = mysqli_query($link, $queryTwo);
}

echo "<table border=\"1\"><tr><th>DvdTitle</th><th>RunningTime</th><th>Delete</th></tr>";
if (mysqli_num_rows($result) == 0)
   echo "<tr><td colspan='2'>No records found.</td></tr>";

   else {
    while ($row = mysqli_fetch_assoc($result)) {
    echo "<tr><td>" . $row['DvdTitle'] . "</td>";
    echo "<td>" . $row['RunningTime'] . "</td>";
    echo "<td>" . "<form>" . "<input type='checkbox' name='deleteThese[]' value='" . $row['DvdID'] . "' >" . "</form>" . "</td></tr>\n"; 
           }
        }
    echo "</table>";
    mysqli_free_result($result);
    mysqli_close($link);
?>

每个 DvdTitle 都有一个唯一的 Dvd ID,因此每一行的值就是 dvd 的 ID $row['DvdID']

4

2 回答 2

1

添加括号将允许选择这些 ID 进行删除。

 IN($deleteThese)

编辑

提交按钮后不要关闭表单。把它放在代码的末尾。这将允许表单包含复选框值。

<form action="" method="post">
<p><input type="text" name="search"> <input type="submit" value="Search"></p>
<!-- YOUR PHP CODE -->
<p><input type="submit" name="deleting" value="Delete"></p>
</form>

第二次编辑[要求改进代码]

将 isset 移动到表单顶部。

<?php
if (isset ($_POST['deleting']) && isset ($_POST['deleteThese']) )
{
  $deleteThese = implode(",", $_POST['deleteThese']); 
  $queryTwo = "DELETE FROM `$dbname`.`dvds` WHERE `dvds`.`DvdID` IN ($deleteThese)"; 
  $resultTwo = mysqli_query($link, $queryTwo);
}
?>
<form>....
于 2013-03-24T23:29:13.590 回答
0

$deletethese可能需要在它周围加上引号。

于 2013-03-24T23:34:36.500 回答