-1

我有一个带有 HTML 表单的 PHP 页面,用于更改 MySQL 数据库中的一些信息。表单提交到另一个 PHP 页面,该页面根据从表单收到的信息进行大约 7-10 次查询。这些查询按特定顺序排列非常重要。我mysqli::multi_query用来执行这些查询。最后,在我执行查询后,我使用header("Location: " . $_SERVER['HTTP_REFERER']);表单将用户返回到页面。当用户返回上一页时,我的问题就出现了。页面在查询中间出现的时间点加载,而不是在成品中加载。如果我然后点击刷新它会加载更新的信息。如何防止我的页面加载,直到它可以从数据库中获取更新的信息,而不是加载中间查询?

编辑以添加代码(即使我认为它与潜在问题无关):

$sql = "SELECT * FROM tool_categories";
if($result = $MySQLi->query($sql)){
    $toolCategories = array();
    while($row = $result->fetch_assoc()){
        $toolCategories[] = $row;
    }
    $result->free();
}

if(isset($_POST['editCategory'])){ // Editing category
    if(!in_array($_POST['categoryName'], $toolCategories)){ // Make sure it doesn't exist already
        $sql = "UPDATE tool_categories SET categoryName='" . $_POST['categoryName'] . "' WHERE categoryID=" . $_POST['categoryID'];
        if($_POST['placement'] != 0){
            if(!in_array($_POST['placement'], array_column($toolCategories, 'categoryID'))){ // Check if it exists
                $sql .= "; UPDATE tool_categories SET categoryID=" . $_POST['placement'];
            }else{
                // Welp, gotta make some changes to categoryID's to make this fit!
                $sql = "UPDATE tool_categories SET categoryID=0 WHERE categoryID=" . intval($_POST['categoryID']) . ";";
                $sql .= "UPDATE tool_categories SET categoryID=categoryID-1 WHERE categoryID >= " . intval($_POST['categoryID']) . ";";
                $sql .= "UPDATE tools SET categoryID=categoryID-1 WHERE categoryID >= " . intval($_POST['categoryID']) . ";";
                $sql .= "ALTER TABLE tool_categories DROP INDEX categoryID;";
                $sql .= "ALTER TABLE tool_categories DROP PRIMARY KEY;";
                $sql .= "UPDATE tool_categories SET categoryID=categoryID+1 WHERE categoryID >= " . intval($_POST['placement']) . ";";
                $sql .= "UPDATE tools SET categoryID=categoryID+1 WHERE categoryID >= " . intval($_POST['placement']) . ";";
                $sql .= "ALTER TABLE tool_categories ADD INDEX categoryID (categoryID);";
                $sql .= "ALTER TABLE tool_categories ADD PRIMARY KEY(categoryID);";
                $sql .= "UPDATE tool_categories SET categoryID=" . intval($_POST['placement']) . ", categoryName='" . $_POST['categoryName'] . "' WHERE categoryID=0";
            }
        }
    }
}


$startQuery = microtime(true);
$numberOfQueries = count(explode(';', $sql));
if(!$MySQLi->multi_query($sql)){
    die(db_error());
    for($i = 2; $i < $numberOfQueries+1; $i++){
        if(!$MySQLi->next_result()){
            die(db_error());
        }
    }
}
$endQuery = microtime(true);
$queryTime = $endQuery - $startQuery;
header("Location: " . $_SERVER['HTTP_REFERER'] . "&queryTime=" . $queryTime . "&queries=" . $numberOfQueries);
4

2 回答 2

1

以下代码引用自@mickmackusa 在上述评论中提供的以下帖子。严格标准:mysqli_multi_query 出现 mysqli_next_result() 错误

if($MySQLi->multi_query($sql)){
    do{} while($MySQLi->more_results() && $MySQLi->next_result());
}
if($error_mess = $MySQLi->error){ die("Error: " . $error_mess); }

此代码设法阻止我的下一页加载,直到所有查询都按预期完成。

于 2017-03-20T14:01:20.877 回答
0

你应该避免mysqli::multi_query。而是一一单独运行您的查询。它将阻止您的页面加载,直到它可以获得更新的信息。

于 2017-03-19T12:16:32.190 回答