I'm trying to make a function work, where I ask for a number of free seats left in a car, and if executed, it subtracts 1 from the result and updates the cell in database.
Sql statements must be correct... am I missing out on anything else?
function seatCalc($id){
$stmt=$this->connection->prepare("SELECT seats_left FROM drive WHERE id=?");
$stmt->bind_param("i", $id);
$stmt->bind_result($seat_count);
$stmt->execute();
return $seat_count;
if($seat_count >= 1){
$seat_count -= 1;
$stmt=$this->connection->prepare("UPDATE drive SET seats_left=? WHERE id=?");
$stmt->bind_param("ii", $seat_count, $id);
$stmt->execute();
}
}
It's a part of my university project and unfortunately, I can't reach my professor at the moment.
Thank you in advance!
您的return
语句会在处理if
块之前使您脱离函数。
Here is code:
function seatCalc($id){
$stmt=$this->connection->prepare("SELECT seats_left FROM drive WHERE id=?");
$stmt->bind_param("i", $id);
$stmt->bind_result($seat_count);
$stmt->execute();
if($seat_count > 0){
$stmt=$this->connection->prepare("UPDATE drive SET seats_left=seats_left-1 WHERE id=?");
$stmt->bind_param("i", $id);
$stmt->execute();
}
return $seat_count;
}
Also you can substract one with database..
Oh boy
function seatCalc($id){
$stmt=$this->connection->prepare("SELECT seats_left FROM drive WHERE id=:i");
$stmt->bind_param(":i", $id);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$seat_count = intval($row['seats_left']);
if($seat_count > 0){
$seat_count -= 1;
$stmt=$this->connection->prepare("UPDATE drive SET seats_left=seats_left-1 WHERE id=:id");
$stmt->bind_param(":id", $id);
$stmt->execute();
}
return $seat_count;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.