简体   繁体   中英

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

I have created 2 tables with the following structure:

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. My mySQL-Query looks like this (I am using symfony's php templating engine "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. 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'.

mitarbeiter = workers mobilfunkkarten = mobile phone cards (SIM cards)

With update statements, the auto_increment value doesn't change. 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. 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 .

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.

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:

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
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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