2

user、student、studentdetails の 3 つのテーブルがあります。

ユーザーの主キーはテーブル Student (userid) のフィールドの外部キーであり、学生の主キーはテーブル studentdetails (studentid) のフィールドの外部キーです。

1 回の送信で 1 つのフォームから 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 (??) を使用する必要がありますが、エラーは発生せず、2 つのテーブルに挿入されますが、3 番目のテーブルには挿入されません。間の SELECT ステートメントと関係があるのではないかと思いますか? ここで知恵を絞って、助けていただければ幸いです。事前に感謝します。

4

1 に答える 1

1

mysqli からセミコロンで区切られた複数の SQL ステートメントを実行しようとしているようです。それはうまくいきません。個別のステートメントを個別に発行する必要があります。

MySQL のトランザクションを使用できます (MyISAM ではなく、テーブルに InnoDB またはその他のアクセス方法を使用している場合: MyISAM はトランザクションを処理しません)。

次のようにします。

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

これにより、すべての挿入物などが同時に表示されます。

しかし: あなたは最新の自動インクリメント ID 番号を利用しようとしています。あなたはこれを間違っています。LAST_INSERT_ID()の代わりに MySQL の関数を使用する必要があります。

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

パターン。これLAST_INSERT_ID()は、最初の挿入から ID の値を提供するため機能するため、2 番目の挿入でそれが使用されます。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);
于 2014-09-19T12:24:05.417 に答える