我有一个我创建的批准/拒绝用户系统,我遇到了它的问题。
我首先必须批准或拒绝用户。如果该用户获得批准,则会将其名称放入名为“已批准用户”的页面的其他部分。在此区域内,我更改了用户的权限/组级别。但是,如果我在批准的用户部分中有多个用户,如果我尝试更新用户,则无论我尝试更改哪个用户,它都会更新显示的最后一条记录。
例如,如果我有这个并试图更新Rick的记录,Bob会更新。 Bob的记录将是我数据库中的最后一个记录。
-Bob
-Tony
-Rick
每当我批准记录时,正确的用户记录都会被更改。只有当我尝试更改组/权限部分时,它才能正常工作。
从这里开始它有效..
$con = mysqli_connect("localhost", "", "", "");
$run = mysqli_query($con,"SELECT * FROM user_requests ORDER BY id DESC");
$numrows = mysqli_num_rows($run);
if( $numrows ) {
while($row = mysqli_fetch_assoc($run)){
if($row['status'] == "Pending"){
$pending_id = $row['id'];
$pending_user_id = $row['user_id'];
$pending_firstname = $row['firstname'];
$pending_lastname = $row['lastname'];
$pending_username = $row['username'];
$pending_email = $row['email'];
?>
<form action="" method="POST" id="status">
<input type='hidden' name='id' value='<?php echo $pending_id; ?>' id='pending_id'/>
<?php
if ($pending_firstname == true) {
echo "Name - ". $pending_firstname . " " . $pending_lastname . "</br>" .
"Username - ". $pending_username . "</br></br>"
//echo print_r($_POST);
?>
<button class="approve" type="submit" form="status" name="approve" value="<?=$pending_id;?>">Approve</button>
<button class="deny" type="submit" form="status" name="deny" value="<?=$pending_id;?>">Deny</button>
</form><br><br><br>
<?php
;} else {
echo "There are no Pending Requests at this time.";
}
}
}
}
?>
<hr><br>
待处理用户结束,到目前为止,它可以正常运行并使用被点击的正确用户。现在,此时当我尝试更新用户的信息时,它更新了显示的第一个用户......
<h2>Approved User Requests</h2><br>
<div id="success" style="color: red;"></div><br>
<?php
$con2 = mysqli_connect("localhost", "", "", "");
$run2 = mysqli_query($con2,"SELECT * FROM user_requests ORDER BY id DESC");
$runUsers2 = mysqli_query($con2,"SELECT * FROM users ORDER BY id DESC");
$numrows2 = mysqli_num_rows($run2);
if( $numrows2 ) {
while($row2 = mysqli_fetch_assoc($run2)){
if($row2['status'] == "Approved"){
//var_dump ($row2);
$approved_id = $row2['user_id'];
$approved_firstname = $row2['firstname'];
$approved_lastname = $row2['lastname'];
$approved_username = $row2['username'];
$approved_email = $row2['email'];
if ($approved_firstname == true) {
echo "Name - ". $approved_firstname . " " . $approved_lastname . "</br>" .
"Username - ". $approved_username . "</br></br>"
?>
<div class="change_group_button">
<a class="change_group" href="javascript:void(0)">Change User Permission</a>
</div><br>
<div id="light" class="change_group_popup">
<a class="close" href="javascript:void(0)">Close</a>
<div class="group_success" style="color: red;"></div><br>
<form id="update_group" action="" method="POST" accept-charset="utf-8">
<div class="field">
<label for="group">Group</label>
<input type="hidden" value="<?php echo $approved_id; ?>" id="approved_id" name="id" />
<input type="hidden" value="<?php echo $approved_firstname; ?>" id="approved_firstname" name="firstname" />
<input type="hidden" value="<?php echo $approved_lastname; ?>" id="approved_lastname" name="lastname" />
<input type="hidden" value="<?php echo $approved_username; ?>" id="approved_username" name="username" />
<input type="hidden" value="<?php echo $approved_email; ?>" id="approved_email" name="email" />
<select id='group_id' name='group' required>
<option value=''><?php echo htmlentities($group); ?></option>
<option value="1">Bench</option>
<option value="2">Spectator</option>
<option value="3">Team Member</option>
<option value="4">Commissioner</option>
</select>
</div>
<input type="submit" value="submit" name="group">
</form>
我用来更新信息的AJAX ..
//AJAX call for Approving the status
$(document).ready(function () {
$('.approve').click(function () {
$.ajax({
url: 'userRequest_approve.php',
type: 'POST',
data: {
id: $(this).val(), //id
status: 'Approved' //status
},
success: function (data) {
//do something with the data that got returned
$("#success").fadeIn();
$("#success").show();
$('#success').html('User Status Changed!');
$('#success').delay(5000).fadeOut(400);
},
//type: 'POST'
});
return false;
});
});
//AJAX call for updating the group
$(document).ready(function () {
$('#update_group').on('submit', function (event) {
event.preventDefault();
$.ajax({
url: 'user_group_update.php',
type: 'POST',
data: {
id: $("#approved_id").val(), //id
firstname: $("#approved_firstname").val(), //firstname
lastname: $("#approved_lastname").val(), //lastname
username: $("#approved_username").val(), //username
email: $("#approved_email").val(), //email
// update_group: $("#group_id").val() //group level
update_group: $(this).find( "#group_id option:selected" ).val()
},
success: function (data) {
//do something with the data that got returned
$(".group_success").fadeIn();
$(".group_success").show();
$('.group_success').html('User Permission Level Changed!');
$('.group_success').delay(5000).fadeOut(400);
alert(data);
},
error: function(jqXHR, textStatus,errorThrown )
{
// alert on an http error
alert( textStatus + errorThrown );
}
});
return false;
});
});
我相信整个问题与id有关,但我不知道我得到的id有什么问题。有没有人看到我做错了什么导致了这个?
答案 0 :(得分:0)
在代码的第一部分中,我发现了一些语法错误。更正:
<?php
$con = mysqli_connect("localhost", "", "", "");
$run = mysqli_query($con,"SELECT * FROM user_requests ORDER BY id DESC");
$numrows = mysqli_num_rows($run);
if( $numrows ) {
while($row = mysqli_fetch_assoc($run)){
if($row['status'] == "Pending"){
$pending_id = $row['id'];
$pending_user_id = $row['user_id'];
$pending_firstname = $row['firstname'];
$pending_lastname = $row['lastname'];
$pending_username = $row['username'];
$pending_email = $row['email'];
echo "<form action='' method='POST' id='status'>\r\n";
echo "<input type='hidden' name='id' value='$pending_id' id='pending_id' />\r\n";
if ($pending_firstname == true) {
echo "Name - $pending_firstname $pending_lastname</br>\r\n";
echo "Username - $pending_username</br></br>\r\n";
echo "<button class='approve' type='submit' form='status' name='approve' value='$pending_id'>Approve</button>\r\n";
echo "<button class='deny' type='submit' form='status' name='deny' value='$pending_id'>Deny</button>\r\n";
echo "</form><br><br><br>\r\n";
} else {
echo "There are no Pending Requests at this time.";
}
}
}
}
?>
其次,我担心你的双重查询。我建议运行查询,迭代结果,然后运行第二个查询。每次查询后也应该释放结果只是为了安全起见。如果它在同一页面中,我也不会创建新连接。
如果您可以在控制台上发布任何错误或结果,可以扩展我的答案。