简体   繁体   English

mySQL:2个具有外键关系的表的UPDATE语句

[英]mySQL: UPDATE statement for 2 tables with a foreign key relation

I have created 2 tables with the following structure: 我创建了具有以下结构的2个表:

mitarbeiter
==================
maID (PK, AUTO_INCREMENT, NOT NULL)
maAnrede
maName
maVname
maDurchwahl
maEmail
maMobilfunkNr
maKartenanzahl
maFirma

mobilfunkkarten
==============================
mfkID (PK, AUTO_INCREMENT, NOT NULL)
mfkTarif
mfkStatus
mfkKartennr
mfkPin
mfkSuperpin
maID(FK)

Now I would like the web user to type in values into form fields. 现在,我希望网络用户在表单字段中输入值。 (I will let him edit his/her entries there, which will be saved in the mysql-database. So these entries are NOT new!) After clicking the "Save"-Button, the data will be saved into the corresponding 2 tables. (我让他在那里编辑他/她的条目,这些条目将保存在mysql数据库中。因此这些条目不是新的!)单击“保存”按钮后,数据将保存到相应的2个表中。 My mySQL-Query looks like this (I am using symfony's php templating engine "twig"): 我的mySQL查询看起来像这样(我正在使用symfony的php模板引擎“ twig”):

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

if(isset($_POST["btnSaveMfk"]))
{
$stmt = DatabaseLink::getInstance()->prepare("UPDATE mitarbeiter SET 
maAnrede = :maAnrede, 
maVname = :maVname, 
maName = :maName, 
maMobilfunkNr = :maMobilfunkNr,
maKartenanzahl = :maKartenanzahl,
maEmail = :maEmail,
maFirma = :maFirma
WHERE mitarbeiter.maID = :maid;
UPDATE mobilfunkkarten SET mfkTarif = :mfkTarif,
mfkStatus = :mfkStatus, 
mfkPin = :mfkPin,
mfkSuperpin = :mfkSuperpin");

$status = $stmt->execute(array(

":maid" => $_POST["txtMaId"],
":maAnrede" => $_POST["txtAnrede"],
..................,
..................,
..................
)); 
header("Location: Mobilfunkdaten"); //back to the PHP table that shows all entries
}

I believe that this won't work because the 2 tables are related with a foreign key and if I update both tables without this relation, the statement will result in an error or it will overwrite something unrelated. 我认为这将不起作用,因为2个表都与外键相关,如果我在没有此关系的情况下更新两个表,则该语句将导致错误或将覆盖不相关的内容。 Am I right with this assumption? 这个假设对吗?

Any solutions on how to solve this? 有什么解决办法吗? I just can't think of any way on how to make sure that anything the user types into the form fields will be saved as 1 dataset into these 2 tables, ie the UPDATED data in the child table 'mobilfunkkarten' will be related to the Primary Key Number in the parent table 'mitarbeiter'. 我只是想不出办法确保将用户在表单字段中键入的任何内容保存为这2个表中的1个数据集,即子表“ mobilfunkkarten”中的UPDATED数据将与父表“ mitarbeiter”中的主键号。

mitarbeiter = workers mobilfunkkarten = mobile phone cards (SIM cards) mitarbeiter =工人mobilfunkkarten =手机卡(SIM卡)

With update statements, the auto_increment value doesn't change. 使用update语句时, auto_increment值不会更改。 And, as I can see from your query, you're not updating the maID value, so it gives no reason for the MySQL parser to throw an error. 而且,从查询中可以看到,您没有更新maID值,因此它没有理由让MySQL解析器抛出错误。 Your query is correct, as far as I can see. 据我所知,您的查询是正确的。

Just one small thing. 只是一件小事。 Define the keys of the associative array without the : symbol. 定义不带:符号的关联数组的键。 You use this symbol to indicate that this place is reserved for the value stored in the variable by the following name. 您可以使用该符号来表示此位置是为以下名称存储在变量中的值保留的。 For example, using 例如,使用

$stmt = DatabaseLink::getInstance()->prepare("update table_name set name=:name where id=:id");

$status = $stmt->execute(array("name" => "test", "id" => 2));

indicates to the parser that the name corresponding to ID 2 has to be updated to test . 向解析器指示必须更新ID 2对应的名称以进行test

But, you are already using the : along with the name of the key. 但是,您已经在使用:和键名了。 So, in your example, your query looks for the value in a key called maAnrede in your script, but the key that you have defined is :maAnrede , and hence, the query doesn't work as expected. 因此,在您的示例中,查询在脚本中名为maAnrede的键中查找值,但是您定义的键为:maAnrede ,因此查询无法正常工作。

Try this change. 试试这个变化。 It'll surely work. 它肯定会工作。

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

if(isset($_POST["btnSaveMfk"]))
{
$stmt = DatabaseLink::getInstance()->prepare("UPDATE mitarbeiter SET 
maAnrede = :maAnrede, 
maVname = :maVname, 
maName = :maName, 
maMobilfunkNr = :maMobilfunkNr,
maKartenanzahl = :maKartenanzahl,
maEmail = :maEmail,
maFirma = :maFirma
WHERE mitarbeiter.maID = :maid;
UPDATE mobilfunkkarten SET mfkTarif = :mfkTarif,
mfkStatus = :mfkStatus, 
mfkPin = :mfkPin,
mfkSuperpin = :mfkSuperpin");

$status = $stmt->execute(array(

"maid" => $_POST["txtMaId"],
"maAnrede" => $_POST["txtAnrede"],
..................,
..................,
..................
)); 
header("Location: Mobilfunkdaten"); //back to the PHP table that shows all entries
}

This situation happened with me as well, and this is the solution that worked for me! 这种情况也发生在我身上,这是对我有用的解决方案!

I believe I fixed it. 我相信我已经解决了。 You need to add this line in the second SQL statement: 您需要在第二条SQL语句中添加以下行:

WHERE mobilfunkkarten.maID = :maid");

See below where I included it. 请参阅下面我包含它的位置。 Fixed the issue for me but I am not entirely sure how safe this one is...any criticism on this approach? 为我解决了这个问题,但我不完全确定这个方法有多安全...对此方法是否有任何批评? Other suggestions? 还有其他建议吗?

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

    if(isset($_POST["btnSaveMfk"]))
    {
    $stmt = DatabaseLink::getInstance()->prepare("UPDATE mitarbeiter SET 
    maAnrede = :maAnrede, 
    maVname = :maVname, 
    maName = :maName, 
    maMobilfunkNr = :maMobilfunkNr,
    maKartenanzahl = :maKartenanzahl,
    maEmail = :maEmail,
    maFirma = :maFirma
    WHERE mitarbeiter.maID = :maid;
    UPDATE mobilfunkkarten SET mfkTarif = :mfkTarif,
    mfkStatus = :mfkStatus, 
    mfkPin = :mfkPin,
    mfkSuperpin = :mfkSuperpin
    WHERE mobilfunkkarten.maID = :maid");

    $status = $stmt->execute(array(

    "maid" => $_POST["txtMaId"],
    "maAnrede" => $_POST["txtAnrede"],
    ..................,
    ..................,
    ..................
    )); 
    header("Location: Mobilfunkdaten"); //back to the PHP table that shows all entries
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM