【发布时间】:2014-01-10 02:49:32
【问题描述】:
试图将我的 php 表单链接到我的数据库,但没有识别出这些值。一条错误消息说:
无效查询:您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 4 行的 '' 附近使用正确的语法。
它还表示所有值的未知变量。
谁能找出故障?
<?php
$dbAddress='localhost';
$dbUsername='root';
$dbPassword='xxxxxxxx';
$dbDatabasename='Studentanswers'
?>
<?php
$link = mysql_connect($dbAddress, $dbUsername, $dbPassword);
if (!$link) {
die("Could not connect");
};
print "Connected to the database server";
$db_selected = mysql_select_db($dbDatabasename, $link);
if (!$db_selected) {
die("Could not use the database");
};
print "selected a DB";
$result = mysql_query ("INSERT INTO $dbDatabasename (`faculty`, `date`, `modulecode`, `moduletitle`, `school`, `modulebookcontent`,
`moduleorganisation`, `lrcmaterials`, `moduledifficulty`, `modulesimilarity`, `contentinteresting`, `previousknowledge`,
`understoodassessmentrequirements`, `assessmentmethod`, `markedwork`, `moduleleader`, `ML_interestforsubject`, `ML_contentclear`,
`ML_appropriateteachingpace`, `ML_reachableforadvice`, `ML_helpfulfeedback`, `lecturer1`, `L1_interestforsubject`,
`L1_contentclear`, `L1_appropriateteachingpace`, `L1_reachableforadvice`, `L1_helpfulfeedback`, `lecturer2`, `L2_interestforsubject`, `L2_contentclear`,
`L2_appropriateteachingpace`, `L2_reachableforadvice`, `L2_helpfulfeedback`, `hoursofindependentstudy`, `overallattendance`,
`bestfeaturesofmodule`, `improvemodule`)
VALUES (`$faculty`, `$date`, `$modulecode`, `$moduletitle`, `$school`, `$modulebookcontent`, `$moduleorganisation`, `$lrcmaterials`, `$moduledifficulty`,
`$modulesimilarity`, `$contentinteresting`, `$previousknowledge`, `$understoodassessmentrequirements`, `$assessmentmethod`, `$markedwork', `$moduleleader`,
`$ML_interestforsubject`, `$ML_contentclear`, `$ML_appropriateteachingpace`, `$ML_reachableforadvice`, `$ML_helpfulfeedback`, `$lecturer1`, `$L1_interestforsubject`,
`$L1_contentclear`, `$L1_appropriateteachingpace`, `$L1_reachableforadvice`,`$L1_helpfulfeedback`, `$lecturer2`, `$L2_interestforsubject`, `$L2_contentclear`,
`$L2_appropriateteachingpace`, `$L2_reachableforadvice`, `$L2_helpfulfeedback`, `$hoursofindependentstudy`, `$overallattendance`, `$bestfeaturesofmodule`, `$improvemodule`)");
if (!$result) {
die('Invalid query: ' . mysql_error());
}
print "run a query against the DB";
mysql_close($link);
?>
【问题讨论】:
-
您从哪里获得 SQL 查询的这些值? 很有可能您忽略了清理用户输入,并且错误是无意中实现的 SQL 注入漏洞的结果。除此之外,至少将代码格式化为人类可读将有助于识别语法错误。
-
将反引号替换为 qoutes
-
PHP 变量
$faculty、$date、$modulecode等是否在 PHP 代码中的任何位置定义?如果它们没有被定义,它们就不会进入你的 SQL 查询。 -
旁注:
$dbDatabasename='Studentanswers'末尾加分号