将php表单链接到数据库 [英] Linking php form to database

查看:99
本文介绍了将php表单链接到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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


推荐答案

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

Why is there no quotations in your query? copy and paste issue? or is that the error?

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 我测试过的它本地有一些更改,以反映您的用例及其工作正常也许它将有助于简化您的插入调用

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

像这样使用;

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

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

So mysql_insert( Table name, array(column name, value));

我的完整php测试文件如下

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

这篇关于将php表单链接到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆