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