如果操作后sql中的数据变为负数,则显示消息并停止操作

时间:2015-07-23 08:44:50

标签: php sql database negative-number

这里最多可以选择五个座位。但如果只有一个座位可供用户并且用户选择五个座位,则数据库中的数据将存储为-4。如果用户选择的座位多于可用座位,我需要生成错误消息或提醒。我该如何解决这个问题?

<div class="row" id="form_book">
    <form role="form" class="from-inline">
        <label for="seats" class="col-md-4">Number of Seats: </label>
        <select name="selected_seat" id="" class="col-md-6 btn btn-default">
            <option value=1>One</option>
            <option value=2>Two</option>
            <option value=3>Three</option>
            <option value=4>Four</option>
            <option value=5>Five</option>
        </select>   
    <input type="submit" class="form-control" value="Confirm Ticket" name="submit" id="confirm_ticket" />
  </form>
</div>


if(isset($_GET['submit'])){
    $seats=$_GET['selected_seat'];
    $id= $_SESSION['id'];
    $sql="select * from bus where id= $id ";
    $query=mysql_query($sql);
    $row=mysql_fetch_array($query);
    /*---------other opreations ---------- */
    $new_seats= $row[7]-$seats;
    $sql_seat= "update bus set seats= $new_seats where id= $id";
    $query_seat=mysql_query($sql_seat);
} 

1 个答案:

答案 0 :(得分:0)

简单比较将解决您的问题我想

/*---------other opreations ---------- */
if($row[7] >= $seats ){ 
  $new_seats= $row[7]-$seats;
 .....
}
else {
  echo "only ".$row[7]." seats are available";
}