gpt4 book ai didi

mysql - 一个事务中的多个插入 mysqli php5

转载 作者:行者123 更新时间:2023-11-29 02:59:29 24 4
gpt4 key购买 nike

我有 3 个表:用户、学生、学生详细信息

用户的主键是表student (userid) 中的一个字段的外键,学生的主键是表studentdetails (studentid) 中的一个字段的外键。

我需要在一次提交中将数据从一个表单插入所有 3 个表,以下是 SQL 脚本:

    $sql = "

INSERT INTO `tbluser`(`username`, `password`, `roleid`, `datecreated`)
VALUES ('$email','$password', '$role', CURRENT_TIMESTAMP);

SELECT @uid := MAX(`userid`) FROM `tbluser`;

INSERT INTO `tblstudent` (`userid`, `scholarnumber`, `firstname`, `middlename`,
`lastname`, `datecreated`)
VALUES ('@uid', '$scholar_no', '$first_name', '$middle_name', '$last_name',
CURRENT_TIMESTAMP);


SELECT @stuID := MAX(`studentid`) FROM `tblstudent`;

INSERT INTO `tblstudentdetails` (`studentid`,`dob`, `studenttype`, `gender`,
`religion`, `category`, `currentsuburbid`, `currentcityid`, `currentaddress1`,
`currentzipcode`, `currentstateid`, `currentcountryid`,`mobile`,`phone1`,`email1`,
`passportnum`, `permasuburbid`, `permaaddress1`, `dateofjoining`,
`admissionreferenceof`, `datecreated`, `dateupdated`)

VALUES ('@stuid', '$dob' ,'$studenttype' ,'$gender','$religion','$category',
'$currentsuburbid', ' $currentcityid', '$currentaddress1', '$currentzipcode',
'$currentstateid', '$currentcountryid', '$mobile',
'$phone1','$email1','$passportnum','$permanentsuburbid', '$permanentaddress1',
'$doj', ' $admissionreference',current_timestamp, current_timestamp);

";

我无法弄清楚问题所在,上面的脚本在 mysql (phpmyadmin) 中有效,但在 php 中不起作用。我知道,我需要使用 multi_query (??) 我是,但它不会给出任何错误并插入两个表,但不会在第三个表中插入。我觉得这可能与中间的 SELECT 语句有关?在这里,我将不胜感激任何帮助。提前致谢。

最佳答案

看起来您正在尝试从 mysqli 运行多个用分号分隔的 SQL 语句。那是行不通的。您需要分别发布每个不同的声明。

您可以使用 MySQL 的事务(只要您对表使用 InnoDB 或其他一些访问方法,并且不是 MyISAM:MyISAM 不处理事务)。

你会这样做:

$connection->begin_transaction();
/* issue your statements one by one */
$connection->commit();

这将使所有插入内容等同时可见。

但是:您正在尝试使用最新的自动增量 ID 号。你做错了。您需要使用 MySQL 的 LAST_INSERT_ID() 函数代替您的

SELECT @uid := MAX(`userid`) FROM `tbluser`;   /*wrong*/

图案。这是有效的,因为 LAST_INSERT_ID() 提供了第一个插入的 ID 值,因此第二个插入将使用它。即使多个程序向表中插入内容也是安全的,因为 MySQL 为每个程序连接保留一个单独的值。它比您拥有的更快,因为它不必查看表格,并在使用前将值返回给您的程序。

这样做,你会得到你想要的。

/* do the first insert, using an autoincrementing uid column */
INSERT INTO `tbluser`(whatever, whatever, whatever)
VALUES (whatever, whatever, whatever);
/* now LAST_INSERT_ID() contains the value inserted into tbluser.uid */

/* do the second insert, using the id from the first insert into tblstudent.userid */
INSERT INTO `tblstudent` (`userid`, whatever, whatever, whatever)
VALUES (LAST_INSERT_ID(), whatever, whatever, whatever);
/* now LAST_INSERT_ID() contains the value inserted into tblstudend.studentid */

/* use that value to insert into tblstudentdetails.studentid */
INSERT INTO `tblstudentdetails` (`studentid`, whatever, whatever, whatever)
VALUES (LAST_INSERT_ID(), whatever, whatever, whatever);

关于mysql - 一个事务中的多个插入 mysqli php5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25933427/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com