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 -
?>
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.
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');
?>
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.
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?