查询失败的原因

时间:2013-09-02 15:09:02

标签: php mysql sql mysqli

我想知道为什么我的查询在运行这个php脚本时失败了。错误是:致命错误:查询失败! SQL: - 错误:在第19行的/home/eland/u5/kbecs/w1268094/public_html/UWSU-Debating-Portal/admin/inventory.php中

这是第19行的代码:

$question = mysqli_real_escape_string($link,$_POST['question']);
$venue = mysqli_real_escape_string($link,$_POST['venue']);
$date = mysqli_real_escape_string($link,$_POST['questiondate']);
//See if question is identical to another question in the table
$sql = mysqli_query($link,"SELECT qQuestionNo FROM DebateQuestion WHERE question='$question'LIMIT 1");
$questionMatch = mysqli_num_rows($link, $sql) or trigger_error("Query Failed! SQL: $sql - Error: ".mysqli_error(db_conx), E_USER_ERROR);; //count the output amount
if ($questionMatch>0){
echo 'Sorry you tried to place a duplicate "Question" into the table, <a href="inventory.php">Click here</a>';
exit();
}
//Add the question to the database 
$sql = mysqli_query($link,"INSERT INTO DebateQuestion (qQuestion, qDebateVenue, qDate)
VALUES($link,'$question','$venue','$date'") or die (mysqli_error());
$qid = mysqli_insert_id();
header("Location: inventory.php");
exit();
}

4 个答案:

答案 0 :(得分:0)

尝试在LIMIT之前放置空格

$sql = mysqli_query($link,"SELECT qQuestionNo FROM DebateQuestion WHERE question='$question'LIMIT 1");

答案 1 :(得分:0)

mysqli_num_rows只接受一个结果对象。尝试:

$questionMatch = mysqli_num_rows($sql) or trigger_error("Query Failed! SQL: $sql - Error: ".mysqli_error(db_conx), E_USER_ERROR); //count the output amount

答案 2 :(得分:0)

由于代码非常不一致和不整齐,您的查询失败了。由于缺少错误报告,您根本不知道。

error_reporting(E_ALL);
ini_set('display_errors',1);
代码顶部的

将有助于揭示您犯下的最愚蠢的错误。

db_conx一样,这是一个无处定义的奇怪常数 并且$sql变量包含除SQL代码之外的任何内容。

$sql = "SELECT qQuestionNo FROM DebateQuestion WHERE question='$question' LIMIT 1";
$res = mysqli_query($link, $sql) or trigger_error(mysqli_error($link)." [$sql]");
if (mysqli_num_rows($link)){
    echo 'Sorry you tried to place a duplicate blah blah';
    exit();
}

它必须至少

答案 3 :(得分:-1)

尝试在LIMIT关键字之前添加空格:

$sql = mysqli_query($link,"SELECT qQuestionNo FROM DebateQuestion WHERE question='".$question."' LIMIT 1");
相关问题