问题
Trying to link my php form to my database but it isn't working. Can anyone find a fault? It keeps giving me a parse error on the 'INSERT INTO' part of the code.
<?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);
?>
回答1:
Why is there no quotations in your query? copy and paste issue? or is that the error?
i.e.
$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)");
Revised answer :)
This function was on php.net by ddlshack at http://au2.php.net/mysql_query I've tested it locally with some changes to reflect your usecase and its working fine perhaps it will help simplify your insert call as well
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).'\')');
}
Used like so;
mysql_insert(
'module_evaluation_results', array(
'col1' => rand(10,100),
'col2' => 'string1',
'col3' => 'string2',
'col4' => rand(10,100),
'col5' => 'string3',
));
So mysql_insert( Table name, array(column name, value));
My full php test file is the following
$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).'\')');
}
I think the problem might be your table name? plus the complexity of the query as written might be some typos but this code definately works inserts can be run multiple times and is simpler to read and code if that helps
回答2:
instead of
moduledifficulty. modulesimilarity
use
moduledifficulty, modulesimilarity
来源:https://stackoverflow.com/questions/21009780/linking-php-form-to-database