Different Image depending if user has already voted
<p>I am developing a links voting site, and I have this function, to check if the user already voted the link:</p>

<pre><code>function  has_voted($user) 
    {
       try
        {
            $db = parent::getConnection();
            $query = "select id from votes where username = '$user' and article_id  = $this-&gt;id";
            $results = parent::execSQL($query);
            if($results-&gt;num_rows == 1) {
                return true;                 
            }
            else 
            {
                return false;
            }            
            parent::closeConnection($db);

        }
        catch(Exception $e){
            throw $e;
        }       
    }
</code></pre>

<p>And in the frontpage I display an image to vote with this line:</p>

<pre><code>  &lt;a href="/index.php?action=vote&amp;amp;param=&lt;?php echo $articles[$index]-&gt;getId(); ?&gt;"&gt;
&lt;img class="vote_button" src="assets/images/triangulo.png" /&gt;
&lt;/a&gt;
</code></pre>

<p>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:</p>

<pre><code>    &lt;a href="/index.php?action=vote&amp;amp;param=&lt;?php echo $articles[$index]-&gt;getId(); ?&gt;"&gt;
&lt;?php if($articles[$index]-&gt;has_voted($articles[$index]-&gt;getUsername()) == true) 
{ ?&gt;&lt;img src="assets/images/triangulo.png"/&gt;&lt;/a&gt;&lt;?php } 
else 
{ ?&gt;&lt;img class="vote_button"  src="assets/images/triangulo2.png" /&gt;&lt;/a&gt;&lt;?php } ?&gt;
</code></pre>

<p>+++Edit:</p>

<p>Schnalle,</p>

<p>Thanks for the analysis, this is what I did:</p>

<ul>
<li><p>ok I took parent::closeConnection($db) out, thanks</p></li>
<li><p>I try to cut the catch statement also but I got this error: <code>Parse error: syntax error, unexpected '}', expecting T_CATCH in /home/mexautos/public_html/kiubbo/data/article.php on line 155</code></p></li>
<li><p>I get the user name here, I dont know if its safe enough:   </p>

<p>function getUsername(){
        return $this->username;
    }</p>

<p>I tried this code to sanitize it:</p>

<p>$query = sprintf("select id from votes where username = '$user' and article_id  = $this->id", 
   mysql_real_escape_string($user),
   mysql_real_escape_string($password));</p>

<p>but I get this error for the mysql_real_escape lines:</p></li>
</ul>

<p>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</p>

<ul>
<li><p>I will close the  tag outside once I fix this, I was not sure if it worked separately.</p></li>
<li><p>You are right I was getting the wrong variable. I changed to $_SESSION['user'] and it worked! </p></li>
<li><p>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.</p></li>
<li><p>I use an id for this, let me implement it.</p></li>
</ul>

<p>Thanks,
CS</p>
