在一个sql查询中更新多个表

l7wslrjt  于 2021-06-23  发布在  Mysql
关注(0)|答案(1)|浏览(314)

我做了一些研究,发现通过一个查询是不可能的,但是可以通过一个事务来实现。但是我不能让sql查询正常工作。如果我一个接一个地使用这些语句,它们都很好用。
我该怎么办?
目标是用一个表单中的信息更新两个表。

$query = "

START TRANSACTION;

UPDATE projects

SET

projects.project_name = '".$mysqli->real_escape_string($_POST['project_name'])."',

projects.project_group = '".$mysqli->real_escape_string($_POST['project_group'])."',

projects.project_notes = '".$mysqli->real_escape_string($_POST['project_notes'])."',

projects.project_created = '".$mysqli->real_escape_string($_POST['project_created'])."',

projects.project_start = '".$mysqli->real_escape_string($_POST['project_start'])."',

projects.project_delivery  = '".$mysqli->real_escape_string($_POST['project_delivery'])."',

projects.project_orderdetails = '".$mysqli->real_escape_string($_POST['project_orderdetails'])."',

projects.project_owner = '".$mysqli->real_escape_string($_POST['project_owner'])."'

where projects.project_id = '".$mysqli->real_escape_string($_REQUEST['id'])."';

INSERT INTO hours

hours.userhours_id = '".$mysqli->real_escape_string($_POST['project_owner'])."',

hours.projecthours_id = '".$mysqli->real_escape_string($_REQUEST['id'])."',

hours.user_hours = '".$mysqli->real_escape_string($_POST['user_hours'])."'

where projects.project_id = '".$mysqli->real_escape_string($_REQUEST['id'])."';

COMMIT;

";
1szpjjfi

1szpjjfi1#

Mysqli 例如,具有处理事务的方法http://php.net/manual/en/mysqli.begin-transaction.php.
代码可以是:

$mysqli->begin_tansaction();
$mysqli->query('query1');
$mysqli->query('query2');
$mysqli->commit();

相关问题