将php表单链接到数据库

时间:2014-01-09 00:57:58

标签: php mysql sql forms hyperlink

尝试将我的php表单链接到我的数据库,但它无法正常工作。任何人都可以找到错误吗?它一直在代码的'INSERT INTO'部分给出一个解析错误。

<?php
$link = mysql_connect($localhost,$root,$xxxxxxx);

if (!$link) {
die("Could not connect");
};

print "Connected to the database server";

$db_selected = mysql_select_db($module_evaluation_results, $link);
if (!$db_selected) {
die("Could not use the database");
};

print "selected a DB";

$result = mysql_query (INSERT INTO module_evaluation_results 
(faculty, date, modulecode, moduletitle, school, modulebookcontent, moduleorganisation, lrcmaterials, moduledifficulty. modulesimilarity, 
contentinteresting, previousknowledge, understoodassessmentrequirements, assessmentmethod, 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, $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);
?>

2 个答案:

答案 0 :(得分:1)

而不是

moduledifficulty. modulesimilarity

使用

moduledifficulty, modulesimilarity

答案 1 :(得分:0)

为什么查询中没有引号?复制粘贴问题?或者是错误?

$result = mysql_query ("INSERT INTO module_evaluation_results 
(faculty, date, modulecode, moduletitle, school, modulebookcontent, moduleorganisation, lrcmaterials, moduledifficulty. modulesimilarity, 
contentinteresting, previousknowledge, understoodassessmentrequirements, assessmentmethod, 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, $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)");

修订回答:)

这个函数在php.net上由ddlshack在http://au2.php.net/mysql_query我在本地进行了测试,并进行了一些更改,以反映你的用例及其工作正常也许它将有助于简化你的插入调用

function mysql_insert($table, $inserts) 
{
    $values = array_map('mysql_real_escape_string', array_values($inserts));
    $keys = array_keys($inserts);

    return mysql_query('INSERT INTO `'.$table.'` (`'.implode('`,`', $keys).'`) VALUES (\''.implode('\',\'', $values).'\')');
}

像这样使用;

 mysql_insert(
        'module_evaluation_results', array(
        'col1' => rand(10,100),
        'col2' => 'string1',
        'col3' => 'string2',
        'col4' => rand(10,100),
        'col5' => 'string3',
    )); 

所以mysql_insert(表名,数组(列名,值));

我的完整php测试文件如下

$link = mysql_connect('127.0.0.1:3306', 'test-user', 'test-pass');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';

$db_selected = mysql_select_db("test", $link);
if (!$db_selected) 
{
    die("Could not use the database");
};

mysql_insert(
    'module_evaluation_results', array(
    'col1' => rand(10,100),
    'col2' => 'string1',
    'col3' => 'string2',
    'col4' => rand(10,100),
    'col5' => 'string3',
)); 

mysql_close($link);

function mysql_insert($table, $inserts) 
{
    $values = array_map('mysql_real_escape_string', array_values($inserts));
    $keys = array_keys($inserts);

    return mysql_query('INSERT INTO `'.$table.'` (`'.implode('`,`', $keys).'`) VALUES (\''.implode('\',\'', $values).'\')');
}

我认为问题可能是你的表名?加上写入的查询的复杂性可能是一些错别字 但是这个代码肯定可以工作,插入可以多次运行,如果有帮助,可以更容易阅读和编码