0

Hi,

I'm having trouble with my session variable. When I add this code in the while loop it doesn't add the product in, but when I put it outside it adds the product to the cart. Can anyone tell me what's wrong with my code? It would save me a lot of time.

Thanks

    <?php 

    session_start();

    $page = 'index.php';

    $con = mysqli_connect("****","***","****","***");

     if (isset($_GET['add'])) {  // If we have clicked on add 
       $quantity  = mysqli_query($con,'SELECT ID,Quantity FROM Products WHERE ID =' .mysqli_escape_string((int) $_GET['add']) );
        while ($row = mysqli_fetch_assoc($con,$quantity)){
           if ($row['Quantity'] !=$_SESSION['cart_'.$_GET['add']]); 






        } 


     $_SESSION['cart_'] += 1;  

      }  






    function products () {

    $con = mysqli_connect("*****","****","***","***");
    mysqli_select_db("Products", $con);

    if (!$con) {
      die('Could not connect: ' . mysql_error());
     }

  $result = mysqli_query($con,'SELECT ID,Name,Description,Price FROM Products WHERE ID >= 0');

  echo "<table border='1'>
  <tr>
  <th> ID </th>
  <th> Products </th> 
  <th> Description </th>
  <th> Price </th>
  <th> Purchase </th>
  </tr>";

  while ($row = mysqli_fetch_array($result)) {

     echo "<tr>" . "<td>" . $row['ID'] . "</td>" . "<td>" . $row['Name'] . "</td>" . "<td>" . $row['Description'] . "</td>" .  "<td>" . number_format($row ['Price'], 2) . "<td>" . '<form action="add.php?add='.$row['ID'] .'" method="POST"><input type="submit" value="Add" name="add">    </form>' . "</td>" . "</tr>";

  }









    }

    echo $_SESSION['cart_'];

  ?>
2
Contributors
1
Reply
3
Views
4 Years
Discussion Span
Last Post by LastMitch
3

@KRUX17

I'm having trouble with my session variable. When I add this code in the while loop it doesn't add the product in, but when I put it outside it adds the product to the cart. Can anyone tell me what's wrong with my code? It would save me a lot of time.

Are you having the same issue from your preivous thread:

http://www.daniweb.com/web-development/php/threads/441076/having-trouble-with-my-php-code

It's a bit hard to help you if you don't really take time to know how this code works. I'm not sure where you got this code from but so far base on the answer you give me that you are not very familiar with PHP.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.