问题是:始终获取错误用户已经存在,并且没有插入数据库。我的目标是在不离开主页的情况下注册用户,并使用ajax请求进行数据库插入。我还希望用户保持离线状态。
数据库结构(ID autoident,用户名唯一,电子邮件唯一,密码,状态枚举(活动,非活动))
我的代码: register.php
<?php
require_once 'config.php';
session_start();
if(isset($_POST['username']) && isset($_POST['password']) && isset($_POST['email'])) {
$username=mysqli_real_escape_string($con,$_POST['username']);
$email=mysqli_real_escape_string($con,$_POST['email']);
$password=md5(mysqli_real_escape_string($con,$_POST['password']));
$result="INSERT INTO users VALUES '', '$username', '$email', '$password', 'inactive'";
$res=mysqli_query($con, $result);
if(mysqli_error($con, $result))
echo mysqli_error($con, $result);
else
echo "Inserted Successfully";
}
?>
config.php用于数据库连接
scripts.js中
$(document).ready(function() {
$('#register').click(function() {
var help = true;
var username=$("#user2").val();
var password=$("#pass21").val();
var password2=$("#pass22").val();
if (password != password2) {
$("#error2").html("<span style='color:#cc0000'>Error:</span> Password mismatch! ");
help = false;
}
var email=$("#email").val();
if( !validateEmail(email)) {
$("#error2").html("<span style='color:#cc0000'>Error:</span> Email not correct!");
help = false;
}
var dataString = 'username='+username+'&password='+password+'&email'+email;
if($.trim(username).length>0 && $.trim(password).length>0 && help==true) {
$.ajax({
type: "POST",
url: "core/register.php",
data: dataString,
cache: false,
beforeSend: function(){ $("#register").val('Connecting...');},
success: function(data){
if(data="Inserted Successfully") {
$("#register").val('Register')
$("#error2").html("<span style='color:#cc0000'>Error:</span> Success");
} else {
$("#register").val('Register')
$("#error2").html("<span style='color:#cc0000'>Error:</span> Username or email already exists");
}
}
});
}
return false;
});
});
function validateEmail($email) {
var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
return emailReg.test( $email );
}
的index.php
<div class="formstyle-r register">
<form action="" method="post">
<h2>Register</h2>
<h4>Username:</h4>
<input type="text" name="user2" id="user2">
<h4>Password:</h4>
<input type="password" name="pass21" id="pass21">
<h4>Confirm password:</h4>
<input type="password" name="pass22" id="pass22">
<h4>E-mail:</h4>
<input type="text" name="email" id="email"> <br>
<input type="submit" name="register" id="register" class="custombtn1" value="Register">
<div class="err" id="error2"></div>
</form>
</div>
此代码始终存在错误已存在。永远不要插入数据库。我错过了什么?
答案 0 :(得分:1)
如果ID设置为AUTO INCREMENT
,则无需在INSERT
声明
INSERT INTO users (username, email, password, status) VALUES ('$username', '$email', '$password', 'inactive');
此外,为了改善有效/无效用户的搜索,您可以考虑将有效列更改为TINYINT(1)
,并使用0
或1
代替文字&#39;有效&#39 ; /&#39;不活动的&#39;
答案 1 :(得分:1)
你错过了查询中的大括号......
$result="INSERT INTO users VALUES('', '$username', '$email', '$password', 'inactive')";
执行此操作并且它可以正常工作...有关将值插入数据库的更多详细信息,请参阅此链接。 http://dev.mysql.com/doc/refman/5.6/en/insert.html
答案 2 :(得分:0)
更改
$result="INSERT INTO users VALUES '', '$username', '$email', '$password', 'inactive'";
到
$result="INSERT INTO users (username, email, password, status) VALUES ('".$username."', '".$email."', '".$password."', 'inactive')";
答案 3 :(得分:0)
当您调用mysql_real_escape_string()时,看起来您的参数顺序错误
$username=mysqli_real_escape_string($con,$_POST['username']);
should be
$username=mysqli_real_escape_string($_POST['username'],$con);