問題是:總是出現錯誤用戶已經存在,並且沒有插入到數據庫中。我的目標是註冊用戶,不要離開主頁,並用ajax請求進行數據庫插入。另外我希望用戶保持離線狀態。MySQL查詢不顯示錯誤但不插入
數據庫結構(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-\.][email protected]([\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>
此代碼總是給出錯誤已存在。並且永遠不會插入數據庫。我錯過了什麼?
沒有成功,沒有錯誤,數據庫中沒有新行,可能是ajax的問題? – Riiwo
一旦給出'error_reporting(E_ALL);'並檢查是否有任何錯誤... – phpfresher