您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

将php表单链接到数据库

将php表单链接到数据库

为什么查询中没有报价?复制并粘贴问题?还是那是错误

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).'\')');
}

我认为问题可能出在您的表格名称上?加上写的查询的复杂性可能是一些错字,但是此代码一定可以多次执行插入操作,并且如果有帮助,则更易于阅读和编写代码

php 2022/1/1 18:43:55 有307人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶