Jump to content


Photo

Multiple "WHERE" statement in mysql_query() not working.


  • Please log in to reply
7 replies to this topic

#1 zeroblitzt

zeroblitzt
  • Members
  • Pip
  • Newbie
  • 9 posts

Posted 05 August 2006 - 10:46 PM

Hey all, I have this block of code used to mark inappropriate messages on my message board. Thing is, I need to check if a user has already marked a message once, so I do that with this line:

$checkusrx = mysql_query("SELECT * FROM reports WHERE marker = ".$u['userid']." AND msgid = ".$msgid."");


Unfortunately this does not work. I always either get either nothing (the page stays exactly the same and doesn't seem to update, even though it should be) or the error message I crafted, that says "You may not mark a message more than once". Thing is, the marked message  queue is currently empty so there is no way I could have marked something already.

Here is the code below. The problem (I think) is in between lines 4 and 10. Any help would really be appreciated, I just spent an hour trying different ways to do this but none have worked so far.


if ($action == "r")
{
$msgid = $_GET['msg'];
$checkusrx = mysql_query("SELECT * FROM reports WHERE marker = ".$u['userid']." AND msgid = ".$msgid."");

if ($checkusrx == FALSE)
{
$checkmsgx = mysql_query("SELECT * FROM reports WHERE msgid = ".$msgid."");
if ($checkmsgx == FALSE)
{

$reportx = mysql_query("INSERT INTO reports (`msgid`, `marker`) VALUES ('$msgid', '".$u['userid']."')");

if ($reportx)
{
echo "<span class='boardbar2'>Thank you, this message has been marked for moderation.</span>";
}
else
{
echo "<span class='error'>Error: Could not complete moderation - ".mysql_error()."</span>";
}
}
else
{
$marknumx = mysql_query("SELECT marks FROM reports WHERE msgid = ".$msgid."");
$marknum = mysql_fetch_assoc($marknumx);
$uprepx = mysql_query("UPDATE reports SET marks = '". $marknum['marks']+1 ."' WHERE msgid = ".$msgid."");
if ($uprepx)
{
echo "<span class='boardbar2'>This message has already been reported, so your report has been merged. Thank you.</span>";
}
}
}
else
{
echo "<span class='error'>ERROR: You may only mark a message once.</span>";
}

}



#2 king arthur

king arthur
  • Members
  • PipPipPip
  • Advanced Member
  • 335 posts
  • LocationUK HQ

Posted 05 August 2006 - 10:49 PM

You're using double quotes inside double quoted strings for your queries, and because you are not adding "or die(mysql_error())" on the end of the call to mysql_query() you are not catching the syntax errors.  Use single quotes inside the strings instead.
Sir Isaac Newton said "If I have seen farther, it is by standing on the shoulders of giants". But it is not recorded as to whether he said it before or after he was hit on the head by a falling apple.

#3 zeroblitzt

zeroblitzt
  • Members
  • Pip
  • Newbie
  • 9 posts

Posted 05 August 2006 - 10:52 PM

Just to clarify, do you mean I should take:

  $checkusrx = mysql_query("SELECT * FROM reports WHERE marker = ".$u['userid']." AND msgid = ".$msgid."");


And make it into:

  $checkusrx = mysql_query('SELECT * FROM reports WHERE marker = '.$u['userid'].' AND msgid = '.$msgid.'');



OR do you mean I should do this:

  $checkusrx = mysql_query("SELECT * FROM reports WHERE marker = $u['userid'] AND msgid = $msgid");



#4 king arthur

king arthur
  • Members
  • PipPipPip
  • Advanced Member
  • 335 posts
  • LocationUK HQ

Posted 05 August 2006 - 10:57 PM

Sorry, forget that, it was the "" at the end of the queries that was confusing me.....
Sir Isaac Newton said "If I have seen farther, it is by standing on the shoulders of giants". But it is not recorded as to whether he said it before or after he was hit on the head by a falling apple.

#5 king arthur

king arthur
  • Members
  • PipPipPip
  • Advanced Member
  • 335 posts
  • LocationUK HQ

Posted 05 August 2006 - 11:01 PM

The thing is, you're not actually checking the number of rows returned - I think if mysql_query doesn't return false it does not necessarily mean any rows were returned.
Sir Isaac Newton said "If I have seen farther, it is by standing on the shoulders of giants". But it is not recorded as to whether he said it before or after he was hit on the head by a falling apple.

#6 zeroblitzt

zeroblitzt
  • Members
  • Pip
  • Newbie
  • 9 posts

Posted 05 August 2006 - 11:08 PM

Well I just changed it to

$checkusrx = mysql_query("SELECT * FROM reports WHERE marker = ".$u['userid']." && msgid = ".$msgid."") or die(mysql_error());
$checkusr = mysql_fetch_assoc($checkusrx);

if (!$checkusr)
{


But still nothing (as in its not giving me an Mysql error message or one of my own error messages).

#7 king arthur

king arthur
  • Members
  • PipPipPip
  • Advanced Member
  • 335 posts
  • LocationUK HQ

Posted 05 August 2006 - 11:15 PM

Try putting some stand-alone echo statements in various places to find out what it actually is doing.
Sir Isaac Newton said "If I have seen farther, it is by standing on the shoulders of giants". But it is not recorded as to whether he said it before or after he was hit on the head by a falling apple.

#8 zeroblitzt

zeroblitzt
  • Members
  • Pip
  • Newbie
  • 9 posts

Posted 05 August 2006 - 11:17 PM

Yep, I did and I found the problem: Seems you were right about the mysql_query not returning FALSE everywhere, because in another query I do the same thing (check to see if its false) and it wasnt coming up. I got it sorted now, everything seems to be working too :D

Thanks for the help.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users