【问题标题】:Trouble linking php to database [duplicate]无法将php链接到数据库[重复]
【发布时间】: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'末尾加分号

标签: php mysql sql database


【解决方案1】:

几个cmets:

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

【讨论】:

  • 什么是 register_globals?对不起。
  • @taniakeira 使用register_globals,您可以像$faculty 一样访问已发布的变量,例如$_POST['faculty']$_GET['faculty'],参见php.net/manual/en/security.globals.php
【解决方案2】:

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

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

如您所知,您必须像这样分隔变量:“.$variable”。

【讨论】:

  • 你可以在带有双引号的字符串中使用 vars 完美地尝试一下:echo "Hello $name";
  • 但我认为习惯是愚蠢的。最好像我展示的那样学习。以后单 qoutes 就没有问题了。
  • 这将是您的意见,在复杂的语句中,当您使用双引号时它会提高可读性
  • 请在您的代码示例中将后面的 qoutes 更改为单个 qoutes:'".$dbDatabasename."'
  • 您不需要在值中使用反引号,但最好与表名一起使用。
【解决方案3】:

修复了 $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);
    ?>

【讨论】:

    猜你喜欢
    • 2016-07-30
    • 1970-01-01
    • 2018-10-22
    • 2020-04-15
    • 2014-01-27
    • 1970-01-01
    • 2018-08-01
    • 2017-07-02
    • 2013-12-14
    相关资源
    最近更新 更多