我正在尝试检查是否有可用的地方来预订活动,并且我使用if语句来执行此操作。如果语句检查registeredPlaces是否小于可用位置的总数,并且是否有可用位置,则应该允许预订并插入到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));
}
?>
答案 0 :(得分:0)
我给你一个选择: 我认为scheduledPlaces和noOfPlaces的某些行是null,所以你必须比较它们。 您可以创建一个这样的计数器函数:
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查询不变。
答案 1 :(得分:0)
看起来整个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 ";
}
}