我正在使用jquery mobile并制作一个简单的注册页面并尝试使用ajax提交表单
我有以下脚本
<script>
$(document).ready(function(){
$('#submit').click(function(){
var formData = $("#registrationform").serialize();
$Ajax({
type:"POST",
url:'register.php',
data:formData,
success:function(data)
{
alert('succedded')
$('#notification').text(data);
},
error:function(data)
{
alert('failed')
$('#notification').text(data);
}
});
});
});
请告诉我通知div中没有显示错误的地方,数据库中保存的数据是什么错误
我的register.php看起来像这样
$Name = $_POST[username];
$password = $_POST[password];
$email=$_POST[Email];
$con= mysql_connect('localhost','root','');
mysql_select_db('mobileblog',$con);
$sql="select * from users where Name like '$Name'";
$result= mysql_db_query($sql, $connection);
$row= mysql_fetch_row($result);
if($row)
{
echo 'username already exist,try another name' ;
}
else
{
$sql="insert into users (Name,password,email) values('$Name','$password','$email')";
$retval=mysql_query($sql, $connection);
if(! $retval)
{
echo 'error:try later';
}
else
{
echo 'You have been registered successfuly';
}
}
答案 0 :(得分:0)
将 mysql_num_rows 用于计数行,也将$ connection替换为$ con
$Name = $_POST[username];
$password = $_POST[password];
$email=$_POST[Email];
$con= mysql_connect('localhost','root','');
mysql_select_db('mobileblog',$con);
$sql="select * from users where Name like '$Name'";
$result= mysql_db_query($sql, $con);
$row_count = mysql_num_rows($result);
if($row_count>0) {
echo 'username already exist,try another name' ;
} else {
$sql="insert into users (Name,password,email) values('$Name','$password','$email')";
$retval=mysql_query($sql, $con);
if(! $retval)
{
echo 'error:try later';
}
else
{
echo 'You have been registered successfuly';
}
}