我试图做我的第一次AJAX调用,我想要做的事很简单,但我的数据库没有被更新。使用特定ID更新数据库的AJAX调用
我想要做的就是当我点击用户旁边的接受按钮,他们的身份证件将被接收并以新状态“已接受”发送,状态从“待处理”更改为“已接受” '为我的user_requests数据库表中的特定用户。
数据库中没有任何内容正在改变,唯一发生在AJAX代码中的事情是我获得了我的#success消息,但可能是0.3秒,并且它不会淡出。
有没有人看到我在做什么错在我的尝试?
<h2>Pending User Requests</h2>
<br />
<div id="success" style="color: red;"></div>
<?php
$con = mysqli_connect("localhost", "root", "", "db");
$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)){
//comment added by php-dev : condition could be set in the query -->
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'];
?>
<!-- comment added by php-dev : useless form tag -->
<form action="" method="POST" id="status">
<!-- comment added by php-dev : useless input field, no field name -->
<input type='hidden' value='<?php echo $pending_id; ?>' id='pending_id' />
<?php
// comment added by php-dev : comparing string to boolean value true
if ($pending_firstname == true) {
echo "Name - ". $pending_firstname . " " . $pending_lastname . "</br>"
. "Username - ". $pending_username . "</br></br>"
?>
<!-- comment added by php-dev : conditional form closing tag -->
</form>
<button class="approve" type="submit" form="status" name="approve"
value="<?= $pending_id; ?>">
Approve
</button>
<button id="deny" type="submit" form="status" name="deny" value="Denied">
Deny
</button>
<br><br><br>
<?php
// comment added by php-dev : else statement misplaced -->
;} else {
echo "There are no Pending Requests at this time.";
}
}
}
}
?>
我的AJAX调用...
<script>
$(document).ready(function(){
$('.approve').click(function(){
$.ajax({
url: 'userRequest_approve.php',
data: {
id: $(this).val(), //the value of what you clicked on
//you clicked on it so you know the status might as well hardcode it
status: 'Approved'
},
success: function(data) {
//do something with the data that got returned
// comment added by php-dev : for debug purposes, the #success should show
// the server reponse instead
$('#success').html('User Status Changed!');
//do something with the data that got returned
$('#success').delay(5000).fadeOut(400);
},
type: 'POST'
});
});
});
</script>
我userRequest_approve.php文件插入到数据库更新状态...
<?php
require_once 'core/init.php';
$term = mysql_escape_string($term); // Attack Prevention
$pending_id = $_POST['id'];
$status = $_POST['approve'];
$con = mysqli_connect("localhost","root","","db");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$stmt = $con->prepare(
"INSERT INTO user_requests (status, date_responded) VALUES (?, NOW())"
);
if (false===$stmt) {
// Check Errors for prepare
die('User Request update prepare() failed: ' . htmlspecialchars($con->error));
}
$stmt->bind_param('s', $status);
// comment added by php-dev : should be false === $stmt->bind_param ...
if (false===$stmt) {
// Check errors for binding parameters
die('User Request update bind_param() failed: ' . htmlspecialchars($stmt->error));
}
$stmt->execute();
// comment added by php-dev : should be false === $stmt->execute ...
if (false===$stmt) {
die('User Status update execute() failed: ' . htmlspecialchars($stmt->error));
}
?>
我通过格式化更多可读性更新你的答案。 我还在代码中添加了一些注释,请检查它们。 –