From my editclient.php
(controller) I want to update 2 tables in my database at the same time with $qryUpdate
, but I have no idea how to do that.
I have looked at this How to update two tables in one statement in SQL Server 2005? but I couldn't get that to work.
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');
}
I want to execute the next query at the same time as the other one
UPDATE client
SET name='$name', street='$street', number='$number', postalcode='$postalcode', location='$location', phonenumber='$phonenumber'
WHERE user_id='$user_id';"
At this moment i have this:
$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.