Heyho,
我遇到了SLIM Restful-API的问题。我查找了其他类似问题的答案,This Answer非常接近我,但我没有运气让它起作用。重点是我的API之前工作正常,从那以后我甚至都没有改变。删除工作正常,而UPDATE和ADD不起作用。
我用两件事来测试它,第一件事就是CURL:
添加
功能:
function addTech() {
$request = Slim::getInstance()->request();
$tech = json_decode($request->getBody());
$sql = "INSERT INTO tech (tech_lastname, tech_firstname, tech_adress, tech_zip, tech_tel, user_id) VALUES (:tech_lastname, :tech_firstname, :tech_adress, :tech_zip, :tech_tel, :user_id)";
try {
$db = getConnection();
$stmt = $db->prepare($sql);
$stmt->bindParam("tech_lastname", $tech->tech_lastname);
$stmt->bindParam("tech_firstname", $tech->tech_firstname);
$stmt->bindParam("tech_adress", $tech->tech_adress);
$stmt->bindParam("tech_zip", $tech->tech_zip);
$stmt->bindParam("tech_tel", $tech->tech_tel);
$stmt->bindParam("user_id", $tech->user_id);
$stmt->execute();
$tech->tech_id = $db->lastInsertId();
$db = null;
echo json_encode($tech);
} catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
} catch(Exception $e) {
echo '{"error":{"text":'. $e->getMessage() . '}}';
}
}
卷曲:
c:\curl>curl -i -H "Content-Type: application/json" -X POST -d '{"tech_lastname":"why","tech_firstname":"why","tech_adress":"why","tech_zip":"why","tech_tel":"why","user_id":"1"}' http://localhost/itec-api/tech
错误:
HTTP/1.1 200 OK
Date: Tue, 17 May 2016 10:57:29 GMT
Server: Apache/2.4.9 (Win32) OpenSSL/1.0.1g PHP/5.5.11
X-Powered-By: PHP/5.5.11
Content-Length: 106
Content-Type: text/html
{"error":{"text":SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'user_id' cannot be null}}
更新
功能:
function updateTech($tech_id) {
$request = Slim::getInstance()->request();
$body = $request->getBody();
$tech = json_decode($body);
$sql = "UPDATE tech SET user_id=:user_id, tech_lastname=:tech_lastname, tech_firstname=:tech_firstname, tech_adress=:tech_adress, tech_zip=:tech_zip, tech_tel=:tech_tel WHERE tech_id=:tech_id";
try {
$db = getConnection();
$stmt = $db->prepare($sql);
$stmt->bindParam("user_id", $tech->user_id);
$stmt->bindParam("tech_lastname", $tech->tech_lastname);
$stmt->bindParam("tech_firstname", $tech->tech_firstname);
$stmt->bindParam("tech_adress", $tech->tech_adress);
$stmt->bindParam("tech_zip", $tech->tech_zip);
$stmt->bindParam("tech_tel", $tech->tech_tel);
$stmt->bindParam("tech_id", $tech_id);
$stmt->execute();
$db = null;
echo json_encode($tech);
} catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
}
卷曲:
curl -i -X PUT -d '{"tech_id":"3","tech_firstname":"updatetest"}' http://localhost/itec-api/tech/3
错误:
{"error":{"text":SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`itec_app_workflow`.`tech`, CONSTRAINT `fk_techniker_user1` FOR
EIGN KEY (`user_id`) REFERENCES `user` (`user_id`) ON DELETE CASCADE ON UPDATE CASCADE)}}
我猜问题来自getBody。我尝试了一个回声
echo '$request:' . $request->getBody() . '</br>';
得到了这个:
$request:'{tech_lastname:why,tech_firstname:why,tech_adress:why,tech_zip:why,tech_tel:why,user_id:1}'</br>{"error":{"text":SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'user_id' cannot
be null}}
在这里,表格:
-- -----------------------------------------------------
-- Table `itec_app_workflow`.`user`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `itec_app_workflow`.`user` (
`user_id` INT NOT NULL AUTO_INCREMENT,
`username` VARCHAR(45) NULL,
`user_email` VARCHAR(45) NULL,
`user_role` INT NULL,
`password` VARCHAR(150) NULL,
`salt` VARCHAR(150) NULL,
PRIMARY KEY (`user_id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `itec_app_workflow`.`tech`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `itec_app_workflow`.`tech` (
`tech_id` INT NOT NULL AUTO_INCREMENT,
`tech_lastname` VARCHAR(45) NULL,
`tech_firstname` VARCHAR(45) NULL,
`tech_adress` VARCHAR(100) NULL,
`tech_zip` VARCHAR(45) NULL,
`tech_tel` VARCHAR(45) NULL,
`user_id` INT NOT NULL,
PRIMARY KEY (`tech_id`, `user_id`),
INDEX `fk_techniker_user1_idx` (`user_id` ASC),
CONSTRAINT `fk_techniker_user1`
FOREIGN KEY (`user_id`)
REFERENCES `itec_app_workflow`.`user` (`user_id`)
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
我真的希望你们能帮助我。 在此先感谢:)