1

There seems to be a problem with my code that i am unable to fix. I am getting errors that i am having trouble with.

The error is:

Undefined index id, undefined variable result, and undefined variable i

How do I fix this?

<?php
session_start();
require 'connect.php';
require 'item.php';
$relult = mysqli_query($link, 'select * from tickets where id='.$_GET['id']);
$product = mysqli_fetch_object($result);
if(isset($_GET['id'])){
    $item= new Item();
    $item->id = $product->id;
    $item->name = $product->game;
    $item->price = $product->price;
    $item->quantity = 1; 
    $_SESSION['cart'][] = $item;
}
?>
<table align="center">
<td colspan="11"><h4> Upcoming games 2016/2017</h3> <td>
  <tr>
    <th>id</th>
    <th>game</th>
    <th>price</th>
    <th>stadium</th>
    <th>quantity</th>
    <th>Sub Total</th>
  </tr>       
    <?php
    $cart = unserialize(serialize($_SESSION['cart']));
    for($i-0; $i<count($cart); $i++){
    ?>
     <tr> 
         <td><?php echo $cart[$i]->id; ?> </td>   
         <td><?php echo $cart[$i]->game; ?> </td>   
         <td><?php echo $cart[$i]->price; ?> </td>   
         <td><?php echo $cart[$i]->stsdium; ?> </td>
         <td><?php echo $cart[$i]->quantity; ?> </td>
         <td><?php echo $cart[$i]->price * $cart[$i]->quantity; ?> </td>   
     </tr>    

    <?php } ?>

</table>       
          <a href=" buytickets.php">Add more tickets to your cart</a>      
          </div>
Clay
  • 4,458
  • 3
  • 29
  • 45
Kevin G
  • 121
  • 7

2 Answers2

1

I'm afraid there are quite a lot of problems here.

  • First, two obvious ones: you have $relult instead of $result on line 5. You have $i-0 instead of $i=0 on line 29.

  • Furthermore, you're performing a query (a non-safe one, have a look at parameter binding with PDO) using $_GET['id'], while you don't appear to be sure this variable will be available: you check on line 8 if it is set, but that check is performed after injecting that unknown value in a query...

  • On line 14 you are presuming that $_SESSION['cart'] is an array, although this array has not been defined before.

  • What's up with $cart = unserialize(serialize($_SESSION['cart']))? If the cart is an array of Item objects, you can simply iterate like so: foreach ($_SESSION['cart'] as $item) { echo $item->id; /*etc*/ }
1sloc
  • 1,130
  • 5
  • 12
0

Undefined index id

I guess $_GET['id'] is undefined. Maybe you need $_POST['id'] or $_SESSION['id']?
See this answer to see the difference between GET and POST.

undefined variable result

You got a typo in line 5. $relult = should be $result =.

undefined variable i

Again a typo, this time in line 28: for($i-0; should be for($i=0;.

Your first access at $_SESSION['cart'] should be a check if it's set and an array. Do something like this to initialize the value:

if(!isset($_SESSION['cart']) || !is_array($_SESSION['cart'])  
    $_SESSION['cart'] = array();

The line

<td colspan="11"><h4> Upcoming games 2016/2017</h3> <td>

should be covered in a <tr> element and the typo <h4></h3> needs to be fixed. Why is the colspan 11 when you only got 6 columns?

$cart = unserialize(serialize($_SESSION['cart']));

This doesn't make sense to me. Why not $cart = $_SESSION['cart'];?
Beware that you didn't set $cart[$i]->stsdium;. Did you mean stadium?

Also you should think about filtering GET and POST values and use prepared Statements to increase security.
Filtering the id from POST could look like this:

$id = filter_input(INPUT_POST, 'id', FILTER_VALIDATE_INT);
Community
  • 1
  • 1
Kaemmelot
  • 41
  • 1
  • 5