Search code examples
phpformsradio-button

Populating radio buttons?


i have written some code that populates radio buttons with data from a database. I am having some problems with this code though, for one the form allows me to select multiple radio buttons at once which it shouldn't. Another issue is that the text shown besides the button itself is the values from the "customerID" column in the table when it should the values from the "lastName" column in the table and the value of the radio button should be the values in the "customerID" field which seems fine. If you wanted to know the actual structure of the columns in the table, the "customerID" column is first, "firstName" is second (but not needed in this form) and "lastName" is third.

Here is my current code :

<?php
$conn = mysql_connect("localhost", "twa312", "dam6av9a");
mysql_select_db("warehouse312", $conn)
or die ('Database not found ' . mysql_error() );

$sql = "select customerID, lastname from customer";
$rs = mysql_query($sql, $conn)
or die ('Problem with query' . mysql_error());

while ($row=mysql_fetch_array($rs)) {
$options .= '<input type="radio" id="custID" name="custID" value="'.$row[0].'" />'.$row[1];
 }

?>


<form method="GET" action="task8.php" id="custinfo">

Choose name:<?php echo $options; ?><br>

<p><input type="submit" name="submit" value="Submit"/>&nbsp;<input type="reset" value="Reset" />

</form>

Any help with solving this would be really great!


Solution

  • It doesn't matter what the order of the columns is in your database... it matters the order you're selecting the fields in. Since you first select lastName, you need to replace $row[1] with $row[0].

    Also, try changing the id so that it is unique.