如何使用$ qryUpdate在一条语句中更新两个表

时间:2019-04-25 14:34:48

标签: php mysql

在我的editclient.php(控制器)中,我想使用$qryUpdate同时更新数据库中的2个表,但是我不知道该怎么做。

我已经看过这个How to update two tables in one statement in SQL Server 2005?,但我无法使它正常工作。

if (strtolower($_SESSION['role'])=='admin')
    {
        //$conn=bekend door include db.php in header
        $qryCreate="INSERT INTO USER
                    (username, password, email, role_id)
                    VALUES('$nwUsername', '$nwPassword', '$nwEmail', '$nwRole')";
        //controleren of gebruiker al bestaat...
        if (getExistUsername($conn, $nwUsername))
        {
            echo "Gebruiker $nwUsername bestaat al, gebruiker wordt niet aangemaakt...<br>";
            header('refresh: 5; url=index.blade.php');
            exit;
        }
//query uitvoeren met error afhandeling
        if (mysqli_query($conn, $qryCreate))
        {

            echo "Gebruiker $nwUsername is aangemaakt!";
            header('refresh: 5; url=edit.blade.php?name='.$nwUsername.'&action=edit');
        }
        else
        {
            echo "Gebruiker $nwUsername is NIET aangemaakt!<br>
                      Geef deze foutmelding door aan uw beheerder: ".mysqli_error($conn);
            header('refresh: 5; url=index.blade.php');
        }

我想与另一个查询同时执行下一个查询

UPDATE client
                SET name='$name', street='$street', number='$number', postalcode='$postalcode', location='$location', phonenumber='$phonenumber'
                WHERE user_id='$user_id';"

此刻我有这个:

                $qryUpdate =<<<SQL
            BEGIN;
              UPDATE user
              SET username='$username', password='$password'
                WHERE username='$nwUsername';
              UPDATE client 
              SET name='$name', street='$street', number='$number', postalcode='$postalcode', location='$location', phonenumber='$phonenumber'
              WHERE user_id=' .$user_id. ';
              COMMIT;
            SQL;

             var_dump($nwUsername);
            //hieronder worden de gegevens opgeslagen....
            mysqli_query($conn, $qryUpdate);
            if (true) {
                echo "Gebruiker $nwUsername is aangepast naar $username!<br> 
                      Client $name is aangepast.<br> $qryUpdate";
            } //    header('refresh: 2; url=index.blade.php');

            else {
                echo "Wijzigingen zijn NIET doorgevoerd!<br>

        foutmelding: " . mysqli_error($conn) . "<br>Query:" . $qryUpdate;
            }
            }```

But the problem now is that i don't know how to close the query and that the echo shows the query with the correct values but, nothing changes in my database.

1 个答案:

答案 0 :(得分:0)

您可以使用PDO进行此操作。我在这里已将您的查询截短,以说明代码的外观。

$sql = <<<SQL
   START TRANSACTION;
   INSERT INTO user (...) VALUES (...);
   UPDATE client SET ... WHERE ...;
   COMMIT;
SQL;

$dsn = "mysql:dbname=dbname;host=127.0.0.1";
$username = "mysqluser";
$password = "mysqluser-secret-password";

$pdo = new PDO($dsn, $username, $password); // update for your server
$statement = $pdo->prepare($sql);
//
// bind the parameters to avoid SQL injection 
//
$statement->execute();

详细了解transactions in the MySQL documentation。另外,请确保read about PDO可以与多个关系数据库引擎一起使用,而不仅仅是MySQL。