同时更新信息

时间:2019-04-26 17:59:32

标签: php mysql

为什么我的代码不更新?
并回显否!

$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";
}

1 个答案:

答案 0 :(得分:1)

您可以将其作为单个查询来执行。使WHERE子句同时选择呼叫者和被呼叫者。您可以在SET子句中分配多个列,并且可以使用IFCASE使分配的值取决于行的id重新处理。

$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')";