I am developing a links voting site, and I have this function, to check if the user already voted the link:
function has_voted($user)
{
try
{
$db = parent::getConnection();
$query = "select id from votes where username = '$user' and article_id = $this->id";
$results = parent::execSQL($query);
if($results->num_rows == 1) {
return true;
}
else
{
return false;
}
parent::closeConnection($db);
}
catch(Exception $e){
throw $e;
}
}
And in the frontpage I display an image to vote with this line:
<a href="/index.php?action=vote&param=<?php echo $articles[$index]->getId(); ?>">
<img class="vote_button" src="assets/images/triangulo.png" />
</a>
What I want its to insert an "if" to display a different image if the user already voted, I tried this but it shows errors:
<a href="/index.php?action=vote&param=<?php echo $articles[$index]->getId(); ?>">
<?php if($articles[$index]->has_voted($articles[$index]->getUsername()) == true)
{ ?><img src="assets/images/triangulo.png"/></a><?php }
else
{ ?><img class="vote_button" src="assets/images/triangulo2.png" /></a><?php } ?>
+++Edit:
Schnalle,
Thanks for the analysis, this is what I did:
ok I took parent::closeConnection($db) out, thanks
I try to cut the catch statement also but I got this error:
Parse error: syntax error, unexpected '}', expecting T_CATCH in /home/mexautos/public_html/kiubbo/data/article.php on line 155
I get the user name here, I dont know if its safe enough:
function getUsername(){ return $this->username; }
I tried this code to sanitize it:
$query = sprintf("select id from votes where username = '$user' and article_id = $this->id", mysql_real_escape_string($user), mysql_real_escape_string($password));
but I get this error for the mysql_real_escape lines:
Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'mexautos'@'localhost' (using password: NO) in /home/mexautos/public_html/kiubbo/data/article.php on line 145 Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in /home/mexautos/public_html/kiubbo/data/article.php on line 145 Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'mexautos'@'localhost' (using password: NO) in /home/mexautos/public_html/kiubbo/data/article.php on line 146 Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in /home/mexautos/public_html/kiubbo/data/article.php on line 146
I will close the tag outside once I fix this, I was not sure if it worked separately.
You are right I was getting the wrong variable. I changed to $_SESSION['user'] and it worked!
I understand what you say: instead of writing links with a loop just select them and write them down, I will check that to learn how to do it.
I use an id for this, let me implement it.
Thanks, CS