Reputation: 45
I am working on "Bus Reservation Website" that allowed the user to book trips from city to another. I have 128 rout every day. Each rout bus capacity is 44 seat. So i want that when the user click on confirm booking the system will calculate the seats booked on the same day the user selected for the same routs. so if it fully booked message
Sorry no seat available
will be displayed. else the booking will be confirm and number of seats left will be displayed too.
I generated this table in order to count the seats each day and rout: Date table . and my booking table is Booking. Any how, i tried this code to calculate the seat number but it didn't work:
<?php
@session_start();
$username = "root";
$password = "";
$server="localhost";
$database = "Mydatabase";
$table = "booking";
@mysql_connect($server,$username,$password);
mysql_select_db($database);
if(isset($_POST['ok']))
{
//values from my html page
$dept = $_POST['tdep'];
$arr = $_POST['tarr'];
$date = $_POST['ddate'];
$tcode=$_POST['tcode'];
$userid = $_SESSION['login'];
$num =$_POST['num'];
$confirmation = uniqid();
//booking details that will be stored in booking table
$sql = "INSERT INTO $table VALUES('$confirmation','$tcode', '$date', '$num', '$userid', '$price', '$dept','$arr' )";
$query=mysql_query($sql);
//date,rout and number of seats will be inserted to date table
$sql = "INSERT INTO date (tcode,sno,date) VALUES('$tcode', '$num','$date' )";
$query=mysql_query($sql);
//calculating number of seats
$sqld="SELECT COUNT(sno) FROM date WHERE date='$date' AND tcode='$tcode'";
$queryd=mysql_query($sqld);
if ($queryd>44)
echo "No Available Seats left";
else
echo "seats Available= ".$queryd;
//decarsing number of seats in routs table
$sql3 = "UPDATE long_dis SET seat=seat-'$num' WHERE Tcode='$tcode'";
$query3=mysql_query($sql3);
//confirmation message
echo "<h3><font color='red'>Booking is successfully Confirmed for national id :".$userid."</font></h3>";
echo "<h4><font color='blue'>The total cost= ".$price." OMR </br>And your confirmation code is: ". $confirmation."</font></h4>";
}
?>
Thank you for your help in advance,
Upvotes: 0
Views: 1269
Reputation: 45
I actually found that that i should use SUM() not COUNT(). Small codes means big deal!
anyhow, i will list my answer in case anyone would like to benefit.
<?php
$result = mysql_query("SELECT SUM(sno) AS value_sum FROM $table WHERE date='$date' AND tcode='$tcode'");
$row = mysql_fetch_assoc($result);
$sum = $row['value_sum'];
if ($sum<44)
{
echo "<h3><font color='red'>Booking is successfully Confirmed !!</font>";
}
else if($sum>=44)
echo "<h3><font color='red'>SORRY,".$sum." of 44 seat been already booked for the selected date and Rout!!</font></h3>";
?>
Upvotes: 0
Reputation: 917
Firstly you have to fetch the data.
and secondly your if condition of greater than 44 will not work since total of seats that can be booked is 44 your total cannot be greater than 44. hence check for greater than equal to 44.
//calculating number of seats
$sqld="SELECT COUNT(sno) as seatAvialable FROM date WHERE date='$date' AND tcode='$tcode'";
$queryd=mysql_query($sqld);
$resultd=mysql_fetch_assoc($queryd);
if ($resultd['seatAvialable']>=44)
echo "No Available Seats left";
else
echo "seats Available= ".$queryd;
Upvotes: 0
Reputation: 46900
$queryd=mysql_query($sqld);
if ($queryd>44)
Find problem there :) Read this
For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.
It returns a resource, not the number of rows. Find the proper function that returns the number of rows in a result.
While in that quest, also read this How can I prevent SQL injection in PHP?
Upvotes: 2