0

已经为此忙碌了一段时间,但在删除一行后仍然无法重新加载页面。

行删除工作正常,但仅在我重新加载页面后才会出现。一直在移动一些代码以在按下提交时读取它,但没有运气。有任何想法吗 ?

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

2 回答 2

2

我假设您提供的代码是您的应用程序代码的片段,该代码段$resultselect在此部分执行之前设置在某个位置。

这里重要的部分是 MySQL 查询的顺序。如果您在行之前SELECTing 行,那么您在运行时仍会看到已删除的行DELETEmysql_fetch_array()

SELECT在查询之后移动DELETE查询,一切都应该没问题。

于 2012-06-28T06:09:47.547 回答
1

看起来您在删除语句之前的一段代码中获得了 $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>
于 2012-06-28T06:07:38.227 回答