Jump to content

Archived

This topic is now archived and is closed to further replies.

zeroblitzt

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

Recommended Posts

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:

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

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.


[quote]
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>";
}

}
[/quote]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Just to clarify, do you mean I should take:
[quote]  $checkusrx = mysql_query("SELECT * FROM reports WHERE marker = ".$u['userid']." AND msgid = ".$msgid."");[/quote]

And make it into:

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


OR do you mean I should do this:
[quote]
  $checkusrx = mysql_query("SELECT * FROM reports WHERE marker = $u['userid'] AND msgid = $msgid");
[/quote]

Share this post


Link to post
Share on other sites
Sorry, forget that, it was the "" at the end of the queries that was confusing me.....

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Well I just changed it to

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

if (!$checkusr)
{[/quote]

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

Share this post


Link to post
Share on other sites
Try putting some stand-alone echo statements in various places to find out what it actually is doing.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

×

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.