在PDO准备好的语句中使用LAST_INSERT_ID插入多个表

时间:2018-08-23 07:55:54

标签: php mysql sql mariadb

我知道这个主题有很多问答,但是我面临着一个非常个别的问题,这就是为什么我提出一个新问题。

我正在将PHP7mariadb 10.xPHP模板引擎Twig 2.x一起使用。 我的目标是将数据插入2个相关表(地址,人)。

对于不熟悉Twig的人来说,它基本上是一个使用模板来帮助分离htmlphp代码的工具。因此,我的.html文件称为.twig

如果我使用以下代码,则INSERT INTO仅用值填充adresse,而person仍然完全为空。 error.log中没有错误消息。
我想念什么?我做错了什么?

我还添加了BEGIN; --preparestmt--; COMMIT;,以确保该语句属于一起并且应被视为一项事务。

我输入了SELECT last_insert_id();,它给了我结果0。为什么?

adresse.adresse_id到底增加了吗?为什么last_insert_id()不采用增量值,而是采用0

我的表结构

地址

  • adresse_id(INT,PK,非空,AUTO_INCREMENT)
  • name1
  • 大街
  • plz
  • ort

  • person_id(INT,主键,非空,AUTO_INCREMENT)
  • adresse_id(FK)
  • adrgruppe_id
  • adrstatus_id
  • funktion_id
  • 名称
  • vorname
  • 功能
  • abteilung
  • telefon
  • telefon2
  • telefon3
  • telefon4
  • mailadresse
  • 传真
  • sb_kuerzel

neuerBauleiter.twig

{% include "headerKundendienst.twig" %}
<body class="maincontent">
    <div class="main-content">
    <h2>Bauleiter hinzufügen</h2>
        <form method="post" action="neuerBauleiter">

                <p>
                    <label class="editSupplierLabel">Nachname</label>
                    <input class="editSupplier" type="text" name="txtBlName" required>
                </p>
                <p>
                    <label class="editSupplierLabel">Vorname</label>
                    <input class="editSupplier" type="text" name="txtBlVorname" required>
                </p>
                <p>
                    <label class="editSupplierLabel">Anschrift</label>
                    <input class="editSupplier" type="text" name="txtBlAnschrift" required>
                </p>
                <p>
                    <label class="editSupplierLabel">PLZ</label>
                    <input class="editSupplier" type="text" name="txtBlPlz" required>
                </p>
                <p>
                    <label class="editSupplierLabel">Ort</label>
                    <input class="editSupplier" type="text" name="txtBlOrt" required>
                </p>
                <p>
                    <label class="editSupplierLabel">Telefon</label>
                    <input class="editSupplier" type="tel" name="txtBlTel" required>
                </p>
                <p>
                    <label class="editSupplierLabel">Telefon2</label>
                    <input class="editSupplier" type="tel" name="txtBlTel2">
                </p>
                <p>
                    <label class="editSupplierLabel">Telefon3</label>
                    <input class="editSupplier" type="tel" name="txtBlTel3">
                </p>
                <p>
                    <label class="editSupplierLabel">E-Mail</label>
                    <input class="editSupplier" type="email" name="txtBlMail" required>
                </p>
                <p>
                    <label class="editSupplierLabel">Fax</label>
                    <input class="editSupplier" type="tel" name="txtBlFax">
                </p>
                <p>
                    <label class="editSupplierLabel">Sachbearbeiter</label>
                    <input class="editSupplier" type="text" name="txtBlSb" value="{{ loggedUser }}" readonly required>
                </p>
                <p>
                    <label class="editSupplierLabel">Adressgruppe-ID</label>
                    <input class="editSupplier" type="text" name="BlAdrgruppeid" value="1272" readonly required>
                </p>
                <p>
                    <label class="editSupplierLabel">Adressstatus-ID</label>
                    <input class="editSupplier" type="text" name="BlAdrstatusid" value="62" readonly required>
                </p>
                <p>
                    <label class="editSupplierLabel">Funktion-ID</label>
                    <input class="editSupplier" type="text" name="BlFunktionid" value="2" readonly required>
                </p>
                <p>
                    <label class="editSupplierLabel">Funktion</label>
                    <input class="editSupplier" type="text" name="BlFunktion" value="Bauleiter" readonly required>
                </p>
                <p>
                    <label class="editSupplierLabel">Abteilung</label>
                    <input class="editSupplier" type="text" name="BlAbteilung" value="Bauleitung" readonly required>
                </p>
                <p>
                    <input class="button_save" type="submit" name="btnSaveBl" value="Speichern">
                    <a href="listeBauleiter" class="button_delete" style="margin-bottom: 40px; margin-left:150px;">Zur&uuml;ck</a>
                </p>
        </form>
    </div>
    </div>
    </body>
{% include "footer.twig" %}

neuerBauleiter.php

<?php

require_once 'vendor/autoload.php'; // keine pfad anpassung mehr nötig, immer so lassen
require_once "utils/Database.class.php"; // include database configuration

/* Daten für neuen Bauleiter speichern */
if (isset($_POST["btnSaveBl"])) 
{

DatabaseLink::getInstance()->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

$stmt = DatabaseLink::getInstance()->prepare("INSERT INTO adresse(name1, strasse, plz, ort) 
VALUES (:blNachname, :blAnschrift, :blPlz, :blOrt);
INSERT INTO intranet.person(adresse_id, name, vorname, 
telefon, telefon2, telefon4, mailadresse, fax, adrgruppe_id, adrstatus_id, funktion_id, funktion, abteilung, sb_kuerzel)
VALUES (last_insert_id(), :blNachname, :blVorname, 
:blTel, :blTel2, :blTel4, :blMail, :blFax, :bladrgruppeid, :bladrstatusid, :blfunktionid, :blfunktion, 
:blabteilung, :blSachbearbeiter;)");

$result = $stmt->execute(array(
        ":blNachname" => $_POST["txtBlName"],
        ":blVorname" => $_POST["txtBlVorname"],
        ":blAnschrift" => $_POST["txtBlAnschrift"],
        ":blPlz" => $_POST["txtBlPlz"],
    ":blOrt" => $_POST["txtBlOrt"],
    ":blTel" => $_POST["txtBlTel"],
    ":blTel2" => $_POST["txtBlTel2"],
    ":blTel4" => $_POST["txtBlTel3"],
    ":blMail" => $_POST["txtBlMail"],
    ":blFax" => $_POST["txtBlFax"],
    ":blSachbearbeiter" => $_POST["txtBlSb"],
    ":bladrgruppeid" => $_POST["BlAdrgruppeid"], 
    ":bladrstatusid" => $_POST["BlAdrstatusid"],
    ":blfunktionid" => $_POST["BlFunktionid"], 
    ":blfunktion" => $_POST["BlFunktion"],
    ":blabteilung" => $_POST["BlAbteilung"] 
));



    header("Location: listeBauleiter");
}

/* Twig initialisieren */
$loader = new Twig_Loader_Filesystem('template/'); // keine pfad anpassung mehr nötig, immer so lassen
$twig = new Twig_Environment($loader, array(
    "debug" => "true",
));
include "utils/injector.php";

$twig->addExtension(new Twig_Extension_Debug());
//templatewerte
$templateName = "neuerBauleiter.twig";

$data = array(

);

//display
echo $twig->render($templateName, $data);

1 个答案:

答案 0 :(得分:4)

您不能在准备好的查询中放置多个语句。来自manual

  

准备好的语句的SQL语法不支持多语句

因此,您需要将准备工作和执行工作分成以下两个部分。您应该会发现,这意味着LAST_INSERT_ID()开始按预期工作。

$stmt = DatabaseLink::getInstance()->prepare("INSERT INTO adresse(name1, strasse, plz, ort) 
VALUES (:blNachname, :blAnschrift, :blPlz, :blOrt)");
$stmt2 = DatabaseLink::getInstance()->prepare("INSERT INTO intranet.person(adresse_id, name, vorname, 
telefon, telefon2, telefon4, mailadresse, fax, adrgruppe_id, adrstatus_id, funktion_id, funktion, abteilung, sb_kuerzel)
VALUES (last_insert_id(), :blNachname, :blVorname, :blTel, :blTel2, :blTel4, :blMail, :blFax, :bladrgruppeid, :bladrstatusid, :blfunktionid, :blfunktion, 
:blabteilung, :blSachbearbeiter;)");

$result = $stmt->execute(array(
        ":blNachname" => $_POST["txtBlName"],
        ":blVorname" => $_POST["txtBlVorname"],
        ":blAnschrift" => $_POST["txtBlAnschrift"],
        ":blPlz" => $_POST["txtBlPlz"],
        ":blOrt" => $_POST["txtBlOrt"]));

$result2 = $stmt2->execute(array(
    ":blTel" => $_POST["txtBlTel"],
    ":blTel2" => $_POST["txtBlTel2"],
    ":blTel4" => $_POST["txtBlTel3"],
    ":blMail" => $_POST["txtBlMail"],
    ":blFax" => $_POST["txtBlFax"],
    ":blSachbearbeiter" => $_POST["txtBlSb"],
    ":bladrgruppeid" => $_POST["BlAdrgruppeid"], 
    ":bladrstatusid" => $_POST["BlAdrstatusid"],
    ":blfunktionid" => $_POST["BlFunktionid"], 
    ":blfunktion" => $_POST["BlFunktion"],
    ":blabteilung" => $_POST["BlAbteilung"] 
));