好吧,有一段时间我一直在努力弄清楚为什么我的大多数记录都成功更新,而有些人还没有成功。我检查了php变量名/ sql行名,ajax请求,但我没有看到任何错误。我在这里,希望有人可以看看我的代码并帮助我找出错误。
Screenshot to show what I'm refering to.
HTML / JS:
<div class="container">
<div class="page-header">
<h1>General Reservation
<small>Update, Delete</small>
</h1>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="container-fluid">
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>Name</th>
<th>Department/Group</th>
<th>On-site Contact</th>
<th>Phone</th>
<th>Email</th>
<th>Event Description</th>
<th>Setup Time</th>
<th>Number of Participants</th>
<th>Date</th>
<th>Start/End Time</th>
<th>Special Accommodations</th>
<th>Room</th>
<th>Room Style</th>
<th>Catering</th>
<th>Technical Equipment</th>
<th>Pre-Clean</th>
<th>On-clean</th>
<th>Post-Clean</th>
<th>Reserved Parkning</th>
<th>VIP</th>
<th>Action</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
</div>
</div>
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/scripts.js"></script>
<script>
function viewGenData(){
$.ajax({
type: "GET",
url: "servergen.php",
success: function(data){
$('tbody').html(data);
}
});
}
function updateGenData(str){
var id = str;
var name = $('#name-'+str).val();
var dptGroup = $('#dptgroup-'+str).val();
var onSiteContact = $('#onsitecontact-'+str).val();
var phone = $('#phone-'+str).val();
var email = $('#email-'+str).val();
var eventDesc = $('#eventdesc-'+str).val();
var setUpTime = $('#setuptime-'+str).val();
var numOfParticipants = $('#partcnum-'+str).val();
var date = $('#date-'+str).val();
var startEndTime = $('#startendtime-'+str).val();
var specialAcc = $('#specialacc-'+str).val();
var room = $('#room-'+str).val();
var roomStyle = $('#roomstyle-'+str).val();
var catering = $('#catering-'+str).val();
var techEquip = $('#techequip-'+str).val();
var preClean = $('#preclean-'+str).val();
var postClean = $('#postclean-'+str).val();
var onClean = $('#onclean-'+str).val();
var rsvParking = $('#rsvparking-'+str).val();
var vip = $('#vip-'+str).val();
$.ajax({
type: "POST",
url: "servergen.php?p=edit",
data: "name=" + name + "&department=" + dptGroup + "&contact=" + onSiteContact + "&phone=" +
phone + "&email=" + email + "&description=" + eventDesc + "&setuptime=" + setUpTime + "&numberofparticipants=" + numOfParticipants +
"&date=" + date + "&startendtime=" + startEndTime + "&specialacc=" + specialAcc + "&room=" + room + "&roomstyle=" + roomStyle +
"&catering=" + catering + "&techequipment=" + techEquip + "&preclean=" + preClean + "&postclean=" + postClean + "&onclean=" + onClean +
"&rsvparking=" + rsvParking + "&vip=" + vip + "&id=" + id,
success: function(data){
viewGenData();
}
});
}
function deleteGenData(str){
var id = str;
$.ajax({
type: "GET",
url: "servergen.php?p=del",
data: "id="+id,
success: function(data){
viewGenData();
}
});
}
PHP / MySQL的:
<?php
include("db.php");
$page = isset($_GET['p'])?$_GET['p']:'';
if($page=='edit'){
$id = $_POST['id'];
$name = $_POST['name'];
$dptGroup = $_POST['dptgroup'];
$onSiteContact = $_POST['onsitecontact'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$eventDesc = $_POST['eventdesc'];
$setUpTime = $_POST['setuptime'];
$numOfParticipants = $_POST['partcnum'];
$date = $_POST['date'];
$startEndTime = $_POST['startendtime'];
$specialAcc = $_POST['specialacc'];
$room = $_POST['room'];
$roomStyle = $_POST['roomstyle'];
$catering = $_POST['catering'];
$techEquip = $_POST['techequip'];
$preClean = $_POST['preclean'];
$postClean = $_POST['postclean'];
$onClean = $_POST['onclean'];
$rsvParking = $_POST['rsvparking'];
$vip = $_POST['vip'];
$stmt = $db->prepare("update reservations set req_name=?, dept_group=?, onsite_contact=?, phone_num=?, email=?, event_description=?, setup_time=?, num_participants=?, date=?, start_end_time=?, notes=?, room=?, room_style=?, catering=?, tech_equip=?, pre_clean=?, post_clean=?, on_clean=?, reserved_parking=?, vip=? where id=?");
$stmt->bindParam(1,$name);
$stmt->bindParam(2,$dptGroup);
$stmt->bindParam(3,$onSiteContact);
$stmt->bindParam(4,$phone);
$stmt->bindParam(5,$email);
$stmt->bindParam(6,$eventDesc);
$stmt->bindParam(7,$setUpTime);
$stmt->bindParam(8,$numOfParticipants);
$stmt->bindParam(9,$date);
$stmt->bindParam(10,$startEndTime);
$stmt->bindParam(11,$specialAcc);
$stmt->bindParam(12,$room);
$stmt->bindParam(13,$roomStyle);
$stmt->bindParam(14,$catering);
$stmt->bindParam(15,$techEquip);
$stmt->bindParam(16,$preClean);
$stmt->bindParam(17,$postClean);
$stmt->bindParam(18,$onClean);
$stmt->bindParam(19,$rsvParking);
$stmt->bindParam(20,$vip);
$stmt->bindParam(21,$id);
if($stmt->execute()){
echo "Success, data updated.";
} else{
echo "Failed, dated not updated.";
}
} else if($page=='del'){
$id = $_GET['id'];
$stmt = $db->prepare("delete from reservations where id=?");
$stmt->bindParam(1, $id);
if($stmt->execute()){
echo "Success, data deleted.";
} else{
echo "Failed to delete data.";
}
} else{
$stmt = $db->prepare("select * from reservations order by id desc");
$stmt->execute();
while($row = $stmt->fetch()){
?>
<tr>
<td><?php echo $row['req_name']?></td>
<td><?php echo $row['dept_group']?></td>
<td><?php echo $row['onsite_contact']?></td>
<td><?php echo $row['phone_num']?></td>
<td><?php echo $row['email'] ?></td>
<td><?php echo $row['event_description'] ?></td>
<td><?php echo $row['setup_time']?></td>
<td><?php echo $row['num_participants']?></td>
<td><?php echo $row['date']?></td>
<td><?php echo $row['start_end_time']?></td>
<td><?php echo $row['notes']?></td>
<td><?php echo $row['room']?></td>
<td><?php echo $row['room_style']?></td>
<td><?php echo $row['catering']?></td>
<td><?php echo $row['tech_equip']?></td>
<td><?php echo $row['pre_clean']?></td>
<td><?php echo $row['post_clean']?></td>
<td><?php echo $row['on_clean']?></td>
<td><?php echo $row['reserved_parking']?></td>
<td><?php echo $row['vip']?></td>
<td>
<button class="btn btn-warning" data-toggle="modal" data-target="#edit-<?php echo $row['id'] ?>">Update</button>
<!-- Modal -->
<div class="modal fade" id="edit-<?php echo $row['id'] ?>" tabindex="-1" role="dialog" aria-labelledby="editLabel-<?php echo $row['id'] ?>">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="editLabel-<?php echo $row['id'] ?>">Update Data</h4>
</div>
<form>
<div class="modal-body">
<input type="hidden" id="<?php echo $row['id'] ?>" value="<?php echo $row['id'] ?>">
<div class="form-group">
<label for="name-">Name of Requestor </label>
<input type="text" class="form-control" id="name-<?php echo $row['id'] ?>" value="<?php echo $row['req_name'] ?>" />
</div>
<div class="form-group">
<label for="phone-">Phone Number</label>
<input type="tel" class="form-control" id="phone-<?php echo $row['id'] ?>" value="<?php echo $row['phone_num'] ?>"" />
</div>
<div class="form-group">
<label for="setuptime-">Facilities/Catering Set-up time
</label>
<input type="text" class="form-control" id="setuptime-<?php echo $row['id'] ?>" value="<?php echo $row['setup_time'] ?>" />
</div>
<div class="form-group">
<label for="dptgroup-">Department/Group</label>
<input type="text" class="form-control" id="dptgroup-<?php echo $row['id'] ?>" value="<?php echo $row['dept_group'] ?>" />
</div>
<div class="form-group">
<label for="email-">Email Address</label>
<input type="email" class="form-control" id="email-<?php echo $row['id'] ?>" value="<?php echo $row['email'] ?>" />
</div>
<div class="form-group">
<label for="partcnum-">Number of Participants</label>
<input type="text" class="form-control" id="partcnum-<?php echo $row['id'] ?>" value="<?php echo $row['num_participants'] ?>" />
</div>
<div class="form-group">
<label for="onsitecontact-">On-site Contact</label>
<input type="text" class="form-control" id="onsitecontact-<?php echo $row['id'] ?>" value="<?php echo $row['onsite_contact'] ?>" />
</div>
<div class="form-group">
<label for="eventdesc-">Description of the Event</label>
<textarea id="eventdesc-<?php echo $row['id'] ?>" class="form-control" rows="5" cols="25" value="<?php echo $row['event_description'] ?>"><?php echo $row['event_description']?></textarea>
</div>
<div class="form-group">
<label for="date-">Date</label>
<input type="text" class="form-control" id="date-<?php echo $row['id'] ?>" value="<?php echo $row['date'] ?>" />
</div>
<div class="form-group">
<label for="startendtime-">Start/End Time (AM/PM)</label>
<input type="text" class="form-control" id="startendtime-<?php echo $row['id'] ?>" value="<?php echo $row['start_end_time'] ?>" />
</div>
<div class="form-group">
<label for="specialacc-">Special accommodations, notes or additional requirements</label>
<textarea id="specialacc-<?php echo $row['id'] ?>" class="form-control" rows="5" cols="25" value="<?php echo $row['notes'] ?>"><?php echo $row['notes']?></textarea>
</div>
<div class="form-group">
<label for="catering-">Is catering needed?</label>
<input type="text" class="form-control" id="catering-<?php echo $row['id'] ?>" value="<?php echo $row['catering'] ?>" />
</div>
<div class="form-group">
<label for="techequip-">Is technical equipment needed?</label>
<input type="text" class="form-control" id="techequip-<?php echo $row['id'] ?>" value="<?php echo $row['tech_equip'] ?>" />
</div>
<div class="form-group">
<label for="preclean-">Is pre-clean up needed?</label>
<input type="text" class="form-control" id="preclean-<?php echo $row['id'] ?>" value="<?php echo $row['pre_clean'] ?>" />
</div>
<div class="form-group">
<label for="onclean-">Is clean up during the event needed?</label>
<input type="text" class="form-control" id="onclean-<?php echo $row['id'] ?>" value="<?php echo $row['on_clean'] ?>" />
</div>
<div class="form-group">
<label for="postclean-">Is post clean up needed?</label>
<input type="text" class="form-control" id="postclean-<?php echo $row['id'] ?>" value="<?php echo $row['post_clean'] ?>" />
</div>
<div class="form-group">
<label for="rsvparking-">Is reserved parking needed?</label>
<input type="text" class="form-control" id="rsvparking-<?php echo $row['id'] ?>" value="<?php echo $row['reserved_parking'] ?>" />
</div>
<div class="form-group">
<label for="vip">Will there be a very important person will be in attendance (dignitaries, elected officials, high profile)?</label>
<input type="text" class="form-control" id="vip-<?php echo $row['id'] ?>" value="<?php echo $row['vip'] ?>" />
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" onclick="updateGenData(<?php echo $row['id'] ?>)" class="btn btn-primary">Update</button>
</div>
</form>
</div>
</div>
</div>
<button onclick="deleteGenData(<?php echo $row['id'] ?>)" class="btn btn-danger" style="width: 73.94px; margin-top: 2px">Delete</button>
</td>
</tr>
<?php
}
}
?>
答案 0 :(得分:1)
首先更改ajax data
部分:
data: "name=" + name + "&department=" + dptGroup + "&contact=" + onSiteContact + "&phone="
到:
data: {"name": name, department: dptGroup , [...]}
第二:你将始终使用success
部分的js代码。如果发生错误,请发送错误状态,例如:
header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500);
最后:给自己显示错误消息。它应该定义DEBUG常量,并根据设置显示有关错误的更多或更少的信息。