通过Ajax提交表单时获取空查​​询PHP错误

时间:2012-10-17 10:49:22

标签: php ajax

提交表单时(通过Ajax),我收到以下错误消息:

[17-Oct-2012 11:46:29] PHP Warning:  mysqli_query() [<a href='function.mysqli-query'>function.mysqli-query</a>]: Empty query in /home1/xenongro/public_html/testing/enrolment/thanks.php on line 32

我怀疑它与if / else语句有关,但不确定实际问题是什么。

有人可以帮忙吗?

<?php

$firstname = htmlspecialchars(trim($_POST['fname']));
$lastname = htmlspecialchars(trim($_POST['lname']));
$worktel = htmlspecialchars(trim($_POST['worktel']));

$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);

?>

3 个答案:

答案 0 :(得分:0)

var_dump($query);
var_dump($funding);

之前

$result = mysqli_query($dbc, $query);

它会为您提供更多信息

我怀疑$funding可能会对您的常量字符串有轻微的变化

可能是错字/额外空格/大写字母

答案 1 :(得分:0)

有两种情况没有设置查询: 级别与字符串不匹配,或者资金与字符串不匹配。 这可能是空间的问题。

更糟糕的是,你不使用mysql_real_escape_string,除非启用magic_quotes_gpc,否则攻击者可以注入他的SQL。

答案 2 :(得分:0)

$funding似乎未在提供的代码示例中定义,因此您的if都不会匹配。