Add always the same "item" to reading list

Hi. I try to add models to a reading list after I click on a link. The problem is I always add the some model. I use session file and file to display the “added models”

   <?php
ob_start();
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);
include('connection.php'); 
// 2) When the linked to page is visited, add the specified item to the cart with a quantity of one...

// a) Validate the id received from the link.
$id = isset($_GET['ID']) ? (int)$_GET['ID'] : 0; // zero is not used/valid for a database auto-increment value that would be used as an id
if($id < 1){
    // not a valid id
    echo "The requested id is not valid";
} else {
    // the id is valid

    // b) If the cart doesn't exist, create an empty one.
    if(!isset($_SESSION['cart'])){
        $_SESSION['cart'] = array();
    }
    
    // c) Add the item to the cart with a quantity of one, if it is not already in the cart or increment the item quantity if it is already in the cart.
    if(!isset($_SESSION['cart'][$id])){
        // id is not in the cart, create an entry
        $_SESSION['cart'][$id] = 0; // initialize to zero (the next statement will set it to one)
    }
    $_SESSION['cart'][$id]++; // add one to the quantity (if the item was already in the cart with some quantity, this will increment the quantity)
	header('location:viewwishlist.php');
}

// At this point, you are done with the add to cart task.
// You would either link/redirect back to the page that displays all the items
// or you could display the cart contents, by retrieving all the id's from the cart (see the array_keys() and implode() functions), then retrieve the information from the database table and loop over it to display it
// or you could incorporate this code into the main page, making a single page.

// for demo purposes, just display the session data -


// for demo purposes, just display the session data -

?>

Second file to display the items:

<?php
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);
include('connection.php');
echo '<table >'; 
  //set up head row of table 
  echo '<tr>'; 
  echo '<td>ID</td>';  
  echo '<td>Name</td>';  
  echo '<td>Lastname</td>';  
  echo '<td>Size</td>';
  echo '<td>Haircolor</td>';
  echo '<td>Eyecolor</td>'; 
  echo "</tr>"; 
  
  
foreach($_SESSION['cart'] as $value){ 
         $query = 'SELECT * FROM femalemodels_t WHERE ID='.$value;     
        $result = mysql_query($query);     
        $row = mysql_fetch_array($result); 
        if($result === FALSE) { 
    	die(mysql_error()); // TODO: better error handling
		} 
        echo '<tr>'; 
          echo '<td>'.$row['ID'].'</td>';  
          echo '<td>'.$row['m_name'].'</td>';  
          echo '<td>'.$row['m_lastname'].'</td>';  
          echo '<td>'.$row['m_size'].'</td>';
          echo '<td>'.$row['m_haircolor'].'</td>'; 
            echo '<td>'.$row['m_eyecolor'].'</td>'; 
        /*echo '<td ><a style="color:red;" href="lab20_AddBookmark.php?ID='.$row['ID'].'"><img src="icon_bookmark.gif" alt="delete link"/></a></td>';*/ 
          echo "</tr>"; 
    } 
    echo '</table>';  
    
?>

Help would be very much appreciated:)

Try doing a var_dump() on the session array to check what values you’re sending to MySQL in the WHERE clause.

btw @JimmyJoe are you aware that the old mysql_* extension was depreceated from version 5.5 of PHP and is being removed from version 7 of PHP? There’s two replacements for the old mysql_* extension, there’s the mysqli_* extension (MySQL Improved) and there’s PDO. Both have the facility for the use of prepared statements which is what should always be used when dealing with any data that might have been submitted by the user.

Show us the link, and the code you use to produce it. I’m no expert but I can’t see anything wrong above, which implies the link is sending the wrong value for ID.

Hi

I tried var_dump(). No it is really strange. I can add certain models, but then I add model it come the same again

Here my two updated codes:

    <?php
ob_start();
session_start();
include('connection.php'); 
// 2) When the linked to page is visited, add the specified item to the cart with a quantity of one...

// a) Validate the id received from the link.
$id = isset($_GET['ID']) ? (int)$_GET['ID'] : 0; // zero is not used/valid for a database auto-increment value that would be used as an id
if($id < 1){
    // not a valid id
    echo "The requested id is not valid";
} else {
    // the id is valid

    // b) If the cart doesn't exist, create an empty one.
    if(!isset($_SESSION['cart'])){
        $_SESSION['cart'] = array();
    }
    
    // c) Add the item to the cart with a quantity of one, if it is not already in the cart or increment the item quantity if it is already in the cart.
    if(!isset($_SESSION['cart'][$id])){
        // id is not in the cart, create an entry
        $_SESSION['cart'][$id] = 0; // initialize to zero (the next statement will set it to one)
    }
    $_SESSION['cart'][$id]++; // add one to the quantity (if the item was already in the cart with some quantity, this will increment the quantity)
}

// At this point, you are done with the add to cart task.
// You would either link/redirect back to the page that displays all the items
// or you could display the cart contents, by retrieving all the id's from the cart (see the array_keys() and implode() functions), then retrieve the information from the database table and loop over it to display it
// or you could incorporate this code into the main page, making a single page.
// for demo purposes, just display the session data -
header('location:viewreadinglist.php');
?>

Second code:

<?php
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);
session_start();
include('connection.php');
  var_dump($_SESSION['cart']);
  echo '<table >'; 

 foreach($_SESSION['cart'] as $value){ 
         $query = 'SELECT * FROM femalemodels_t WHERE ID='.$value;     
        $result = mysql_query($query);     
        $row = mysql_fetch_array($result); 
        echo '<tr>'; 
          echo '<td>'.$row['ID'].'</td>';  
          echo '<td>'.$row['m_name'].'</td>';  
          echo '<td>'.$row['m_address'].'</td>';  
          echo '<td>'.$row['m_phonenumber'].'</td>';
        /*echo '<td ><a style="color:red;" href="lab20_AddBookmark.php?ID='.$row['ID'].'"><img src="icon_bookmark.gif" alt="delete link"/></a></td>';*/ 
          echo "</tr>"; 
    } 
    echo '</table>'; 
?>

I really don’t have clue what the problem is. Any suggestions? Help would be very much appreciated:)

How exactly are you adding things? You’ve commented out the link that would add an item.

Hi

I add items (models) with a link.

Like this:

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

  echo '<tr>';
  echo '<div class="tableContent">';
  echo '<td>';?> <img src="<?php echo $row['path'];?>" height="142" width="100"> <?php echo "</td>"; 
  echo '<td>'.$row['ID'].'</td>';  
  echo '<td>'.$row['m_name'].'</td>';   
  echo '<td>'.$row['m_address'].'</td>'; 
  echo '<td>'.$row['m_phonenumber'].'</td>';
  echo '<td>'.$row['m_emailaddress'].'</td>'; 
   echo '<td>'.$row['m_size'].'</td>'; 
    echo '<td>'.$row['m_eyecolor'].'</td>'; 
     echo '<td>'.$row['m_haircolor'].'</td>';
     echo '<td>'.$row['m_bust'].'</td>'; 
     echo '<td>'.$row['m_waist'].'</td>';
     echo '<td>'.$row['m_hips'].'</td>';  
  echo '<td><a href="editfemalemodels.php?ID='.$row['ID'].'"><img src="EditIcon2.png" alt="edit link"/></a></td>'; 
  echo '<td ><a style="color:red;" href="deletefemalemodels.php?ID='.$row['ID'].'"><img src="DeleteIcon2.png" alt="delete link"/></a></td>'; 
  echo '<td ><a style="color:red;" href="readinglist.php?ID='.$row['ID'].'"><img src="AddIcon2.png" alt="create link"/></a></td>'; 
echo '</div>';
echo "</tr>"; 


}

But I don’t thing that is the problem. I think the problem is either in the session file or in the file where I display the items from the session cart.

So you define $result where in the page you posted in post #6? Showing me code from a different page doesnt do anything for THIS page.

Not sure what you mean. I pass on the id of the selected “item” to the sessions. The problem is that don’t know exactly how should define $result to display the session? Can you help me?

Okay, let me try coming at it from the other side to see if it makes more sense. When you look at the page with the ‘add’ link, do you see the correct item ID #'s next to each item?

Also is the first block of code you displayed in post #1 the code for readinglist.php?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.