Jump to content

Not sure what is wrong with these IF statements


Go to solution Solved by .josh,

Recommended Posts

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);
?>
  • Solution

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 ==

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.