gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 08:02:20 25 4
gpt4 key购买 nike

我知道这个主题有很多问答,但我面临的是一个相当个别的问题,这就是我提出新问题的原因。

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

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

如果我使用下面的代码,INSERT INTO 只会用值填充 adresseperson 仍然完全是空的。 error.log 中没有错误消息。
我错过了什么?我做错了什么?

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

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

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

我的表结构:

地址

  • adresse_id(INT、PK、NOT NULL、AUTO_INCREMENT)
  • 姓名1
  • 大街

  • person_id(INT,主键,NOT NULL,AUTO_INCREMENT)
  • adresse_id (FK)
  • adrgruppe_id
  • adrstatus_id
  • funktion_id
  • 姓名
  • 姓名
  • 功能
  • 开始
  • 电话
  • 电话2
  • 电话3
  • 电话4
  • 邮箱地址
  • 传真
  • 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

因此,您需要将准备和执行分成 2 个,如下所示。您应该会发现这意味着 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"]
));

关于php - 在 PDO 准备语句中使用 LAST_INSERT_ID 插入多个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51980869/

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