0

I'm currently working on an assignment that involves a lot of php stuff. So basically what I have is a website that allows you to buy movie tickets, you create an account, sign in to access the movies, choose the day and time, quantity and then checkout. I'm currently stuck on trying to retrieve the day and time.
This code is the php page for after the user has signed in and is currently choosing what movie they want and all of their options.

//Sign In Page
$query = 'SELECT * FROM project_movies ORDER BY title;';
$results = $conn->query($query);
if ($results && $results->num_rows > 0) {
  $n = $results->num_rows;
  echo '<form method="post" action="project_review.php"><table border="1">';
  echo '<tr><th>Movies</th><th>Synopsis</th><th>Day</th><th>Time</th><th>Quantity</th><th>Add to Cart</th></tr>';
  for ($i = 0; $i < $n; $i++) {
    $movies = $results->fetch_assoc();
    echo "<tr><td>$movies[title]<br /><img src='$movies[pathname]'></td><td>$movies[synopsis]</td><td>";
  
    //Select Day of Week
    $days = array('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat');
    $length = count($days);
    for ($j = 0; $j < $length; $j++) {
        echo "<input type='checkbox' value='$days[$j]' name='day'>
        <label for='$days[$j]'>$days[$j]</label><br />";
    }
    echo "</td><td>";
    
   //Select Time
   $time = array('12:00','3:00','6:00','9:00');
   $length = count($time);
    for ($m = 0; $m < $length; $m++) {
        echo "<input type='checkbox' value='$time[$m]' name='time'>
        <label for='$time[$m]'>$time[$m]</label><br />";
    }
    echo "</td><td>";
    
    //Select Quantity of Tickets
    echo "Kids $3 <input type='number' value='kids' name='kids' style=;width:35px;'><br />
          Adults $9 <input type='number' value='adult' name='adult' style=;width:35px;'><br />
          Seniors $5 <input type='number' value='senior' name='senior' style=;width:35px;'>";
          
    echo "</td><td><input type='checkbox' name='checkout[]' value='$movies[mid]'";
        
    echo "></td></tr>";
  } 
  echo '</table><input type="submit" value="Checkout"></form>';
}

This section of code is from the review php page which is what will show the user what they chose.

//Review Page
$_SESSION['checkout'] = $_POST['checkout'];
$_SESSION['day'] = $_POST['day'];
$_SESSION['time'] = $_POST['time'];

echo '<p>Movie tickets in your cart:</p>';
// connect to MySQL server
$conn = new mysqli($hn, $user, $passwd, $db);
if ($conn->connect_error)
  die($conn->connect_error);

$n = count($_SESSION['checkout']);
echo '<ul>';

for ($i = 0; $i < $n; $i++) {
  $mid = $_SESSION['checkout'][$i];
  $day = $_SESSION['day'][$j];
  $time = $_SESSION['time'][$m];
  $query = "SELECT title FROM project_movies WHERE mid = $mid;";
  //echo $query;
  $result = $conn->query($query);
  if ($result && $result->num_rows > 0) {
    $movies = $result->fetch_assoc();
    echo '<li>' .$movies['title'] . '</li>';
    echo '<p> &rarr;' .$day. ' at ' .$time. '</p>';

So far all I have working is the ability to see which movie was chosen, multiple of them can be chosen and they'll show up. If I select a day and time for only one movie, then it'll show up correctly. However, when I choose a second movie with a second set of day and time choices, it will just print out that day and time for both movies.
Here is the link to the page, the login I have set for now is
User: admin
Pass: 123456

  • 2
    ypur code is vulnerable to **sql injection** so use only **orepared statements with parameters** see https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php – nbk Nov 15 '20 at 23:17
  • Just a note not related to my answer but if I choose a date, and a time, and how many tickets I want; I really shouldn't have to check another box saying I want to add it to my cart. It's pretty evident I already do want to do that and I think it would be an improvement if your code could figure that out. :) – Dawson Irvine Nov 15 '20 at 23:35
  • Thank you both for your help, I will take them both into consideration! – Brandon Wong Nov 16 '20 at 03:43

1 Answers1

1

You have your movie choice as a checkbox with the name checkout[]. This will effectively allow you to add multiple movies, which I am sure you intended.

However, your date/times are still only a constant date instead of date[]. Thus the last input element set with the name date and time in your form will be the only ones sent.

Try adding the [] to the names of your date and time inputs to send all the dates and times via $_POST. Then you can access each with $_POST["date"][0] (first movie) and $_POST["date"][1] (second movie).

Edit

I noticed on your review page that you have

$mid = $_SESSION['checkout'][$i];
$day = $_SESSION['day'][$j];
$time = $_SESSION['time'][$m];

I do not see anywhere it is specifying what $j and $m are. And since you are in the for loop. I am thinking you need to change these to $i as that is the loop you are going through at the moment.

You may need to do another loop in order to get all the times and dates the user selected.

Edit #2 Looking at the source code your script generates through the example you provided the dates are listed as:

<label for='Sun'>Sun</label><br /><input type='checkbox' value='Mon' name='day[1]'>
<label for='Mon'>Mon</label><br /><input type='checkbox' value='Tue' name='day[2]'>
<label for='Tue'>Tue</label><br /><input type='checkbox' value='Wed' name='day[3]'>
<label for='Wed'>Wed</label><br /><input type='checkbox' value='Thu' name='day[4]'>
<label for='Thu'>Thu</label><br /><input type='checkbox' value='Fri' name='day[5]'>
<label for='Fri'>Fri</label><br /><input type='checkbox' value='Sat' name='day[6]'>

Which is what I first specified. This is the same for all movies so the last one set is the last one passed. Make them so they are:

<label for='Sun'>Sun</label><br /><input type='checkbox' value='Mon' name='day[][1]'>
<label for='Mon'>Mon</label><br /><input type='checkbox' value='Tue' name='day[][2]'>
<label for='Tue'>Tue</label><br /><input type='checkbox' value='Wed' name='day[][3]'>
<label for='Wed'>Wed</label><br /><input type='checkbox' value='Thu' name='day[][4]'>
<label for='Thu'>Thu</label><br /><input type='checkbox' value='Fri' name='day[][5]'>
<label for='Fri'>Fri</label><br /><input type='checkbox' value='Sat' name='day[][6]'>

And then you can loop through them to retrieve what you need for each movie.

Dawson Irvine
  • 322
  • 4
  • 19
  • I will take a look into this when I get a chance, thank you so much for the help – Brandon Wong Nov 16 '20 at 03:44
  • On the review page you think I should make 2 separate loops for the day and time respectively and then nest it in the checkout for loop? So far I haven't got it working yet – Brandon Wong Nov 16 '20 at 14:40