您好我的代码有问题,我已经多次检查过,但似乎无法找到它。我正在使用HTML,JS,Slim框架和PHP,并从mysql获取数据。 任何帮助将不胜感激!
JS
$(document).ready(function(){
$("#edit").click(function(){
var coach=new Coach(
$("#CoachFirstName").val(),
$("#CoachLastName").val(),
$("#CoachExperienceYrs").val(),
$("#CoachPastTeam").val());
$.ajax({
type:'PUT',
dataType:"json",
url:"db.php/coachs/1",
data:JSON.stringify(coach),
success: showResponse,
error: showError
});
}); });
function Coach(CoachFirstName, CoachLastName, CoachExperienceYrs, CoachPastTeam){
this.CoachFirstName=CoachFirstName;
this.CoachLastName=CoachLastName;
this.CoachExperienceYrs=CoachExperienceYrs;
this.CoachPastTeam=CoachPastTeam;
} function showResponse(responseData) {
console.log(responseData); }
function showError() {
alert("Error while updating" );
};
PHP - SLIM
$app->put('/coachs/:id', 'updateCoach');
function updateCoach($id){
$request = Slim::getInstance()->request();
$coach = json_decode($request->getBody());
$sql = "UPDATE coach SET CoachFirstName=:CoachFirstName, CoachLastName=:CoachLastName, CoachExperienceYrs=:CoachExperienceYrs, CoachPastTeam=:CoachPastTeam WHERE CoachID=:id";
try {
$db = getConnection();
$stmt = $db->prepare($sql);
$stmt->bindParam("CoachFirstName",$coach->CoachFirstName);
$stmt->bindParam("CoachLastName",$coach->CoachLastName);
$stmt->bindParam("CoachExperienceYrs",$coach->CoachExperienceYrs);
$stmt->bindParam("CoachPastTeam",$coach->CoachPastTeam);
$stmt->bindParam("id",$id);
$stmt->execute();
$coach = $stmt->fetchObject();
$db = null;
responseJson(json_encode($coach),200);
}catch(PDOException $e){
responseJson('{"error":{"text":'.$e->getMessage().'}}', 500);
}
}