尝试将我的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); ?>
为什么查询中没有报价?复制并粘贴问题?还是那是错误?
IE
$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)");
这个函数是ddlshack在php.net上的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).'\')'); }
我认为问题可能出在您的表格名称上?加上写的查询的复杂性可能是一些错字,但是此代码一定可以多次执行插入操作,并且如果有帮助,则更易于阅读和编写代码