无法将php链接到数据库

时间:2014-01-09 18:40:18

标签: php mysql sql database

尝试将我的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);
?>

3 个答案:

答案 0 :(得分:3)

一些评论:

  1. 您需要使用单引号或双引号引用您的值(无反引号):VALUES ('$faculty', '$date' ...,
  2. 您应该切换到PDO或mysqli并准备好语句,因为mysql_*函数已被弃用,以避免潜在的SQL注入问题。
  3. 您的变量似乎未定义。如果您依赖register_globals,请不要,它是危险的并且已弃用。

答案 1 :(得分:1)

使用变量时,应该这样做:

"INSERT INTO `".$dbDatabasename."` ..
VALUES ('".$faculty."', '".$date."', ..

如你所知,你必须分开这样的变量:“。$ variable。”

答案 2 :(得分:0)

修正 $ markedwork

附近的输入错误
   <?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);
    ?>