微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

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

对于那些不熟悉Twig的人来说,它基本上是一个工具,可以通过使用模板来分离html和PHP代码.因此,我的.html文件称为.twig.

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

我还添加了BEGIN; –preparestmt–;承诺;确保该语句属于同一语句,应视为一个事务.

我输入SELECT last_insert_id();结果为0.为什么?

adresse.adresse_id毕竟增加了吗?为什么last_insert_id()不取增量值而是0?

我的表结构:

地址

> adresse_id(INT,PK,NOT NULL,AUTO_INCREMENT)
>名称1
>大街
> plz
>奥尔

> person_id(INT,PRIMARY KEY,NOT NULL,AUTO_INCREMENT)
> adresse_id(FK)
> adrgruppe_id
> adrstatus_id
> funktion_id
>名称
> vorname
>功能
> abteilung
>电话
> 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);

解决方法:

您不能在准备好的查询中放置多个语句.从manual开始:

sql Syntax for prepared statements does not support multi-statements

因此,您需要将您的准备工作分成两个执行部分,如下所示.您应该发现这意味着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"] 
));

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐