我遇到了一个问题这是一个简单的形式我必须通过单选按钮提交数据问题是没有值显示在数据库中既没有disases名称也没有yes,没有选项我创建了数据库名为doctor with fields id, dis_name和ans。
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
require 'db.php';
if (!empty($_POST)) {
// keep track validation errors
$disError = null;
$ansError = null;
// keep track post values
$dis_name = isset($_POST['dis_name']);
$ans=isset($_POST['ans']);
// validate input
$valid = true;
if (empty($dis_name)) {
$disError = 'Please enter Diseases Name';
$valid = false;
}
if (empty($ans)) {
$ansError = 'Please check one of option';
$valid = false;
}
// insert data
if ($valid) {
if(isset($_POST['dis_name'])){
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO diseases (dis_name) values(?)";
$q = $pdo->prepare($sql);
$q->execute(array($dis_name));
Database::disconnect();
//header("location: diseases.php");
}
}
}
?>
//some html code here
<div class="control-group <?php echo !empty($ansError)?'error':'';?>">
<label class="check">Have you suffered pain preiviously???</label>
<div class="controls">
<input type="radio" name="choice" <?php if (isset($ans) && $ans=="yes") echo "checked";?>
value="Yes">Yes
<input type="radio" name="choice"<?php if (isset($ans) && $ans=="no") echo "checked";?>
value="No">No
<?php
//if(isset($_POST['submit'])){
if(!empty($_POST['choice'])){
$ans=isset($_POST['ans']);
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO diseases (question) values(?)";
$q = $pdo->prepare($sql);
$q->execute(array(isset($_POST['choice'])));
Database::disconnect();
}
//}
?>
`isset() it gives undefined index warning.`
答案 0 :(得分:0)
我认为ans
代表 answer ,它由radio
控件值填充。这就是你在服务器端获取它的方式:
$ans=isset($_POST['ans']);
在您的HTML中,它实际上是choice
你不应该这样得到吗?
$ans=isset($_POST['choice']);
编辑:
为什么要使用isset
函数来获取帖子值?
答案 1 :(得分:0)
isset()
返回布尔值,输入名为&#39; choice&#39;。
也许你的意思是:
if( isset($_POST['choice']) ){
$ans = $_POST['choice'];
// Optionally you may want $ans to be boolean:
if($ans == 'Yes') {
$ans = TRUE;
} else {
$ans = FALSE;
}
}
然后(如果正确定义了表列以存储布尔/整数/枚举):
$q->execute(array($ans));
将存储1(True)或0(False)