0

Why does not my code update?
and echo no!

$sql_updata1 = "UPDATE information_user SET invitation = '1' WHERE information_user.id = '$ID_Invitation'; UPDATE information_user SET valid = '$num_Invitation '+' $number_userInvitation' WHERE information_user.id = '$ID_Invitation'; UPDATE information_user SET valid = '$num_Caller '+' $number_userCaller' WHERE information_user.id = '$id_user_invitation';";

if ((mysqli_query($con, $sql_updata1)){
    echo "ok";
} else {
    echo "no";
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
Reza
  • 23
  • 7
  • 2
    Why are you doing this with multiple queries? You can put multiple assignment in a single `UPDATE`. `SET invitation = 1, valid = '$num_Invitations' + $number_userInvitation, ...` – Barmar Apr 26 '19 at 18:04
  • @Barmar, I've re-opened it, would you post an answer suggesting how it could be done - thanks. – Nigel Ren Apr 26 '19 at 18:05
  • you can use MySQL multi-query to execute multiple statements. – Rakesh Jakhar Apr 26 '19 at 18:07
  • I'm actually in the API When some of the bets are checked and arrived here, I want to add the invitations 5 to invite and invite 3 to invite number 1 that can no longer invite anyone. If you have a better solution, thank you for saying that – Reza Apr 26 '19 at 18:07
  • 1
    Just a bit of advice - if at all possible you should switch from mysqli to PDO https://www.php.net/manual/en/book.pdo.php. It is more secure. – raphael75 Apr 26 '19 at 18:09
  • See also: [PHP PDO MySQL Transaction code structure](//stackoverflow.com/q/8618618) – mario Apr 26 '19 at 18:15
  • 1
    Having that query on one line where you have to scroll back and forth to even begin to see what you're doing is a huge hassle here. – tadman Apr 26 '19 at 18:16

1 Answers1

1

You can do this as a single query. Make the WHERE clause select both the caller and callee. You can assign to multiple columns in the SET clause, and you can use IF or CASE to make the value that you assign dependent on the id of the row you're processing.

$sql_updata1 = "
    UPDATE information_user 
    SET invitation = IF(id = '$ID_Invitation', '1', invitation)
        valid = IF(id = '$ID_Invitation', 
                    $num_Invitation + $number_userInvitation, 
                    $num_Caller + $number_userCaller)
    WHERE id IN ('$ID_Invitation', '$id_user_invitation')";
Barmar
  • 741,623
  • 53
  • 500
  • 612