我正在开发“巴士预订网站”,该网站允许用户预订从一个城市到另一个城市的旅行。 我每天都有128溃败。每趟巴士容量为 44 个座位。 所以我希望当用户点击确认预订时,系统将计算用户为相同路线选择的同一天预订的座位。 因此,如果已订满消息
将显示Sorry no seat available
。 否则预订将被确认,并且还会显示剩余座位数。
我生成此表是为了计算每天的座位数和溃败情况: Date table 。我的预订表是 Booking 。无论如何,我尝试使用此代码来计算座位号,但它不起作用:
<?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>";
}
?>
感谢您提前提供的帮助,
最佳答案
$queryd=mysql_query($sqld);
if ($queryd>44)
在那里找到问题:)阅读此内容
For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.
它返回资源,而不是行数。找到返回结果行数的正确函数。
在该任务中,另请阅读此 How can I prevent SQL injection in PHP?
关于php - 在 PHP 中使用 COUNT(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36216961/