PHP从数据库删除不工作 [英] PHP deleting from database not working

查看:112
本文介绍了PHP从数据库删除不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让使用者检查要删除的项目。当用户检查一个或多个项目并单击删除按钮时,这些数据将从数据库中删除。我还添加了一个搜索框来搜索dvd。搜索框工作,但删除不工作。这是它在浏览器中的样子。







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

 < form action =method =post> 
< p>< input type =textname =search> < input type =submitvalue =Search>< / p>
< p>< input type =submitname =deletingvalue =Delete>< / p&
< / form>
<?php
$ link = mysqli_connect($ host,$ user,$ password,$ dbname);
if(!$ link){
die('Could not connect:'。mysqli_connect_error());
}
echo'已成功连接< br />';

//搜索代码到这里

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'>未找到记录< / 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> \\\
;
}
}
echo< / table>;
mysqli_free_result($ result);
mysqli_close($ link);
?>

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

解决方案

添加括号将允许选择要删除的ID。

  IN($ deleteThese)

EDIT



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

 < form action =method =post> 
< p>< input type =textname =search> < input type =submitvalue =Search>< / p>
<! - 您的PHP代码 - >
< p>< input type =submitname =deletingvalue =Delete>< / p&
< / form>

第二次修改[请求改进代码]
$ b

在表单顶部移动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> ....


I'm trying to let the user check off which item to be deleted. When the user check off one or many items and click the Delete button, those data will be erased from the database. I've also added a search box to search for the dvd. The search box works, but the deleting doesn't. This is what it looks like in the browser.


My PHP looks like this (I took out the searching code):

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

Each DvdTitle has an unique Dvd ID, hence the value of each row is the dvd's ID $row['DvdID'].

解决方案

Adding the parentheses will allow for those ID's to be selected for deletion.

 IN($deleteThese)

EDIT

Do not close the form after the submit button. Put that at the end of the code. This will allow the form to include the checkbox values.

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

2nd Edit [requested to improve code]

Move the isset on top of the form.

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

这篇关于PHP从数据库删除不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆