i have a problem with this code regarding in counting..

index.php

<?php
  
       
   
      if(isset($_GET['btnsubmit'])){
   
       
   
      $can = array($_GET['pres'],$_GET['vp']);

  
      mysql_connect('localhost','root');
  
      mysql_select_db('sample_poll');
  
       

      foreach($can as $val){

      $sql = mysql_query( "UPDATE candidate SET votes=votes+1 where can_id='".$val."'" );

      }
 
      echo "<h1>vote complete!</h1>

      <a href='voteresult.php'>see all vote results</a>";

       

      }else{

          echo "<form method='get'>";
      mysql_connect('localhost','root');
   
      mysql_select_db('sample_poll');


        $sql = mysql_query( "SELECT * FROM candidate WHERE category='president'" );
        echo "<h4>President</hr><table>  ";

      while($row = mysql_fetch_array($sql)){
 
      echo "<tr><td>".$row['can_names']."</td> <td><input type='radio' name='president' value='1'></td></tr>";

      }
  
      echo "</table>";
 

 
      $sql2 = mysql_query( "SELECT * FROM candidate WHERE category='vice-president'" );

      echo "<h4>Vice-President</hr> <table>";

      while($row2 = mysql_fetch_array($sql2)){
  
      echo "<tr><td>".$row2['can_names']."</td><td><input type='radio' name='vice_president' value='2'></td></tr>";

            }


      echo "</table>";
       echo"<p><input type='submit' name='btnsubmit' value='VOTE'></p>";
       echo"</form>";

      }

        ?>

it works very well..but when i select candidates and see the result of votes,it didn't count..
here's the displayvotes.php

<?php
 
      mysql_connect('localhost','root'); // i use default, but use your custom DBMS log-in
   
      mysql_select_db('sample_poll'); // sample database, i user your name.. hehe
  
      echo "<h3>vote results</h3>";
  
       
         $sql = mysql_query( "SELECT * FROM candidate WHERE category='president'" );
        echo "<h4>President</hr>

      <table>

      <tr>
  
      <th>name</th><th>votes</th>

      </tr>";
 
      while($row = mysql_fetch_array($sql)){
 
      echo "<tr>

      <td>".$row['can_names']."</td><td>".$row['votes']."</td>
 
      </tr>";
  
      }
  
      echo "</table>";
 
       
 
      $sql2 = mysql_query( "SELECT * FROM candidate WHERE category='vice-president'" );
 
      echo "<h4>Vice-President</hr>
  
      <table>
  
      <tr>
  
      <th>name</th><th>votes</th>
  
      </tr>";
 
      while($row2 = mysql_fetch_array($sql2)){
  
      echo "<tr>
 
      <td>".$row2['can_names']."</td><td>".$row2['votes']."</td>
  
      </tr>";
  
      }
  
      echo "</table>";





     
 
   
     
      ?>

run this program you will see the problem..tnx..hope to solve this one..tnx. :)

Member Avatar for diafol

Think your $val is giving 'Array' as output as opposed to the candidate id. Try $val[0];

Think your $val is giving 'Array' as output as opposed to the candidate id. Try $val[0];

i'll try using $val[0]; but it didn't work..it should count and validate the student's id..the voter must vote only once,if twice the message 'already voted' will appear on the page...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.