2 回答

TA貢獻(xiàn)1856條經(jīng)驗(yàn) 獲得超17個(gè)贊
對(duì)的,這是可能的。您可以執(zhí)行現(xiàn)在正在執(zhí)行的操作,但需要execute()在準(zhǔn)備每個(gè)查詢后調(diào)用方法。除此之外,將其包裝在事務(wù)中也是一個(gè)好主意。事務(wù)可幫助您確保所有操作成功或全部失敗。如果其中一個(gè)失敗,其他的就不會(huì)被執(zhí)行。
您的固定代碼應(yīng)如下所示:
<?php
$var1 = $_POST['var1'];
$var2 = $_POST['var2'];
$var3 = $_POST['var3'];
// Database connection
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); // switches error reporting on
$conn = new mysqli('localhost','user','password','database');
$conn->set_charset('utf8mb4'); // always set the charset
// Start transaction
$conn->begin_transaction();
$stmt = $conn->prepare("insert into table1(var1, var2, var3) values(?, ?, ?)");
$stmt->bind_param("sss", $var1, $var2, $var3);
$stmt->execute();
$stmt = $conn->prepare("insert into table2(var1) values(?)");
$stmt->bind_param("s", $var1);
$stmt->execute();
$stmt = $conn->prepare("insert into table3(var2, var3) values(?, ?)");
$stmt->bind_param("ss", $var2, $var3);
$stmt->execute();
// End transaction
$conn->commit();

TA貢獻(xiàn)1777條經(jīng)驗(yàn) 獲得超3個(gè)贊
嘗試調(diào)用 $stmt->execute();?每次調(diào)用 $stmt->bind_param() 后;
?PDO 支持多個(gè)查詢(PDO_MYSQL、PDO_MYSQLND)
- 2 回答
- 0 關(guān)注
- 171 瀏覽
添加回答
舉報(bào)