在被指控复制和粘贴代码之前,请先阅读。
通过ajax教程的一些个人调整,我得到了想要的输出,该输出在列上显示“ YES”,“ NO”或“ CANCELLED”。现在,我试图将主键名称从“ id”更改为“ orderid”,但是当我这样做时,sql和ajax停止运行。
这听起来很简单,但我感到困惑,因为“ id”无处不在。 它用作我的ajax代码中的变量,html中的属性以及SQL表中主键的名称。
如果我将SQL中的主键名称从“ id”更改为“ orderid”,我想知道如何更改代码中的“ id”吗?
index.php
<?php
include 'connection.php';
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<table class="table">
<thead>
<tr>
<th>Email</th>
<th>Scheduled</th>
</tr>
</thead>
<tbody>
<?php
$table = mysqli_query($connection ,'SELECT * FROM user');
while($row = mysqli_fetch_array($table)){ ?>
<tr id="<?php echo $row['id']; ?>">
<td data-target="email"><?php echo $row['email']; ?></td>
<td data-target="scheduled">
<input id='userId' type='hidden'/>
<?php
if ($row['scheduled'] == 1) {
?>
<a href="#" data-role="update" data-id="<?php echo $row['id'] ;?>">YES</a>
<?php
} else if ($row['scheduled'] == 0) {
?>
<a href="#" data-role="update" data-id="<?php echo $row['id'] ;?>">NO</a>
<?php
} else if ($row['scheduled'] == 2) {
?>
<a href="#" data-role="update" data-id="<?php echo $row['id'] ;?>">CANCELLED</a>
<?php
}
?>
</td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
<!-- Modal -->
<div id="myModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content" style="width: 300px; margin: 0 auto;">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<div class="modal-body">
<div class="form-group">
<a href="#" id="update_yes" class="btn btn-success text-center center-block">YES</a><br>
<a href="#" id="update_no" class="btn btn-danger text-center center-block">NO</a><br>
<a href="#" id="update_cancelled" class="btn btn-warning text-center center-block">CANCEL</a>
</div>
</div>
</div>
</div>
</div>
</body>
<script>
$(document).ready(function(){
// append values in input fields
$(document).on('click','a[data-role=update]',function(){
var id = $(this).data('id');
var scheduled = $('#'+id).children('td[data-target=scheduled]').text();
$('#scheduled').val(scheduled);
$('#userId').val(id);
$('#myModal').modal('toggle');
});
// now create event to get data from fields and update in database
$('#update_no').click(function(){
var id = $('#userId').val();
var scheduled = 0;
$.ajax({
url : 'connection.php',
method : 'post',
data : {scheduled: scheduled , id: id},
success : function(response){
// now update user record in table
$('#'+id).children('td[data-target=scheduled]').text('<a href="#">NO</a>');
$('#myModal').modal('toggle');
window.location.reload()
}
});
});
$('#update_yes').click(function(){
var id = $('#userId').val();
var scheduled = 1;
$.ajax({
url : 'connection.php',
method : 'post',
data : {scheduled: scheduled , id: id},
success : function(response){
// now update user record in table
$('#'+id).children('td[data-target=scheduled]').html('<a href="#">YES</a>');
$('#myModal').modal('toggle');
window.location.reload()
}
});
});
$('#update_cancelled').click(function(){
var id = $('#userId').val();
var scheduled = 2;
$.ajax({
url : 'connection.php',
method : 'post',
data : {scheduled: scheduled , id: id},
success : function(response){
// now update user record in table
$('#'+id).children('td[data-target=scheduled]').text('<a href="#">CANCELLED</a>');
$('#myModal').modal('toggle');
window.location.reload()
}
});
});
});
</script>
</html>
connection.php
<?php
$connection = mysqli_connect('localhost' , 'root' ,'' ,'ajax_test');
if(isset($_POST['id'])){
$id = $_POST['id'];
$scheduled = $_POST['scheduled'];
$result = mysqli_query($connection , "UPDATE user SET scheduled = '$scheduled' WHERE id='$id'");
}
?>
数据库名称“ ajax_test”,表名称“ user”