我尝试检查重复的用户名'从数据库,我想发送消息回用户,如果用户名存在使用AJAX。到目前为止,它只插入数据并且验证仍然不起作用。欢迎任何帮助,谢谢!
已编辑:
我正在使用' $ .jcrowl'为了在插入数据时获得反馈(参考add_user.php),它将弹出反馈(例如:'用户成功添加')。那么如何申请这个在数据库中找到的重复用户名'进入这个$ .jcrowl,我需要如何从' save_user.php'发送验证。回到' add_user.php'?
add_user.php
<div class="row-fluid">
<!-- block -->
<div class="block">
<div class="navbar navbar-inner block-header">
<div class="muted pull-left"><i class="icon-plus-sign icon-large"></i> Add Admin User</div>
</div>
<div class="block-content collapse in">
<div class="span12">
<form method="post" id="add_user">
<label>First Name :</label>
<input class="input focused" name="firstname" id="focusedInput" type="text" placeholder = "Firstname" required>
<label>Last Name :</label>
<input class="input focused" name="lastname" id="focusedInput" type="text" placeholder = "Lastname" required>
<label>User Type :</label>
<select name="user_type" class="input focused" required/>
<option></option>
<?php $user_level=mysql_query("select * from user_level")or die(mysql_error());
while ($row=mysql_fetch_array($user_level)){
?>
<option value="<?php echo $row['user_type']; ?>"><?php echo $row['type_name']; ?></option>
<?php } ?>
</select>
<label>Username :</label>
<input class="input focused" name="username" id="focusedInput" type="text" placeholder = "Username" required>
<label>Password :</label>
<input class="input focused" name="password" id="focusedInput" type="password" placeholder = "Password" required>
<?php //if admin = 1 and if user = 2
//$session_id=$_SESSION['id'];
$run = $conn->query("select * from users where user_id = '$session_id'")or die(mysql_error());
$user_row = $run->fetch();
$user_type = $user_row['user_type'];
if ($user_type == 1) {
?>
<div class="control-group">
<div class="controls">
<button data-placement="right" title="Click to Save" id="save" name="save" class="btn btn-inverse"><i class="icon-save icon-large"></i> Save</button>
<script type="text/javascript">
$(document).ready(function(){
$('#save').tooltip('show');
$('#save').tooltip('hide');
});
</script>
</div>
</div>
<?php //not admin
}
else { ?>
<button data-placement="right" title="Click to Save" id="save" name="save" class="btn btn-inverse" disabled="disabled"><i class="icon-save icon-large"></i> Save</button> Only admin allowed!
<script type="text/javascript">
$(document).ready(function(){
$('#save').tooltip('show');
$('#save').tooltip('hide');
});
</script>
<?php }
?>
</form>
</div>
</div>
</div>
<!-- /block -->
</div>
<script>
jQuery(document).ready(function($){
$("#add_user").submit(function(e){
e.preventDefault();
var _this = $(e.target);
var formData = $(this).serialize();
$.ajax({
type: "POST",
url: "save_user.php",
data: formData,
success: function(html){
$.jGrowl("User Successfully Added", { header: 'User Added' });
window.location = 'admin_user.php';
}
});
});
});
</script>
save_user.php
<?php
include('dbcon.php');
include('session.php');
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$user_type = $_POST['user_type'];
$username = $_POST['username'];
$password = $_POST['password'];
$query = mysql_query("select * from users where username = '$username' and password = '$password' and firstname = '$firstname' and password = '$password'")or die(mysql_error());
$row = mysql_fetch_array($query);
$username = $row['username'];
if ($username == 0) {
{
$conn->query("insert into users (username,password,firstname,lastname,user_type) values('$username','$password','$firstname','$lastname','$user_type')")or die(mysql_error());
}
else
{
echo('USERNAME_EXISTS');
}
?>
答案 0 :(得分:1)
首先停止使用mysql_*
扩展名,在PHP 7中弃用并关闭。使用mysqli_*
或PDO
。
<强>解决方案:强>
您只需在查询中检查用户名,例如:
SELECT * FROM `users` WHERE `username` = '$username'
第二点是,您只需要使用count()
或num rows
函数来检查是否存在记录:
使用MYSQLi的示例:
$conn = mysqli_connect($dbhost, $dbuser, $dbpass, $db);
$sql = "SELECT * FROM `users` WHERE `username` = '$username'";
$query = mysqli_query($conn,$sql);
$row = mysqli_fetch_array($query);
if(count($row) <= 0){
//success stuff
}
else{
// error stuff
}
答案 1 :(得分:1)
您只需要检查username
而不是密码。
唯一性条件仅适用于username
。
修改查询:
$query = mysql_query("select * from users where username = '$username' and password = '$password' and firstname = '$firstname' and password = '$password'")or die(mysql_error());
要:
$query = mysql_query("select * from users where username = '$username' ")or die(mysql_error());
注意:不要使用mysql_*
个用户。它们是不推荐使用的,它们将在未来的PHP版本中删除。请改用PDO
或mysqli_*
。
答案 2 :(得分:0)
我认为您需要在提交后显示错误消息。首先,您需要在jquery中进行一些更改
$.ajax({
data: form_data,
url: "save_user.php",
method: "POST",
dataType: "JSON",
beforeSend: function () {
// show image if process
}
}).done(function (data) {
if (data.status === "success") {
$.jGrowl(data.message, { header: 'User Added' });
window.location = 'admin_user.php';
}
if (data.status === "failure") {
$.jGrowl(data.message, { header: 'Error Found' });
}
}).error(function () {
$.jGrowl("Some Error Found", { header: 'Error' });
}).complete(function () {
});
在你的save_user.php中 改变这些行
$query = mysql_query("select * from users where username = '$username'");
$number = mysql_num_rows($query);
if ($number == 0) {
$query2=mysql_query("insert into users (username,password,firstname,lastname,user_type) values
('$username','$password','$firstname','$lastname','$user_type')");
if($query2){
$data['status'] = "success";
$data['message'] = "User Created successfully.";
}else{
$data['status'] = "failure";
$data['message'] = "Error: " . mysql_error();
}
}
else
{
$data['status'] = "failure";
$data['message'] = "USERNAME_EXISTS";
}
echo json_encode($data);
注意:不要使用mysql_ *函数。它们是不推荐使用的,它们将在未来的PHP版本中删除。请改用PDO或mysqli_ *。