Still speaking generally, I thought I better clear up a misapprehension here:
Rodney H. wrote:You are using an assignment operator "=", but assignment I think will always be true.
Not quite: the value of an assignment expression is the value of whatever was being assigned, so "$rs=5" evaluates to 5 and "$rs=0" evaluates to 0. (That's why statements like ($a = $b = $c; can work). It's that value that gets tested by the if statement to see if it's equivalent to true or false. In the latter example, 0 is equivalent to false, so if($rs=0) always fails.
Back to specifics. Taking the most recent version of the posted code, and sweeping up some of the discarded rubbish lying around, and then using
tags around the code to save my eyesight, I get:
[code=php]@session_start();
require_once("engine.php");
// Checks if the user is authorized
if (!is_authorized())
{
print "You must login before voting!";
exit();
}
// Checks the item input
if (!check_item_id(@$_GET["id"]))
{
print "Incorrect item id or missing";
exit();
}
// Pick up the user ID of the user
$link = mysql_connect("localhost", "user", "pass") or die("Unable to connect to the database");
mysql_select_db("bestof");
$query = "select id FROM users where username= '".$_SESSION["username"]."'";
$rs_query=mysql_query($query,$link);
$rs=mysql_fetch_array($rs_query);
$userid = $rs['id'];
//echo $userid
$result = mysql_query("SELECT * FROM items WHERE id_user = '$userid' AND id_category = " . $_GET['id']) or exit(mysql_error());
if (mysql_num_rows($result))
{
echo 'You already voted';
}
else
{
echo 'Go ahead and vote';
}
I'd improve on Devinmke's code a bit; if you only want to see if there are rows, and you don't care about what they contain...
$result = mysql_query("SELECT COUNT(*) FROM items WHERE id_user = '$userid' AND id_category = " . $_GET['id']) or die(mysql_error());
if (mysql_result($result, 0)!=0)
{
echo 'You already voted';
}
else
{
echo 'Go ahead and vote';
}
Then again, if you're getting $userid from the database only so that it can be used in a database query....
$link = mysql_connect("localhost", "user", "pass") or die("Unable to connect to the database");
mysql_select_db("bestof");
$count_votes = mysql_query("
SELECT
COUNT(*)
FROM
items, users
WHERE
items.id_category = " . $_GET['id']."
AND users.username = '".$_SESSION['username']."'
AND items.id_user = users.id") or die(mysql_error());
if (mysql_result($count_votes, 0)!=0)
{
echo 'You already voted';
}
else
{
echo 'Go ahead and vote';
}
Which does look like the correct query to use.