AJAX - 检查重复数据并发送通知
Posted
技术标签:
【中文标题】AJAX - 检查重复数据并发送通知【英文标题】:AJAX - check duplicate data and send notification 【发布时间】:2016-08-10 02:25:33 【问题描述】:我尝试从数据库中检查重复的“用户名”,如果用户名存在,我想使用 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');
?>
【问题讨论】:
$username 永远不会是 0,您可能对 num_rows 感兴趣。另外,不推荐使用 mysql,使用 PDO 只需使用select * from users where username = '$username'
和if(count($row) <= 0)
你应该首先对包含 user_name 的列应用唯一 id 约束
那么mysql永远不会允许列中的重复值超过你可以处理重复值的错误
【参考方案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
【讨论】:
比我的回答好得多!! @festvender,请选择此答案作为解决方案,因为它得到了很好的解释并解决了您的问题。【参考方案2】:您只需要检查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_*
。
【讨论】:
【参考方案3】:我认为您需要在提交后显示错误消息。所以首先您需要在 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_*。
【讨论】:
按钮不起作用。我用这部分替换了你的代码$.ajax( type: "POST", url: "save_user.php", data: formData, success: function(html) $.jGrowl("User Successfully Added", header: 'User Added' ); window.location = 'admin_user.php'; );
按钮仍然无法使用。如果我错了请纠正,我只是用这部分替换了你的jquery代码,其他的还是一样$.ajax( type: "POST", url: "save_user.php", data: formData, success: function(html) $.jGrowl("User Successfully Added", header: 'User Added' ); window.location = 'admin_user.php'; );
对你更好,我昨天有点忙,对不起以上是关于AJAX - 检查重复数据并发送通知的主要内容,如果未能解决你的问题,请参考以下文章