我刚刚在本地主机上完成了一个表单,它工作得很好,但是当我将代码上传到网络主机时,它并没有按预期工作。它是一个使用 PHP、MySQL 和 JQuery 的简单形式。当我单击“提交”时,它会显示我的自定义错误窗口,提示电子邮件已注册,但问题是数据库为空。我在 ajax 部分中放入了一些代码来检查电子邮件,这就是它的内容(它应该只显示一个数字):
警告 :mysql_fetch_assoc():提供的参数不是 /*/*/public_html/check_user.php 中的有效 MySQL 结果资源 第 17 行
这是 check_user.php:
<?php
require_once("SqlChromoConnection.php");
// Check if email is not empty in the form
if(isset($_POST['email'])) {
// create the query
$sql = "SELECT COUNT(*) AS count
FROM Users
WHERE email = '" . trim($_POST['email']) . "'";
// create object to handle the connection
$conn = new SqlChromoConnection();
$conn->getDatabaseConnection(); // establish a connection
$data = $conn->executeQuery($sql); // execute query
$count= mysql_fetch_assoc($data); // save result in $count
$exists = $count['count']; // access only the field 'count'
$conn->closeConnection(); // close the connection
echo $exists;
exit(0);
}
?>
这是检查电子邮件的 ajax 部分:
if( !re.test(email) || email.indexOf(' ') > 0) {
message = "Email NOT valid!!!";
messageDialog("Warning", message, "warning", 2);
return false;
} else {
// use ajax to check if a user has been previously registered
// using this email
var valid = false;
$.ajax(
{
url:"check_user.php", // url that will use
async: false,
data:{ // data that will be sent
email:email
},
type:"POST", // type of submision
dataType:"text", // what type of data we'll get back
success:function(data)
{
window.alert(data);
// if check_user returns 0
// means that there's no any user registered with that email
if(data == 0 ) {
valid = true;
}
}
});
if(!valid) {
message = "This email is registered already!";
messageDialog("Error", message, "error", 2);
return false;
}else return true;
}
正如我所说,代码在本地主机中运行良好。
如有任何建议,我们将不胜感激。
问候。
最佳答案
在mysql_fetch_assoc之前添加@,它会删除来自此语句的所有警告。 使用类似@mysql_fetch_assoc()。
关于php - Web 主机上出现未知错误,但代码在本地主机上运行良好,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20550090/