尝试开机自检“选择”HTML表单输入时出现PHP错误
PHP error when trying to POST "select" HTML form input
所以我创建了一个非常简单的HTML调查表单,它只有一个选择下拉输入和一个文本输入字段。仅当选择字段位于"其他"选项上时,才会显示文本输入。所以这一切都很好,我有JavaScript处理所有这些,它工作得很好。
现在,当我尝试将表单值发布到PHP文件,然后将值插入数据库表时,问题就出现了。每次尝试提交表单时,我都收到此错误:
Error: INSERT INTO survey (select, other) VALUES ('flyer','')
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' other) VALUES ('flyer','')' at line 1
由于奇怪的单引号似乎切断了sql字符串的第一部分,我被引导相信我以某种方式SQL在我的表单POST或PHP验证中注入了自己。我已经多次浏览代码以寻找奇怪的引号,从头开始完全重写表单,从我的数据库中三次检查表和行名,甚至抓住一个我知道在其他地方工作的SQL字符串将其更改为在这里工作。不幸的是,我总是遇到同样的错误,所以我非常感谢你们能借给我的任何见解或帮助。
我的 HTML 表单:
<form action="includes/survey.php" method="POST" id="hear_form">
<label for="hear_select">How did you here about us?</label>
<br>
<select id="hear_select" name="hear_select">
<option value='flyer'>
Flyer left on door
</option>
<option value='email'>
Email from Troop
</option>
<option value='sodo'>
SODO News
</option>
<option value='conway'>
Conway News
</option>
<option value='southwest'>
Southwest Orlando Bulletin
</option>
<option value='winter'>
Winter Park Observer
</option>
<option value='baldwin'>
Baldwin Park Living
</option>
<option value='facebook'>
Facebook
</option>
<option value='neighborhood'>
Neighborhood posting
</option>
<option value='other'>
Other
</option>
</select>
<label id="otherlabel" for="other_type">Where else did you hear about us?</label>
<input id="other_type" type="text" name="other_type" maxlength="200" value="">
<input type="submit" value="Submit" id="hear_submit">
</form>
我的PHP:
require_once 'db_con.php';
require_once 'functions.php';
$selectErr = "";
$otherErr = "";
//validating inputs
if ($_SERVER["REQUEST_METHOD"] == "POST"){
if (empty($_POST["hear_select"])){
$selectErr = "* An answer is required";
$valid = false;
}else{
$select = test_input($_POST["hear_select"]);
$valid = true;
}
if (empty($_POST["other_type"])) {
$other = test_input($_POST["other_type"]);
$valid = true;
}else{
if((strlen($_POST["other_type"]) < 200)){
$other = test_input($_POST["other_type"]);
$valid = true;
}else{
$otherErr = "* An answer must have less than 200 characters";
$valid = false;
}
}
if($selectErr != '' || $otherErr != ''){
$valid = false;
}
if($valid){
var_dump($_POST);
//inserting variables into the database
$sql = "INSERT INTO survey (select, other) VALUES ('$select','$other')";
//checking if all worked, if it did redirect page top next step
if ($mysqli->query($sql) === TRUE) {
header( 'Location: index.php' ) ;
} else {
echo "Error: " . $sql . "<br>" . $mysqli->error;
}
$mysqli->close();
exit;
}
}
function test_input($data) {
$data = trim($data);
$data = str_replace('"', "", $data);
$data = str_replace("'", "", $data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
查询的问题在于"select"是SQL标准中的保留关键字,如果要将其用作列名,则必须正确转义。
最佳选择是重命名列,或在查询中对其进行转义。 有关更多详细信息,请参阅以下链接。
转义保留关键字
相关文章:
- JQuery关注上一个点击的输入错误
- Jquery - 文本等于(邮件输入错误)
- 如何在输入错误的用户名和/或密码时将用户从登录控件重定向到错误页面
- HTML5 从 JavaScript 手动设置输入错误
- 如果输入错误的用户名或密码,则显示错误页面
- 防止在用户输入错误时重新加载页面
- 当输入错误时,如何停止 HTML 将数据插入数据库
- 值未从输入错误插入数据库:phonegap中未定义
- 一个不需要输入错误消息的assert函数
- 如果输入错误,将文本输入保持为只读状态4秒
- 使用Javascript输入错误时更改边框颜色
- Javascript null输入错误
- 页面在输入错误的用户名/密码时被刷新,但没有显示错误消息
- 当之前输入错误的值时,Javascript验证不起作用
- PDF.js -输入错误密码时的回呼问题
- 表单验证导致表单字段在数据库中输入错误
- 输入错误密码时显示消息
- 在angular js中,如果用户输入错误的路径并显示正确的URL,它会重定向到主页
- 如何通过URL获取值,防止输入错误
- jquery.使用jquery 1.10.2时,编辑放置输入错误(示例如下)