已经忙碌了一段时间但是在删除一行后仍然无法重新加载页面。
行删除工作正常,但只有在我重新加载页面后才会显示。 一直在移动一些代码,以便在按下提交时读取但没有运气。 任何想法?
<?php
mysql_connect("localhost", "root", "") or die(mysql_error()) ;
mysql_select_db("booking") or die(mysql_error()) ;
$resultselect = mysql_query("SELECT * FROM test_mysql ") or die(mysql_error());
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>row delete</title>
<link rel="stylesheet" type="text/css" href="" />
</head>
<body>
<?php
if(isset($_POST['done'])){
$done = implode(',', $_POST['done']);
$deletequery = "DELETE FROM test_mysql WHERE id IN ($done)";
$resultdelete = mysql_query($deletequery) or die(mysql_error());
}
?>
<form method="post" action="">
<?php while($row = mysql_fetch_array($resultselect)){?>
<input type="checkbox" name="done[]"
id="<?php echo $row['id'] ?>"
value="<?php echo $row['id'] ?>">
<label for="<?php echo $row['id'] ?>">
<?php echo $row['name'] . ' ' . $row['lastname']. ' '.$row['email']; ?>
</label>
<br />
<?php } ?>
<input type ="submit" value ="submit">
</form>
<?php
if($resultdelete) {
echo 'Item(s) deleted from list.';
}
?>
<pre>
<?php print_r($_POST) ?>
</pre>
</body>
</html>
答案 0 :(得分:2)
我假设,因为您提供的代码是您的应用程序代码的片段,$resultselect
在此部分执行之前就已设置好。
这里重要的部分是MySQL查询的顺序。如果您在SELECT
行之前DELETE
行,那么当您运行mysql_fetch_array()
时,您仍会看到已删除的行
在SELECT
查询后移动DELETE
查询,一切正常。
答案 1 :(得分:1)
看起来你在delete语句之前的代码段中得到$ row的结果集。如果将$ deletequery代码放在页面顶部,则可以删除相应的数据记录,然后获取新的结果集,然后将其显示给用户。
无论您的循环是什么查询
while($row = mysql_fetch_array($resultselect))
{
....
}
需要在
之后运行$resultdelete = mysql_query($deletequery) or die(mysql_error());
语句。
编辑要运行的完整代码:
<?php
mysql_connect("localhost", "root", "") or die(mysql_error()) ;
mysql_select_db("booking") or die(mysql_error()) ;
if(isset($_POST['done']))
{
$done = implode(',', $_POST['done']);
$deletequery = "DELETE FROM test_mysql WHERE id IN ($done)";
$resultdelete = mysql_query($deletequery) or die(mysql_error());
}
$resultselect = mysql_query("SELECT * FROM test_mysql ") or die(mysql_error());
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>row delete</title>
<link rel="stylesheet" type="text/css" href="" />
</head>
<body>
<form method="post" action="">
<?php while($row = mysql_fetch_array($resultselect)){?>
<input type="checkbox" name="done[]"
id="<?php echo $row['id'] ?>"
value="<?php echo $row['id'] ?>">
<label for="<?php echo $row['id'] ?>">
<?php echo $row['name'] . ' ' . $row['lastname']. ' '.$row['email']; ?>
</label>
<br />
<?php } ?>
<input type ="submit" value ="submit">
</form>
<?php
if($resultdelete) {
echo 'Item(s) deleted from list.';
}
?>
<pre>
<?php print_r($_POST) ?>
</pre>
</body>
</html>