so I have this problem. When I select a radio button of question one for example it selects the radio button. But when I select another radio button from a different question, the first radio button that I've selected isn't selected anymore and the other radio button is selected. I've tried searching for a solution,but I can't find any. It's for a school project, we have to make a poll. Here's the code:
<?php
mysql_connect(" ", " ", " ");
mysql_select_db(" ") or die(mysql_error());
if(isset($_POST['verzenden'])) {
$query="UPDATE optie SET stemmen = stemmen + 1 WHERE id='" . $_POST['item']. "'";
}
if(mysql_query($query)){
echo"Stem opgeslagen! <br /><br/>";
} else {
echo"Fout tijdens opslaan stem!<br /><br/>";
}
$result = mysql_query("SELECT * FROM poll");
while($data = mysql_fetch_assoc($result)){
echo"<b>" . $data['vraag'] . "</b><br/><br />";
echo"<form method='post' action=''>";
echo "<fieldset>";
$result2 = mysql_query("SELECT * FROM optie WHERE poll ='" . $data['id'] . "'");
while($data2 = mysql_fetch_assoc($result2)){
echo"<input type='radio' name='item' value='" . $data2['id'] . "'/>" . $data2['optie'] . "<br />";
}
echo"<br/>";
echo"</fieldset>";
}
?>
<input type='submit' name='verzenden' value='Verzenden'>
</form>
<a href="checkbox.php">Ga verder</a>
I hope that someone can help me. bye, Catherine
That's happening because all of your radio button sets have the same name: item
. Here are a couple ways you can make the radio button sets unique; pick the one that's best for you.
Append the poll.id
value to each button name:
$result2 = mysql_query("SELECT * FROM optie WHERE poll ='" . $data['id'] . "'");
while($data2 = mysql_fetch_assoc($result2)){
echo"<input type='radio' name='item_" . $data['id'] . "' value='" . $data2['id'] . "'/>" . $data2['optie'] . "<br />";
}
Append a sequence number to each button name:
$result2 = mysql_query("SELECT * FROM optie WHERE poll ='" . $data['id'] . "'");
$buttonNumber = 0;
while($data2 = mysql_fetch_assoc($result2)){
++$buttonNumber;
echo"<input type='radio' name='item_$buttonNumber' value='" . $data2['id'] . "'/>" . $data2['optie'] . "<br />";
}
Finally, note that the mysql_query
family of functions is deprecated because it can leave you vulnerable to SQL Injection attacks. See here and other places for information about using mysqli
or PDO
instead.