我的一个表单提交需要执行数千个insert语句。在这些插入之后我想将页面重定向到另一个php页面,在那里我想使用我们刚插入的表。
$query = "INSERT INTO ....;INSERT INTO ....;INSERT INTO ....;INSERT INTO ....;INSERT INTO ....;INSERT INTO ....;INSERT INTO ....;INSERT INTO ....;ec";
$execute = mysqli_multi_query($query);
header('location:http://www.page2.php');
但是现在它重定向到page2.php而没有完成完整的查询。所有都插入但我想在所有插入语句完成后才重定向。
答案 0 :(得分:0)
这可能有帮助。只需为每个单独的事务创建一个连接并运行查询(Insert)。如果任何查询失败,则不会执行其余事务。只有成功运行了所有查询,您的网页才会重定向,或者会给出错误信息。
<?php
$conn = //establish connection;
$check=1;
$query=array("INSERT INTO ....","INSERT INTO ....","INSERT INTO ....","INSERT INTO ....","INSERT INTO ....");
foreach($query as $q )
{
if$conn->query($q) === TRUE)
{continue;}
else{$check = 0; break;}
}
if($check==1)
{header('location:http://www.page2.php');}
else{die("There Has Been An Error");}
?>
&#13;
答案 1 :(得分:0)
我认为这个技巧会起作用。
$query = "INSERT INTO ....;INSERT INTO ....;INSERT INTO ....;INSERT INTO ....;INSERT INTO ....;INSERT INTO ....;INSERT INTO ....;INSERT INTO ....;ec";
$execute = mysqli_multi_query($query);
if ($execute ==1 ) {
header('Location: http://www.page2.com/');
}
答案 2 :(得分:-2)
您必须使用mysqli_multi_query();如果您同时有多个查询要执行,那是最好的选择。
您的解决方案应该非常简单,请尝试以下操作:
<?php
// your conneciton strings
$servername = "Hostname";
$username = "Database User";
$password = "Database Password";
$database = "Your Database Name";
$dbc = new mysqli($servername, $username, $password, $database);
if ($dbc->connect_error) {
die("Connection failed: " . $dbc->connect_error);
}
// build your query whatever way you are comfortable
$query = "INSERT INTO ....";
$query .= "INSERT INTO ....";
$query .= "INSERT INTO ....";
$query .= "INSERT INTO ....";
// execute your queries
if (mysqli_multi_query($dbc, $query)){
header('location:http://www.page2.php');
exit;
}
?>