我正在尝试检查是否有可用于预订活动的位置,并且我正在使用If语句来执行此操作。If语句检查bookedPlaces是否少于可用位置的总数,如果有可用位置,它应该允许预订并插入到clientEvent中,并更新activityEvent表中的bookingPlaces。如果没有可用的地方,它应该回显完全预订。它总是回响着预订已满的回声。我也不确定如何在一个问题中插入和更新。
<?php
session_start();
require("connection.php");
$con=mysqli_connect("$mysql_host","$mysql_user","$mysql_password","$mysql_database");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql ="select bookedPlaces, noOfPlaces from activityEvent";
$result = mysqli_query($con,$sql);
$clientID=$_SESSION['clientID'];
$activityEventID =$_SESSION['activityEventID'];
while($row = $result->fetch_assoc())
{
$booked =$row['bookedPlaces'];
$noofPlaces = $row['noOfPlaces'];
}
if($booked < $noofPlaces)
{
echo "You have booked ";
$sql="Insert into clientEvent(clientID, activityEventID) Values('$clientID','$activityEventID')";
$sql= "update activityEvent set AvailablePlaces = ($available -1) where activityEventID = $activityEventID";
echo "$sql";
}
else
{
echo "Fully booked ";
}
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
?>发布于 2015-03-07 23:06:44
我给你一个选择:我假设bookedPlaces和noOfPlaces的一些行是空的,所以你必须比较它们。您可以创建一个计数器函数,如下所示:
function counter($row){
$mysqli = new mysqli("localhost", "root", "", "first");
$res = $mysqli->query("SELECT COUNT({$row}) FROM records WHERE {$row} !='' "); // !=0 or !=[what's your no value]
$row = $res->fetch_row();
return $row[0];
}现在您可以使用
if(counter('bookedPlaces') < counter('noOfPlacest') {在此选项中,保留fetch_assoc查询的原样。
发布于 2015-03-07 23:09:33
看起来整个if-else块if($booked < $noofPlaces)都在您的while循环之外。因此,if语句计算的是意想不到的值。
更正后的代码应该如下所示。
while ($row = $result->fetch_assoc()) {
$booked = $row['bookedPlaces'];
$noofPlaces = $row['noOfPlaces'];
if ($booked < $noofPlaces) {
echo "You have booked ";
$sql = "Insert into clientEvent(clientID, activityEventID) Values('$clientID','$activityEventID')";
$sql = "update activityEvent set AvailablePlaces = ($available -1) where activityEventID = $activityEventID";
echo "$sql";
}
else {
echo "Fully booked ";
}
}https://stackoverflow.com/questions/28916112
复制相似问题