一段调用ajax及PHP向数据库写入数据的代码,写入数据总是不成功 T_T

RT,整个project大意就是让用户回答一些问题,回答完所有问题之后就将他们的回答存入数据库。但是现在运行完程序后,数据库里却没有写入任何数据。我也没看到报错信息。试着... RT,整个project大意就是让用户回答一些问题,回答完所有问题之后就将他们的回答存入数据库。 但是现在运行完程序后,数据库里却没有写入任何数据。我也没看到报错信息。试着修改很多次了,一直不知道哪里有问题。我刚学这个不久,代码都是网上搜来然后修改的,求大神们帮忙解答 :-) 下面是相关代码

下面是回答完所有问题后,调用submit()函数。arr[][]数组里存储的是所有回答,确认过没有问题。数据库里有个test数据库,里面有个叫temp的表。表里三个字段设置的都是text格式

var xmlhttp;
var serverPage = "file.php";
function submit() {
// 将创建XMLHttpRequest对象写到getxmlhttp()函数中,并获取该对象
xmlhttp = getxmlhttp();
xmlhttp.open("POST", serverPage, true);
// 创建POST请求
xmlhttp.setRequestHeader("Content-Type",
"application/x-www-form-urlencoded; charset=GB2312");
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
obj.innerHTML = xmlhttp.responseText;
}
}
for (i = 0; i < numOfPairs; i++) {
var temp1, temp2, temp3;
temp1 = arr[i][0];
temp2 = arr[i][1];
temp3 = arr[i][2];
xmlhttp.send(temp1 + temp2 + temp3);
console.log('Send data through xmlhttp: ' + temp1 + temp2 + temp3);
}
}
// Function to create an XMLHttp Object.
function getxmlhttp() {
var xmlhttp = false;
// Check if we are using internet explorer.
try {
// If the javascript version is greater than 5.
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
// If not, then use the older active x object.
try {
// If we are using internet explorer.
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
// Else we must be using a non-internet explorer browser.
xmlhttp = false;
}
}
// If we are using a non-internet explorer browser, create a javascript
// instance of the object.
if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
xmlhttp = new XMLHttpRequest();
}
return xmlhttp;
}

下面是file.php文件以及dbconnector.php文件

<?php
//file.php
require_once ("dbconnector.php");

opendatabase();
$temp1 = $_POST["temp1"];
$temp2 = $_POST["temp2"];
$temp3 = $_POST["temp3"];
$myquery = "INSERT INTO temp (font1, font2, similarity)
VALUES ('$temp1','$temp2','$temp3')";
//执行SQL语句
if (!mysql_query($myquery)) {
header("Location: theform.php?message=There was a problem with the entry.");
exit;
}
//返回成功信息
header("Location: theform.php?message=Success");
?>

<?php
//dbconnector.php
//Define the mysql connection variables.
define ("MYSQLHOST", "localhost");
define ("MYSQLUSER", "root");
define ("MYSQLPASS", "123");
define ("MYSQLDB", "test");
function opendatabase(){
$db = mysql_connect (MYSQLHOST,MYSQLUSER,MYSQLPASS);
try {
if (!$db){
$exceptionstring = "Error connection to database: <br />";
$exceptionstring .= mysql_errno() . ": " . mysql_error();
throw new exception ($exceptionstring);
} else {
mysql_select_db (MYSQLDB,$db);
}
return $db;
} catch (exception $e) {
echo $e->getmessage();
die();
}
}
?>
展开
 我来答
windblast
2013-04-07 · 知道合伙人软件行家
windblast
知道合伙人软件行家
采纳数:5633 获赞数:13609
毕业于空军第一航空学院电子专业,1991年开始接触电脑,从事多年计算机编程,具有较丰富的经验。

向TA提问 私信TA
展开全部
看了下代码,感觉不是很全,至少没见到调用ajax的代码。

据我的经验,通常你可以用firebug来调试下,通过调试过程中的返回信息来判断问题是出在哪个位置,或者也可以把相对完整的代码放上来,这样调试起来也方便。
追问
就是用的firebug来调试的,但是没有报错信息。。就是执行到submit()函数的console.log('Send data through xmlhttp: ' + temp1 + temp2 + temp3);  这里,这个能正常执行完,然后就没别的信息了。 我也不知道到底哪里出了错  写入数据库相关的别的函数基本没了啊。就是在每回答一个问题之后,我都会用getValue()把回答存在arr[][] 这个数组中。然后回答完所有问题之后,我用"submit();"这句子调用了submit()函数,没别的了。
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式