是否有可能有两个mysqli_queries像这样?:
mysqli_query($dblink, "INSERT INTO images (project_id, user_id, image_name, date_created, link_to_file, link_to_thumbnail, given_name) VALUES ('$project_id', '$user_id', '$image_name', '$date_created', '$link_to_file', '$thumbnail', '$ImageName')");
mysqli_query($dblink, "INSERT INTO images_history (project_id, user_id, image_name, date_created, link_to_file, link_to_thumbnail, given_name, day, month, year) VALUES ('$project_id', '$user_id', '$image_name', '$date_created', '$link_to_file', '$thumbnail', '$ImageName', '$day', '$month', '$year')");
Run Code Online (Sandbox Code Playgroud)
基本上我想更新数据库中的两个表.有一个更好的方法吗?
dre*_*010 22
可以使用mysqli_multi_query().
例:
<?php
$mysqli = new mysqli($host, $user, $password, $database);
// create string of queries separated by ;
$query = "INSERT INTO images (project_id, user_id, image_name, date_created, link_to_file, link_to_thumbnail, given_name) VALUES ('$project_id', '$user_id', '$image_name', '$date_created', '$link_to_file', '$thumbnail', '$ImageName');";
$query .= "INSERT INTO images_history (project_id, user_id, image_name, date_created, link_to_file, link_to_thumbnail, given_name, day, month, year) VALUES ('$project_id', '$user_id', '$image_name', '$date_created', '$link_to_file', '$thumbnail', '$ImageName', '$day', '$month', '$year');";
// execute query - $result is false if the first query failed
$result = mysqli_multi_query($mysqli, $query);
if ($result) {
do {
// grab the result of the next query
if (($result = mysqli_store_result($mysqli)) === false && mysqli_error($mysqli) != '') {
echo "Query failed: " . mysqli_error($mysqli);
}
} while (mysqli_more_results($mysqli) && mysqli_next_result($mysqli)); // while there are more results
} else {
echo "First query failed..." . mysqli_error($mysqli);
}
Run Code Online (Sandbox Code Playgroud)
关键是如果要在一次调用中执行多个查询,则必须使用mysqli_multi_query.出于安全原因,mysqli_query不会执行多个查询以防止SQL注入.
还要记住的行为mysqli_store_result.它返回FALSE如果查询没有结果集(这INSERT查询不),所以你还必须检查mysqli_error地看到,它返回一个空字符串意味着INSERT成功.
请参阅:
mysqli_multi_query
mysqli_more_results
mysqli_next_result
mysqli_store_result
You*_*nse 15
Stack Overflow 上的一些答案是如此自相矛盾,以至于令人兴奋。
关键是如果您想在一次调用中执行多个查询,则必须使用 mysqli_multi_query。出于安全原因,mysqli_query 不会执行多个查询以防止 SQL 注入。
它基本上是说,“关键是你必须使用没有保险装置的枪支,因为普通武器不会让你用脚射击自己。所以这里是分解它的方法,现在你可以使自己瘫痪一枪!”
尽管 OP 没有询问如何在单个调用中运行两个查询,尽管引用了在单个调用中运行多个查询的能力本质上是危险的明确警告,但答案漫不经心地提供了规避此限制的方法。
最糟糕的是,所有这些危险而繁琐的混乱都是徒劳的。仅仅是因为在一次调用中运行多个查询没有一个单一的理由。一项一项地运行查询是数据库 API 的使用方式。
基本上我想更新我的数据库中的两个表。有一个更好的方法吗?
$stmt = $dblink->prepare("INSERT INTO images
(project_id, user_id, image_name, date_created, link_to_file, link_to_thumbnail, given_name)
VALUES (?,?,?,?,?,?,?)");
$stmt->bind_param("ssssss", $project_id, $user_id, $image_name, $date_created, $link_to_file, $thumbnail, $ImageName);
$stmt->execute();
$stmt = $dblink->prepare("INSERT INTO images_history
(project_id, user_id, image_name, date_created, link_to_file, link_to_thumbnail, given_name, day, month, year)
VALUES (?,?,?,?,?,?,?,?,?,?)");
$stmt->bind_param("ssssssssss", $project_id, $user_id, $image_name, $date_created, $link_to_file, $thumbnail, $ImageName, $day, $month, $year);
$stmt->execute();
Run Code Online (Sandbox Code Playgroud)
它不仅更干净,而且100% 不受 SQL 注入的影响。
如果您的查询之一失败,只需向 mysqli 询问错误消息,然后修复错误。