0

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'

Danila Ganchar
  • 10,266
  • 13
  • 49
  • 75
  • 2
    If you can, you should [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) [statements](http://php.net/manual/en/pdo.prepared-statements.php) instead, and consider using PDO, [it's really not hard](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Aug 17 '15 at 14:28
  • 2
    [Your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Jay Blanchard Aug 17 '15 at 14:28
  • what is `echo $_POST['mentor'];` returning? – Lal Aug 17 '15 at 14:30
  • try checking the POST data..add `var_dump($_POST)` ath the start of the PHP file and see if the POST array contains `mentor`. – Lal Aug 17 '15 at 14:31

1 Answers1

1

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.

Don't Panic
  • 41,125
  • 10
  • 61
  • 80
  • 1
    The same way you get it when you check if it is set: `$_POST['mentor']`. You can use that value in your query instead of `$arr['mentorName']`, if that is the value you want. I wish I could give better advice, but I'm a bit confused by what the `SELECT * from msmentor` is for, so I'm actually not completely sure what the overall goal of your code is. – Don't Panic Aug 17 '15 at 14:58
  • Okaay.Thanks for your help! it's very helpful – Michelle Lee Widjaja Aug 17 '15 at 15:24