doujie4787 2017-03-19 12:09
浏览 30
已采纳

PHP页面加载中查询(MySQL)[重复]

This question already has an answer here:

I've got a PHP page with an HTML form on it for the purpose of changing some information in the MySQL database. The form submits to another PHP page that makes about 7-10 queries based on the information received from the form. It's very important that these queries be in a specific order. I'm using mysqli::multi_query to carry out these queries. At the end after I carry out my query I use header("Location: " . $_SERVER['HTTP_REFERER']); to return the user to the page with the form. My problem arises when the user is returned to this previous page. The page loads at a point in time that appears mid-query, not with the finished product. If I then hit refresh it loads with the updated information. How can I prevent my page from loading until it can obtain the updated information from the database instead of loading mid-query?

Edit to add code (even though I don't think it's relevant to the underlying question):

$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);
</div>
  • 写回答

2条回答 默认 最新

  • doudan1123 2017-03-20 14:01
    关注

    The below code is referenced from the following post provided by @mickmackusa in an above comment. Strict Standards: mysqli_next_result() error with mysqli_multi_query

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

    This code managed to prevent my next page from loading until all queries were completed as intended.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度