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

Dani AI

Generated

Short answer: yes — one submit will send every checked radio if each office uses a different name (as said). already set different names (vote1, vote2, vote3) so the browser will post all of them together. The server just needs to process each posted field instead of handling only vote1.

A cleaner approach is to name inputs as an array so you can loop easily (example HTML and a secure server-side pattern):

<form method="post" action="process_votes.php">
  <!-- example: use logical keys -->
  <input type="radio" name="vote[president]" value="12"> Alice
  <input type="radio" name="vote[president]" value="13"> Bob

  <input type="radio" name="vote[vice]" value="21"> Carol
  <input type="radio" name="vote[vice]" value="22"> Dave

  <input type="submit" value="Vote!">
</form>
// process_votes.php (sketch using PDO)
$votes = $_POST['vote'] ?? [];
$pdo = new PDO(..., [PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION]);
$pdo->beginTransaction();
$stmt = $pdo->prepare('UPDATE candidates SET votecount = votecount + 1 WHERE c_ID = ?');
foreach ($votes as $office => $cid) {
  if (!ctype_digit((string)$cid)) continue;      // basic validation
  // optional: verify $cid belongs to $office before counting
  $stmt->execute([$cid]);
}
$pdo->commit();
header('Location: thanks.php'); // PRG to avoid duplicate submissions
exit;

Cautions/troubleshooting: validate candidate IDs server-side and check they match the expected office (prevent tampering); use prepared statements (avoid deprecated mysql_*); protect against double voting (session, voter table, token); log errors and var_dump($_POST) while debugging; use transactions so partial updates do not occur.

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.

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.

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.