我想更新数据库信息。当我使用这一行代码时,它会更新。不幸的是,这告诉我我的ajax无法成功执行,因为它没有继续执行成功功能。请帮忙。
HTML代码
<form class="EditEmployees" method="POST">
<span>Employee ID:</span><span id="employeeid"></span>
<span>First Name</span>
<input type="text" id="editfirstname" name="editfirstname" placeholder="" class="tb-style">
<span>Last Name</span>
<input type="text" id="editlastname" name="editlastname" placeholder="" class="tb-style">
<span class="label" for="">Gender</span>
<select class="tb-style" name="editgender" id="editgender">
<option>Please Select</option>
<option value = "male">Male</option>
<option value="female">Female</option>
</select>
<span class="label" for="dob">Date of Birth</span>
<input type="date" id="editdob" name="editdob" class="tb-style">
<div class='input-group date' id='datetimepicker1' >
<div class="input-group-addon">
<span class="label" for="dob">Date Hired</span>
<input type="date" id="editdatehired" name="editdatehired" class="tb-style">
<button type="" class="btn-EditSave w-100" name="">Save Changes</button>
</form>
jQuery代码。
$('.btn-EditSave').click(function(){
// alert($('#editfirstname').val())
$.ajax({
url:'ajax.php',
data:{id:id,
type:'update-records-via-id',
FirstName:$('#editfirstname').val(),
LastName:$('#editlastname').val(),
dob:$('#editdob').val(),
datehired:$('#editdatehired').val(),
gender:$('#editgender').val()
},
dataType:'JSON',
type:'POST',
success: function(data){
alert("data saved")
}
})
})
PHP代码:更新数据库上的数据
function updaterecordviaid($id,$FirstName,$LastName,$gender,$dob,$datehired,$position){
$conn = mysqli_connect("localhost","root","","test");
$result = "update employees set FirstName = '$FirstName', LastName='$LastName', Gender = '$gender', DateOfBirth = '$dob', DateHired= '$datehired', PositionId='$position' where EmployeeId='$id'";
if(mysqli_query($conn,$result)){
echo json_encode(array('message'=>'Record Succesfully Updated!','FirstName'=>$FirstName,'LastName'=>$LastName));
return;
}
echo json_encode(array('message'=>'Something went wrong'));
return;
}