chicago123 Posted March 9, 2014 Share Posted March 9, 2014 I'm a PHP beginner - I'm not sure what is wrong with this code. The problem: When $qotw is equal to 1 or when it is equal to 2, neither statement is being executed (see code below). I am not getting any errors. What I tried: I tried putting if($qotw = '1') instead of if(qotw == '1') and if(qotw = '2') instead of if(qotw == '2'). In that case the 'cash' increased as it was supposed to. However, when $qotw was equal to 2, the first statement still executed and the second one did not (the 'cash' value was still increasing and the message that should be displayed according to the 2nd IF statement did not show up). The 'cash' value and 'qotw' value should only increase when $qotw is equal to 1.Please help me fix this so that BOTH statements will execute at the appropriate times. Thanks! Here is my code: <?php $con=mysqli_connect("localhost","users","password","users"); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } //statement1 if($qotw = '1') { mysqli_query($con,"UPDATE users SET cash = cash + $scash WHERE username = '". $_SESSION['username'] . "'"); mysqli_query($con,"UPDATE users SET qotw = 1 + qotw WHERE username = '". $_SESSION['username'] . "'"); } //statement2 if($qotw = '2') { echo "You have already attempted this question."; } mysqli_close($con); ?> Quote Link to comment https://forums.phpfreaks.com/topic/286824-not-sure-what-is-wrong-with-these-if-statements/ Share on other sites More sharing options...
Solution .josh Posted March 9, 2014 Solution Share Posted March 9, 2014 first off, where are you actually defining $qotw? I don' tsee anywhere in your code where you are actually defining it... except in your condition, which that's not how it works. = is the assignment operator. == is the equality operator. edit: on a side note, when you change it to e.g. if ($qotw=='1') .. note that php is a loosely typed language, which means '1' will also stand for true, so anything that evaluates as true will make the condition true. IOW you may instead want to use a strict comparison with === instead of == Quote Link to comment https://forums.phpfreaks.com/topic/286824-not-sure-what-is-wrong-with-these-if-statements/#findComment-1471892 Share on other sites More sharing options...
chicago123 Posted March 9, 2014 Author Share Posted March 9, 2014 Thanks for your reply. The problem was that I had defined $qotw further down the page AFTER these statements. I realized that I have to define it BEFORE these statements, and now it is working fine. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/286824-not-sure-what-is-wrong-with-these-if-statements/#findComment-1471926 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.