G' day! my code here was good..

<?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'>
             
      <h2><center>Select Candidates</h2>

       

      <h3>president:</h3>
      1.) lace <input type='radio' name='president' value='1'>

      2.) lawrence <input type='radio' name='president' value='2'>

      <h3>vice-president:</h3>

      1.) neji <input type='radio' name='vice_president' value='3'><br />

      2.) lee <input type='radio' name='vice_president' value='4'>


 
      <p><input type='submit' name='btnsubmit' value='VOTE'></p>

      </form>";
 
       
        }
        ?>

the name lace,lawrence,neji and lee is given..i want to display the names saved in database by president and vice_president..

For starters try replacing line 20 with the following.

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

For starters try replacing line 20 with the following.

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

hmmm before i change and run it,i know i need to change those names based in my database right.? i forgot how to use it in php..but something like this

<?php echo $name; ?>

but im not sure with that.. =p i know it doesn't work..that's why im asked for that correct code..tnx again..Godbless :)

You can try this:

<?php
$sql = mysql_query("SELECT * FROM `candidate`");
while ($res = mysql_fetch_array($sql))
{
?>
   <?php echo $res['name']; ?> <input type='radio' name='president' value='<?php echo $res['can_id']; ?>'>

You can try this:

<?php
$sql = mysql_query("SELECT * FROM `candidate`");
while ($res = mysql_fetch_array($sql))
{
?>
   <?php echo $res['name']; ?> <input type='radio' name='president' value='<?php echo $res['can_id']; ?>'>

ok i'll try this one.. hope it will work.. :) tnx..

hehe that code is familiar... ill help you takeshi when you still got problems.... when i get home and after i had my 8 hour sleep. coz im working now and i can't do that right now hehe.

yeah kishan is right u need just a simple query

<?php

$sql = mysql_query( "SELECT name FROM candidate" );
while($row = mysql_fetch_array($sql)){
echo $row['name']."<br />";
}

?>

here try this link so you have full understanding on traversing query results http://www.w3schools.com/PHP/func_mysql_fetch_array.asp

yeah kishan is right u need just a simple query

<?php

$sql = mysql_query( "SELECT name FROM candidate" );
while($row = mysql_fetch_array($sql)){
echo $row['name']."<br />";
}

?>

here try this link so you have full understanding on traversing query results http://www.w3schools.com/PHP/func_mysql_fetch_array.asp

ok..tnx...but kishan's code didn't work well..there's an error..i mean nothings happen..ill try your code..but i guess it's the same..
here's my sql:
candidate table:

CREATE TABLE `candidate` (
  `can_id` int(4) NOT NULL auto_increment,
  `name` varchar(30) NOT NULL,
  `votes` int(20) NOT NULL,
  `category` varchar(25) NOT NULL,
  PRIMARY KEY  (`can_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;

voter table:

CREATE TABLE `voter` (
  `voter_id` int(4) NOT NULL auto_increment,
  `name` varchar(30) NOT NULL,
  PRIMARY KEY  (`voter_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;

This should work, or do you want something else?

<?php
$sql = mysql_query("SELECT * FROM `candidate` WHERE `category`='President'");
while ($res = mysql_fetch_array($sql))
{
?>
   <?php echo $res['name']; ?> <input type='radio' name='president' value="<?php echo $res['can_id']; ?>"/>
<?php }  ?>

This should work, or do you want something else?

<?php
$sql = mysql_query("SELECT * FROM `candidate` WHERE `category`='President'");
while ($res = mysql_fetch_array($sql))
{
?>
   <?php echo $res['name']; ?> <input type='radio' name='president' value="<?php echo $res['can_id']; ?>"/>
<?php }  ?>

yeah.but it didn't work..there's an error..
here's the code of mine i've revised..
index.php

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

  
      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
  
       

      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'>
             
      <h2><center>Select Candidates</h2>



      <h3>president:</h3>
      <?php
$sql = mysql_query("SELECT * FROM `candidate` WHERE `category`='President'");
while ($res = mysql_fetch_array($sql))
{
?>
   <?php echo $res['name']; ?> <input type='radio' name='president' value="<?php echo $res['can_id']; ?>"/>
<?php }  ?>

      2.) bhebe <input type='radio' name='president' value='2'>

      <h3>vice-president:</h3>

      1.) neji <input type='radio' name='vice_president' value='3'><br />

      2.) lee <input type='radio' name='vice_president' value='4'>


 
      <p><input type='submit' name='btnsubmit' value='VOTE'></p>

      </form>";
 
       
        }
        ?>

voteresult.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['name']."</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['name']."</td><td>".$row2['votes']."</td>
  
      </tr>";
  
      }
  
      echo "</table>";





     
 
   
     
      ?>

try to run these code in your server to see there's an error..my sql was given already..it should work...hmm

Spot the error hey. This sounds fun...
Anyways my suggestion is to use mysql_real_escape_string() on the $val variable (line 20) and also your mysql_connect() function doesn't have all of the required parameters. Also on line 39 of index.php needs the closing quotes and return symbol for the echo function.

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.