douchen4534 2017-06-18 07:53
浏览 78
已采纳

单选按钮值,仅发送一个表单参数

I want to use radio buttons in my application.When i click Male , i want it to input male into the database, when i click Female, it sends female into the database.

Code looks like this

<html>
<head></head>
<title>Radio Test</title>
<body>
<h1>Radio Test</h1>
<form name="testform" id="testform" method="post" action="test.php" />
Name : <input type="text" name="fullname" id="fullname" /></br>
    Male <input type="radio" name="male" id="male" value="Male" />
    Female <input type="radio" name="female" id="female" value="FeMale" /></br></p>
        <input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>

For the HTML Area, Now i want to make it output values, but it doesnt respond. My PHP looks like this but instead of sending one value, it sends both

<?php

$host = "127.0.0.1"
$user = "root"
$pass = ""
$db = "people_info"

$con = mysqli_connect($host, $user, $pass, $db) or die('Cannot Connect :'.mysqli_error());
$fullname = mysqli_real_escape_string($con, $_POST['fullname']);
$male = mysqli_real_escape_string($con, $_POST['male']);
$female = mysqli_real_escape_string($con, $_POST['female']);

$sql = "insert into data (fullname,male,female) values ('".$fullname."', '".$male."', '".$female."')";
mysqli_query($con,$sql) or die ('Failed Query :'.mysqli_error($con));
mysqli_close($con);
?>

I was thinking of using switch case, but that made it even worse. It didnt send any values as well..

  • 写回答

2条回答 默认 最新

  • dow46218 2017-06-18 07:59
    关注

    I think, you should have one column Gender in your table. I'm not sure why are you storing it in two different column.

    Change your code to following

    Male <input type="radio" name="gender" id="male" value="Male" />
    Female <input type="radio" name="gender" id="female" value="FeMale" /></br></p>
    

    Then,

     $gender = mysqli_real_escape_string($con, $_POST['gender']);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?