道歉,因为这是一个重复的问题,但由于某些原因,我无法对我收到的答案发表任何评论,而且问题没有得到解答。
提交表单时(通过Ajax),我收到以下错误消息:
PHP警告:
mysqli_query()
[function.mysqli-query]:第32行/home1/xenongro/public_html/testing/enrolment/thanks.php中的空查询
我怀疑它与if / else语句有关,但不确定实际问题是什么。作为测试,我删除了if / else语句并成功提交了表单中的一些值。不幸的是,当我有条件的时候,它出现了上述错误。
有人可以帮忙吗?
<?php
$firstname = htmlspecialchars(trim($_POST['fname']));
$lastname = htmlspecialchars(trim($_POST['lname']));
$worktel = htmlspecialchars(trim($_POST['worktel']));
$funding = htmlspecialchars(trim($_POST['funding']));
$level = htmlspecialchars(trim($_POST['level']));
$dbc = mysqli_connect('localhost', 'xxxxx', '<xxxx>', 'xxxx')
or die ('Could not connect to MySQL server.');
if ($level != "IOSH Managing Safely"){
if ($funding == "Self Funding"){
$query = "INSERT INTO enrolments (fname, lname, worktel)" .
"VALUES ('$firstname', '$lastname', '$worktel')";
}
else if ($funding == "Employer Funding"){
$query = "INSERT INTO enrolments (fname, lname, worktel)" .
"VALUES ('$firstname', '$lastname', '$worktel')";
}
}
else if ($level == "IOSH Managing Safely"){
if ($funding == "Self Funding"){
$query = "INSERT INTO enrolments (fname, lname, worktel)" .
"VALUES ('$firstname', '$lastname', '$worktel')";
}
else if ($funding == "Employer Funding"){
$query = "INSERT INTO enrolments (fname, lname, worktel)" .
"VALUES ('$firstname', '$lastname', '$worktel')";
}
}
$result = mysqli_query($dbc, $query)
or die ('error querying database');
mysqli_close($dbc);
?>
答案 0 :(得分:0)
因为你正在使用else,如果在else if块中执行查询并在注释中恢复
$result = mysqli_query($dbc, $query) //Inside the else if block
答案 1 :(得分:0)
如果您通过ajax发布并使用jquery,您应该首先提交这样的内容,否则它不会将任何帖子值带到ajax网址
$.ajax ({
type:'post',
url:"domainPath",
success: function(response) {
if(!response.trim()) { ....
答案 2 :(得分:0)
如果$funding
不等于“自我资助”和“雇主资助”,则$query
将为空。
否则$query
不能为空。
答案 3 :(得分:0)
尝试此解决方案(在$ ErrorsRow中,您可以解决所有表单错误):
if(isset($_POST['submit_button'])){
$Data = $_POST;
foreach ($Data as $key => $value) {
if(!empty($value)) {
$DataRow[$key] = htmlspecialchars(trim($value));
} else {
$ErrorsRow[] = 'Empty field '. $key;
}
}
if($DataRow && !isset($ErrorsRow)){
$firstname = $DataRow['fname'];
$lastname = $DataRow['lname'];
$worktel = $DataRow['worktel'];
$funding = $DataRow['funding'];
$level = $DataRow['level'];
$dbc = mysqli_connect('localhost', 'xxxxx', '<xxxx>', 'xxxx')
or die ('Could not connect to MySQL server.');
if ($level != "IOSH Managing Safely"){
if ($funding == "Self Funding"){
$query = "INSERT INTO enrolments (fname, lname, worktel)" .
"VALUES ('$firstname', '$lastname', '$worktel')";
}
else if ($funding == "Employer Funding"){
$query = "INSERT INTO enrolments (fname, lname, worktel)" .
"VALUES ('$firstname', '$lastname', '$worktel')";
}
}
else if ($level == "IOSH Managing Safely"){
if ($funding == "Self Funding"){
$query = "INSERT INTO enrolments (fname, lname, worktel)" .
"VALUES ('$firstname', '$lastname', '$worktel')";
}
else if ($funding == "Employer Funding"){
$query = "INSERT INTO enrolments (fname, lname, worktel)" .
"VALUES ('$firstname', '$lastname', '$worktel')";
}
}
if(isset($query)) {
$result = mysqli_query($dbc, $query) or die ('error querying database');
mysqli_close($dbc);
}
} else {
if(isset($ErrorsRow) && !empty(implode($ErrorsRow))){
$Errors = implode(',', $ErrorsRow);
echo 'some error message'. $Errors;
}
}
}