【问题标题】:Get a selected value from radio button and insert it to MySQL从单选按钮获取选定的值并将其插入 MySQL
【发布时间】:2015-11-10 05:50:01
【问题描述】:

我想从选中的单选按钮中获取一个值并将其插入 MySQL。

我已经使用了isset(),但是当我检查结果时,我无法获得选中的单选按钮。

有人可以帮我吗?

<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");
}

echo $_POST['mentor'] 返回这个 '辅导老师'

【问题讨论】:

标签: php html mysql forms


【解决方案1】:

按照您当前的编写方式 (&lt;input type="radio" value="mentor" name="mentor"/&gt;),您的所有单选按钮将具有相同的值:"mentor"。您需要将值设置为等于导师的名称,如下所示:

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

然后,在doRegisCourse.php 中,您实际上并没有从 $_POST 中拉出选定的导师。您正在检查是否设置了$_POST['mentor'],但是您将INSERTing 到membercourse 的导师的值来自另一个mysql 查询,而不是来自发布的值。

这可能是您的想法,但是让人们为导师选择一个值然后没有真正使用它似乎很奇怪。

【讨论】:

  • 检查是否设置时获取它的方式相同:$_POST['mentor']。如果这是您想要的值,您可以在查询中使用该值而不是 $arr['mentorName']。我希望我能提供更好的建议,但我对 SELECT * from msmentor 的用途有点困惑,所以我实际上并不完全确定您的代码的总体目标是什么。
  • 好的。感谢您的帮助!很有帮助
猜你喜欢
  • 2014-10-07
  • 2012-10-14
  • 2016-08-30
  • 1970-01-01
  • 2013-08-13
  • 2019-06-26
  • 2017-04-27
  • 1970-01-01
相关资源
最近更新 更多