0

我想知道为什么每次运行这个 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

4 回答 4

0

由于非常不一致和不整洁的代码,您的查询失败了。由于缺乏错误报告,您对此一无所知。

error_reporting(E_ALL);
ini_set('display_errors',1);

在你的代码顶部将有助于揭示你犯的最愚蠢的错误。

就像 with db_conxwhich 是一个奇怪的常量,在任何地方都没有定义
,而$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();
}

至少必须是

于 2013-09-02T15:20:39.253 回答
0

尝试在输入之前放置LIMIT空间

$sql = mysqli_query($link,"SELECT qQuestionNo FROM DebateQuestion WHERE question='$question'LIMIT 1");
于 2013-09-02T15:12:56.413 回答
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
于 2013-09-02T15:16:03.637 回答
-1

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

$sql = mysqli_query($link,"SELECT qQuestionNo FROM DebateQuestion WHERE question='".$question."' LIMIT 1");
于 2013-09-02T15:14:32.307 回答