在php和mysql中使用sql事务

在php和mysql中使用sql事务,php,mysql,mysqli,Php,Mysql,Mysqli,首先我应该指出,我对php和MySql的世界还很陌生!我忙于学习,但我可能没有用专业的方式做事 背景 我创建了一个php表单,在提交时将数据插入两个表:post_header和post_details 目前,我正在以以下格式发布两份insert声明: $sql="INSERT INTO post_header (member_id, ....+more fields if ($insert_stmt = $mysqli->prepare($sql)) { $insert_stmt->

首先我应该指出,我对php和MySql的世界还很陌生!我忙于学习,但我可能没有用专业的方式做事

背景 我创建了一个php表单,在提交时将数据插入两个表:post_header和post_details

目前,我正在以以下格式发布两份insert声明:

$sql="INSERT INTO post_header (member_id, ....+more fields
if ($insert_stmt = $mysqli->prepare($sql)) {
 $insert_stmt->bind_param('i', $_POST[member_id]);

if ($insert_stmt->execute()){
 //it worked
 $newpostId=$mysqli->insert_id;
 //now use the header id (primary key from post_header, which auto-increments) to add a record into the detail table

 $sql="INSERT INTO post_detail (header_id, ....+more fields
 if ($insert_stmt = $mysqli->prepare($sql)) {
  $insert_stmt->bind_param('i', $newpostId);
我应该指出,post_header和post_detail有1对1的关系,并且通过header_id链接

我的问题是PHP和MySQL是否有办法让我在一个sql事务中向两个表提交插入


如果有人能给我指出正确的方向,我将不胜感激。谢谢你

Mysqli目前还没有这个功能,只能使用multi_query->不同寻常,这是专业的方式@不幸的是,您既不了解此函数的用途,也不了解事务?我只是不明白为什么每个查询只绑定一个参数。