php - 无法将 php 链接到数据库

标签 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);
?>

最佳答案

一些评论:

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

关于php - 无法将 php 链接到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21028486/

相关文章:

php - Laravel 服务容器是否可以绑定(bind)到类及其所有祖先 - 就像在 Symfony 中一样

php - 如何访问具有相同名称的不同表的多个mysql行

javascript - 配置 htaccess 以直接访问 CSS 和 JS 文件,但将所有其他请求重定向到索引文件

php - 从数千名可能有数百张彩票的用户中选择抽奖彩票获奖者

sql - Oracle SQL - 使用合并函数

php - Mysql结果限制输出

php - 为什么 LOCK TABLES [table] WRITE 不阻止表读取?

mysql - postgres - 修改 xml(MySql UpdateXml 替代)

javascript - 使用Javascript检查数据库中是否存在记录

php - 将 php 表单链接到数据库