im having problem with my booking/stock control code.
i have a sql database, one of the column is called Stock.
there is a Stock of about 500 tickets for each movie.
when user choose to buy 600 tickets (i know unreal) i need echo out SOLD OUT.
whatever number of tickets it shows booking details
i suspect which line is wrong but dont know how to fix it…
abd then need to update stock…
//if the book button hit
if(isset($_POST['book']))
{
//run sql
$sql="SELECT Title AND Date FROM movies WHERE Title='$_POST[Title]' AND Date='$_POST[Date]'";
//if not enough stock- PROBABLY ERROR HERE:
if (Stock < $ticket){
echo "The tickets has been sold out";
}
//if enough stock, give details of booking
else{
echo "<br>Your booking details:<br>
Title: " .$_POST{Title}. "<br>
Date: " .$_POST{Date}. "<br>
Tickets number: " .$ticket ;
//update stock
$UpdateQuery = "UPDATE movies SET Stock=Stock - '$ticket'";
}
}
//if the book button hit
if(isset($_POST['book']))
{
//run sql
$sql="SELECT Title AND Date FROM movies WHERE Title='$_POST[Title]' AND Date='$_POST[Date]'";
mysqli_query($con, $sql);
//if not enough stock- PROBABLY ERROR HERE:
if (Stock < $ticket){
echo "The tickets has been sold out";
}
//if enough stock, give details of booking
else{
echo "<br>Your booking details:<br>
Title: " .$_POST{Title}. "<br>
Date: " .$_POST{Date}. "<br>
Tickets number: " .$ticket ;
//update stock
$UpdateQuery = "UPDATE movies SET Stock=Stock - '$ticket'";
}
}
$sql="SELECT Title AND Date FROM movies WHERE Title='$_POST[Title]' AND Date='$_POST[Date]'";
I’d have expected it to say
$sql="SELECT Title, Date FROM movies WHERE Title='$_POST[Title]' AND Date='$_POST[Date]'";
The next issue, though, is here:
if (Stock < $ticket){
Stock isn’t a valid PHP variable name, so that won’t run and should give you an error message. I suspect you need to get it from your database, but your query only returns Title and Date. But after you’ve executed the query, you don’t retrieve any results from it.
So, you need to add the extra column to your query, add code to retrieve the results from the query, check whether there were any results, and if so, check the value of the Stock column against the ticket quantity required.
Also note that you need to check the date format coming from your form, to see whether it’s in the same format as the date stored in your database. Once you’ve dealt with that, look at prepared statements in your query, rather than just concatenating strings as you do - I don’t know enough about mysqli to offer more there.