0

Hello everyone! I need your help. So I am currently working on my school project for web based voting system and I'm having a hard time to figure it out how to pass value 1 of multiple selected radio buttons to database. Here's my code so far:

Here is my voting form:

---CUT---

<form action="show-poll.php" method="post">


<?php
include('voting_connect.php');
{
$result = mysql_query("SELECT * FROM candidates WHERE c_position='President'")
or die(mysql_error());  

echo "<center><table id='tables' class='sortable'>";
echo "<tr><th scope='col' abbr='' class='nobg'>&nbsp;&nbsp;</th> <th>President:</th></tr>";

while($row = mysql_fetch_array($result))
  {
echo "<tr>";
echo "<td scope='row' class='spec'>" . '<input name="vote1" type="radio" value="'.$row['c_ID'].'">' . "</td>";
echo "<td>";
echo '<a href=# alt=Candidate Profile rel=tooltip content="<div id=imagcon><img src='.$row['c_Location'].' class=tooltip-image/></div><div id=con>Running for:'.$row['c_position'].'</div><div id=con>Gender:'.$row['c_gender'].'</div><div id=con>Year:'.$row['c_Year'].'</div><div id=con>Course:'.$row['c_Course'].'</div><div id=con>Class:'.$row['c_Class'].'</div><div id=con>Partylist:'.$row['c_Partylist'].'</div>">'.$row['c_fname'].' '.$row['c_lname'].'</a>'.'<br>';
echo "</td>";
echo"</tr>"; 

 }
 echo"</table></center>";

}
?> 
<br></br>
<?php
include('voting_connect.php');
{
$result = mysql_query("SELECT * FROM candidates WHERE c_position='Vice President'")
or die(mysql_error());  

echo "<center><table id='tables' class='sortable'>";
echo "<tr><th scope='col' abbr='' class='nobg'>&nbsp;&nbsp;</th> <th>Vice President:</th></tr>";

while($row = mysql_fetch_array($result))
  {
echo "<tr>";
echo "<td scope='row' class='spec'>" . '<input name="vote2" type="radio" value="'.$row['c_ID'].'">' . "</td>";
echo "<td>";
echo '<a href=# alt=Candidate Profile rel=tooltip content="<div id=imagcon><img src='.$row['c_Location'].' class=tooltip-image/></div><div id=con>Running for:'.$row['c_position'].'</div><div id=con>Gender:'.$row['c_gender'].'</div><div id=con>Year:'.$row['c_Year'].'</div><div id=con>Course:'.$row['c_Course'].'</div><div id=con>Class:'.$row['c_Class'].'</div><div id=con>Partylist:'.$row['c_Partylist'].'</div>">'.$row['c_fname'].' '.$row['c_lname'].'</a>'.'<br>';
echo "</td>";
echo"</tr>"; 
 }
 echo"</table></center>";
}
?> 
<br></br>
<?php
include('voting_connect.php');
{
$result = mysql_query("SELECT * FROM candidates WHERE c_position='Secretary'")
or die(mysql_error());  

echo "<center><table id='tables' class='sortable'>";
echo "<tr><th scope='col' abbr='' class='nobg'>&nbsp;&nbsp;</th> <th>Secretary:</th></tr>";

while($row = mysql_fetch_array($result))
  {
echo "<tr>";
echo "<td scope='row' class='spec'>" . '<input name="vote3" type="radio" value="'.$row['c_ID'].'">' . "</td>";
echo "<td>";
echo '<a href=# alt=Candidate Profile rel=tooltip content="<div id=imagcon><img src='.$row['c_Location'].' class=tooltip-image/></div><div id=con>Running for:'.$row['c_position'].'</div><div id=con>Gender:'.$row['c_gender'].'</div><div id=con>Year:'.$row['c_Year'].'</div><div id=con>Course:'.$row['c_Course'].'</div><div id=con>Class:'.$row['c_Class'].'</div><div id=con>Partylist:'.$row['c_Partylist'].'</div>">'.$row['c_fname'].' '.$row['c_lname'].'</a>'.'<br>';
echo "</td>";
echo"</tr>"; 
 }
 echo"</table></center>";
}
?> 
<br />
<br />
<input type="submit" value="Vote!"/>

---CUT---

this is the show-poll.php

---CUT---

<?php  

if (isset($_POST['vote1']))  
    $vote=($_POST['vote1']); 


// database connection  
if (!$db_conn = mysql_connect('localhost', 'root', 'vertrigo'))  
{  
echo 'Could not connect to the database.<br>';  
exit;  
};  
mysql_select_db('db_voting');  
if (!empty($vote))

{  
$vote = addslashes($vote);

$query = "update candidates set votecount = votecount + 1 where c_ID = '$vote'";  
if(!($result = mysql_query($query, $db_conn)))  
{  
echo 'Could not connect to db<br>';  
exit;  
}  
};  

---CUT---

2
Contributors
2
Replies
4
Views
5 Years
Discussion Span
Last Post by aianne
0

Radio buttons are usually for single selection. Checkboxes for multiple.

You can still use radio buttons but you cannot group them with the same name.

0

Yes sir. in every category I have different radio buttons' name. As you can see on my code above sir, In 'President' the radio button name of it is 'vote1' while on 'Vice President' it is 'vote2'. and so I want all the value of selected radio button will pass to database with just one submit button. Do you have any idea sir? Any help would be greatly appreciated.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.