<?php
include("connection.php");
echo "Do you really want to DELETE this field? " ;
?>
<form method="post">
<input type="submit" name="yes" value="YES" />
<?php
$res = mysql_query("SELECT * FROM members");
while($row = mysql_fetch_array($res))
{
if(isset($_REQUEST['yes']))
{
$id=$row['id'];
echo $id;
mysql_query("DELETE from members WHERE id= '$id' ");
header("location: view.php");
}
}
?>
<input type="submit" name="no" value="NO" />
<?php
if(isset($_REQUEST['no']))
{
header("location: view.php");
}
?>
</form>
</body>
这是我点击删除按钮时从列表视图中删除随机行的代码,但我没有得到结果,所以请帮我解决这些问题。
答案 0 :(得分:0)
更改此行:
mysql_query("DELETE from members WHERE id= '$id' ");
到那个:
mysql_query("DELETE FROM members WHERE id= '".$id."' ");
答案 1 :(得分:0)
将db connection作为第二个参数传递给mysql_query()以从成员表中删除行:
mysql_query("DELETE from members WHERE id= '$id'" , $dbCon);
此处数据库连接我指的是您在 connection.php 文件中定义的内容:
$db_host="localhost";
$db_name="test";
$username="root";
$password="";
$dbCon=mysql_connect($db_host,$username,$password);
我希望这能解决你的问题!
答案 2 :(得分:0)
您的查询SELECT * FROM members
没有订购会以不可预测的顺序选择所有行。
if
循环中的while
将始终为true或始终为false:
如果用户点击“是”按钮,全部,表格的行将被删除,因为exit;
之后没有header("location: view.php");
(见this post)
如果用户单击“否”按钮,则while循环的每次迭代都不会执行任何操作。
那么你应该做什么:
exit;
header("location: view.php");
答案 3 :(得分:0)
<?php
include("connection.php");
echo "Do you really want to DELETE this field? " ;
?>
<form method="post">
<input type="submit" name="yes" value="YES" />
<?php
if(isset($_REQUEST['yes']))
{
if(isset($_GET['delet']))
{
$id = $_GET['delet'];
$res = mysql_query("SELECT * FROM members WHERE id = '$id' ");
$row = mysql_fetch_array($res);
$did = $row['id'];
$dname = $row['name'];
$dphn = $row['phn'];
/*echo $did;
echo $dname;
echo $dphn;*/
$ins = "INSERT INTO del (mid,name,phn) values ('$did','$dname','$dphn')";
mysql_query($ins);
mysql_query("DELETE FROM members WHERE id = '$id' ");
header('location: view.php');
}
}
?>
<input type="submit" name="no" value="NO" />
<?php
if(isset($_REQUEST['no']))
{
header("location: view.php");
exit;
}
?>
</form>