简体   繁体   中英

In an elseif statement, how do I stop the next one from being run

I am new to programming and PHP, and am quite discouraged by my inability to get this code to work.

$query_uid2 = mysqli_query($db_conx,"SELECT * FROM table WHERE uid2 IS NOT NULL");
$query_uid3 = mysqli_query($db_conx,"SELECT * FROM table WHERE uid3 IS NOT NULL");

//the update queries

$q2 = mysqli_query($db_conx,"UPDATE table SET uid2='$var' WHERE uid1='$uid1'");
$q3 = mysqli_query($db_conx,"UPDATE table SET uid3='$var' WHERE uid1='$uid1'");

//I used mysqli_num_rows function to create 2 more variables for next step...

if ($q2_num > 0 && $q2 == TRUE) {
    echo "success";
    die();
} elseif ($q3_num > 0 && $q3 == TRUE) {
    echo "success";
    die();
}

My problem is that both if statements carry out, despite the fact that the query_uid is in fact not null .
I am trying to stop the second statement from running if the first statement conditional is true, is this possible?
I hope you can understand my question. I've been trying to figure this out for 2 weeks now, starting to feel hopeless. I can't find anything online or in my book explaining what I'm trying to do.

To explain it again, I don't want the second conditional to run, only the first one assuming it's true . Can someone give me a hint as to how this is possible? I've tried all I can.

If the first if is true , then what's in the elseif will not run. But when you assign the queries' results to those variables, they run before your conditions. The solution is simple, just run the queries inside the condition.

if (condition) {
    mysqli_query(...);
}

Specifically for your situation:

if ($q2_num > 0) {
    mysqli_query($db_conx,"UPDATE table SET uid2='$var' WHERE uid1='$uid1'");
    echo "success";
    die();
} elseif ($q3_num > 0) {
    mysqli_query($db_conx,"UPDATE table SET uid3='$var' WHERE uid1='$uid1'")
    echo "success";
    die();
}

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