Michelle Lee Widjaja
Michelle Lee Widjaja

Reputation: 21

Get a selected value from radio button and insert it to MySQL

I want to get a value from the checked radio button and insert it to MySQL.

I've already used isset(), but when i checked the result, I can't get the selected radio button.

Can someone help me?

<form method="post" class="input" action="doRegisCourse.php">
<table>
<?php
while($row=mysql_fetch_array($query)){
?>
<tr>
    <td><input type="radio" value="mentor" name="mentor"/></td>
    <td><?php echo $row['mentorName']?></td>
    <td><?php echo $row['course']?></td>
    <td><?php echo $row['nim']?></td>
    <td><?php echo $row['email']?></td>
</tr>   
<?php } ?>      
 </table>    
 <input type="submit" value="Next" name="submit">
 </form>

doRegisCourse.php

<?php
include "connect.php";
$name = $_POST['fullname'];
$name2 = $_POST['fullname2'];
$name3 = $_POST['fullname3'];
$course = $_POST['course'];
$mentor = mysql_query("SELECT * from msmentor");
$i = 1;

while ($arr = mysql_fetch_array($mentor)) {
    if (isset($_POST['mentor'])) {
        $query = "INSERT INTO membercourse(memberGroup,courseName,mentorName) 
    VALUES ('".$name."','".$course."','".$arr['mentorName']."'),('".$name2."','".$course."','".$arr['mentorName']."'),
    ('".$name3."','".$course."','".$arr['mentorName']."')";
        echo $_POST['mentor'];
     }
     $i++;
}

if (mysql_query($query)) {
    header("location:indexLogin.php");
}

the echo $_POST['mentor'] returns this 'mentormentormentormentor'

Upvotes: 0

Views: 1520

Answers (1)

Don&#39;t Panic
Don&#39;t Panic

Reputation: 41810

The way you have it written currently (<input type="radio" value="mentor" name="mentor"/>), all of your radio buttons will have the same value: "mentor". You need to set the value equal to the name of the mentor, like this:

<input type="radio" value="<?php echo $row['mentorName']?>" name="mentor"/>

Then, in doRegisCourse.php, you are not actually pulling the selected mentor from $_POST. You are checking to see if $_POST['mentor'] is set, but the values for mentor that you are INSERTing into membercourse are taken from another mysql query, not from the posted value.

That may be what you have in mind, but it seems strange to let people pick a value for mentor and then not really use it.

Upvotes: 1

Related Questions