新手,我在寫(xiě)一個(gè)注冊(cè)功能。通過(guò)ajax、php、mysql實(shí)現(xiàn)注冊(cè)功能。我用chrome的調(diào)試工具看了一下,當(dāng)我輸入已有的用戶(hù)名時(shí)能成功返回 "注冊(cè)成功!" ,當(dāng)我輸入新用戶(hù)名密碼時(shí)echo $res_insert的值是0,返回"系統(tǒng)繁忙,請(qǐng)稍候" ,求大神幫忙,感謝??!js部分:
$(function(){
$("#regbutton").click(function(){
$.ajax({
type:"post",
url:"register.php",
dataType:"json",
data:{username:$("#usernamereg").val(),password:$("#passwordreg").val()},
success:function(feedbackdata){
$("#registerstatus").html(feedbackdata);
}
})
})
});
php部分:
<?php
$username = $_POST["username"];
$password = $_POST["password"];
$servername = "localhost";
$sqlusername = "root";
$sqlpassword = "root";
$dbname = "guitartabs";
// 創(chuàng)建連接
$conn = new mysqli($servername, $sqlusername, $sqlpassword, $dbname);
if ($conn->connect_error) {
die("連接失敗: " . $conn->connect_error);
}
$sql = "SELECT username FROM users where username = '$username'";
$result = $conn->query($sql);
$num = mysqli_num_rows($result);
if($num){
echo "用戶(hù)名已存在";
}
else //不存在當(dāng)前注冊(cè)用戶(hù)名稱(chēng)
{
$sql_insert = "insert into users (`username`,`password`) values ('".$username."','".$password."')";
$res_insert = $conn->query($sql_insert);
if($res_insert)
{
echo "注冊(cè)成功!";
}
else
{
echo "系統(tǒng)繁忙,請(qǐng)稍候";
}
}
?>
2 回答

慕絲7291255
TA貢獻(xiàn)1859條經(jīng)驗(yàn) 獲得超6個(gè)贊
$sql_insert = "insert into users (`username`,`password`) values ('".$username."','".$password."')";
$res_insert = $conn->query($sql_insert);
if($res_insert)
$res_insert 這個(gè)返回值不是id吧
應(yīng)該用這個(gè)方法 mysql_insert_id
- 2 回答
- 0 關(guān)注
- 319 瀏覽
添加回答
舉報(bào)
0/150
提交
取消