0

I have a database table where events are described by users (column "description") and tagged (column "tag"). I want to check that if a certain string of words appears in the "description", some tags are invalid.

Here is what I did so far:

$sql = "SELECT * FROM events WHERE user='{$_SESSION['username']}'";
$qsql = mysql_query($sql) or die ("Error Query [".$sql."]");
while($result = mysql_fetch_array($qsql)){
   $description = mysql_real_escape_string(strtolower($result["description"]));
   $a = "string I want to find";
   $aa = strpos($description, $a);

     if (($aa !== false) AND $result["tag"]!=='1'){
     echo "The string you wrote doesn't allow the tag you used.";
     }
}

The code above works. As you can see, if the given tag is not 1, an error message is echoed.

However, I am having problems when I need to validate the string against TWO OR MORE tags:

if (($aa !== false) AND ($result["tag"]!=='1' OR $result["tag"]!=='2')){
echo "The string you wrote doesn't allow the tag you used.";
}

The error is echoed even if tags 1 or tag 2 are given. I can't understand what is wrong with this.

2
  • 1
    Use AND instead of OR Commented Sep 23, 2016 at 10:22
  • Can you explain why? OR seems correct to me. Commented Sep 23, 2016 at 10:28

1 Answer 1

2

If you use 'OR', it means "Show the error if its not 1 Or its not 2". If ['tag'] is '1', then it is not '2', and if ['tag'] is '2' then it is not '1', so it will always be true. Changing it to 'AND' will solve this.

Your code should be:

if (($aa !== false) AND $result["tag"]!=='1' AND $result["tag"]!=='2'){
  echo "The string you wrote doesn't allow the tag you used.";
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.