【发布时间】:2014-11-03 13:31:18
【问题描述】:
今天我尝试使用 SLIM 框架为我的 REST Web 服务创建更新功能。 但我遇到了这个问题。 我一直坚持我的查询,因为它效果不佳。
这是我的类包含查询
public function updateUserCard($card_id, $user_id, $barcode)
{
$stmt = $this->conn->prepare("UPDATE user_cards uc, cards c
SET uc.barcode = ?
WHERE uc.id = ?
AND uc.id = c.card_id
AND uc.user_id = ?");
if($stmt == FALSE)
{
die($this->conn->error);
}
else
{
$stmt->bind_param("iis", $user_id, $card_id, $barcode);
$stmt->execute();
$num_affected_rows = $stmt->affected_rows;
$stmt->close();
return $num_affected_rows > 0;
}
}
我尝试使用新值更新“条形码”列。但它没有用。
这是我在 index.php 中的代码
$app->put('/cards/users/:id', 'authenticate', function($id) use ($app)
{
// check for required params
verifyRequiredParams(array('barcode'));
global $user_id;
$barcode = $app->request->put('barcode');
$db = new UserCard();
$response = array();
// updating card
$result = $db->updateUserCard($user_id, $card_id, $barcode);
if ($result)
{
// card updated successfully
$response["error"] = false;
$response["message"] = "Card updated successfully";
}
else
{
// card failed to update
$response["error"] = true;
$response["message"] = "Card failed to update. Please try again!";
}
echoRespnse(200, $response);
});
回复总是像$response["message"] = "Card failed to update. Please try again!";
我写错代码了吗? 谢谢你们的帮助:)
【问题讨论】:
-
您的函数 updateUserCard 采用两个参数($user_id, $barcode),在调用 updateUserCard 时,您首先传递条形码,然后传递用户 ID。检查这一行,$result = $db->updateUserCard($barcode, $user_id);
标签: php mysql web-services rest slim