我有一个在前一天可以使用的表格,现在只能在我的iPhone上使用。我在mySQL中有4列,id,submissiondate,housenumber,答案,主机名
从我的iPhone提交时,它们都起作用。
如果您是从台式机尝试的,则“答案”是3个单选按钮,它们不起作用,并插入空白记录。
想知道以前是否有人遇到过。
if (isset($_POST["submit"])) {
$Answer = $_POST['Answer'];
if ($_POST['housenumber'] == "") {
echo "<div class='container' style='text-align:left'><div role='alert' class='alert alert-danger'>
<p class='lead'><strong>ERROR!</strong> All fields are required</p></div></div>";
} else {
$hostname='XXXXXXXXXXX';
$username='XXXXXXXXXXX';
$password='XXXXXXXXXXX';
try {
$dbh = new PDO("mysql:host=$hostname;dbname=qlfence",$username,$password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line
$sql = "INSERT INTO qlfence (id, SubmissionDate, housenumber, Answer, hostname)
VALUES ('id', NOW(),'".$_POST["housenumber"]."','".$_POST["Answer"]."','".$_POST["hostname"]."')";
if ($dbh->query($sql)) {
echo "<div class='container' style='text-align:left'><div class='alert alert-success' role='alert'><p class='lead'><strong>Awesome!</strong> Thanks for your input.</p></div></div>";
}
else{
echo "<script type= 'text/javascript'>alert('Opps, something went wrong. We will loo into');</script>";
}
header('Location: /index.php');
$dbh = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
}
?>
<form action="#" method="post" class="" id="sendmessage" name="sendmessage" >
<input type="hidden" class="form-control" id="hostname" name="hostname" readonly value="<?php echo $hostname; ?>" >
<div class="row">
<div class="col-md-2"> </div>
<div class="col-md-2"><center><input type="text" class="form-control" id="housenumber" name="housenumber" style="width: 35%; border: 2px solid black;"></center></div>
<div class="col-md-6"><label for="housenumber"><span class="lead" >Quarrie Lane #</span></label> </div>
<div class="col-md-2"> </div>
<br>
<br><br>
</div>
<div class="row">
<div class="col-md-2"> </div>
<div class="col-md-2"><input type="radio" id="a1" name="Answer" class="form-control" value="I am Willing to do the fence as soon as everyone is ready."></div>
<div class="col-md-6"><label for="a1"><p class="lead">Willing to do the fence as soon as everyone is ready.</p></label></div>
<div class="col-md-2"> </div>
</div>
<div class="row">
<div class="col-md-2"> </div>
<div class="col-md-2"><input type="radio" id="a2" name="Answer" class="form-control" value="I would rather wait until next summer."></div>
<div class="col-md-6"><label for="a2"><p class="lead">I would rather wait until next summer.</p></label></div>
<div class="col-md-2"> </div>
</div>
<div class="row">
<div class="col-md-2"> </div>
<div class="col-md-2"><input type="radio" id="a3" name="Answer" class="form-control" value="I am not the owner."></div>
<div class="col-md-6"><label for="a3"><p class="lead">Not the owner.</p></label></div>
<div class="col-md-2"> </div>
</div>
<input name="submit" id="submit" type="submit" value="Submit Answer" class="btn btn-lg btn-primary btn-block"></input>
</form>
最佳答案
您不收到$_POST['Answer']
的唯一原因是,在提交表单之前,您不检查任何答案。
除了安全性问题,您的代码也可以正常工作。但是您应该考虑为您的sql查询添加更多安全性。
解决方法是,默认情况下,您可以check
收音机之一,如下所示:
<input checked="true" type="radio" id="a3" name="Answer" class="form-control" value="I am not the owner." />
在这种情况下,您将始终收到您的
$_POST['Answer']
。