会话数组保持覆盖而不是添加到自身

会话数组保持覆盖而不是添加到自身

问题描述:

Ran into an issue today that I have not been able to resolve. I am trying to set up a very basic shopping cart for a project. I have a searchable form on the page searchFilm.php that will retrieve a list of 10 films based on your search criteria. This works without issue. I also have an "Add" button beside each film in the list, that also works well.

When I click "Add" it redirects to another page, as intended, called addToCart.php. This page will then display the information for the film added, which is Title and Rental Rate.

This also has worked without issue. Both pages use a central page call dbConnect.php to connect to and select from the database.

The issue I have run into is trying to create a session array that will hold the film_id of each film that I add, and add them to a table. It keeps overwriting the last value that was held in the array. I have commented out almost everything on the addToCart page to try and simplify my debugging. At this point it seems like I am perhaps starting a new session every time I click add.

I will provide the code for each page. I have been trying to figure this out for 4-5 hours without success. Hoping that another pair of eyes might see something I am missing.

Thanks.

dbConnect.php:

<?php

function connect($db)
{
    if(!$db)
    {
        die('Could not connect to the Sakila Database: ' . mysqli_error($db));
    }
    return $db;
}

function select($db, $table, $id)
{
    $result = mysqli_query($db, "SELECT * from " . $table . " where film_id = '" . $id . "'");
    if(!$result)
    {
        die('Could not retrieve records from the Sakila Database: ' . mysqli_error($db));
    }
    return $result;
}

function searchResult($db, $table, $term)
{
    $result = mysqli_query($db, "SELECT * from " . $table . " where description LIKE ('%" . $term . "%') LIMIT 0,10");
    if(!$result)
    {
        die('Could not retrieve records from the Sakila Database: ' . mysqli_error($db));
    }
    return $result;
}
?>

searchFilm.php:

<html>
<head>
    <title>TITLE!</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<?php

include'dbConnect.php';
session_start();

if(isset($_POST['search']))
{
    $term = $_POST['search'];

    //connect to the database
    $db = connect(mysqli_connect("localhost","root","","sakila"));

    //retrieve results from the database
    $result = searchResult(mysqli_connect("localhost","root","","sakila"),'film', $term);

    //echo the title and description of each row
    echo "<table border=1 bordercolor=red>";
    echo "<tr>";
    echo "<th>Title</th>";
    echo "<th>Description</th>";
    echo "<th>Add To Cart</th>";
    echo "</tr>";
    while($row = mysqli_fetch_assoc($result))
    {
        echo "<tr>";
        echo "<td>" . $row['title'] . "</td> <td>" . $row['description'] . "</td>";
    ?>
        <td>
        <form name="addToCart" action="addToCart.php" method="POST">
            <input type="hidden" name="filmID" value="<?php echo $row['film_id']; ?>" />
            <input type="submit" name="addToCart" value="Add" />
        </form>
        </td>
    <?php
        echo "</tr>";
    }
    echo "</table>";

    mysqli_close($db);
}
?>
    <form method="post" action="searchFilm.php" name="">
        <p>Search:
            <input name="search" type="text" value="" />
        </p>
        <p>
            <input name="" type="submit">
        </p>
    </form>
</body>
</html>

addToCart.php:

<?php
include('dbConnect.php');

if(isset($_POST['filmID']))
{

    $id = $_POST['filmID']; //the item selected


    $_session['cart'][] = $id;


    foreach ($_session['cart'] as $item) 
    { //display contents of array
        echo "$item<br />";
    }

    /*$filmid = $_POST['filmID'];

    $_SESSION['cart'][$filmid];

    $db = connect(mysqli_connect("localhost","root","","sakila"));

    $select = select(mysqli_connect("localhost","root","","sakila"),'film', $filmid);

    echo "<table border=1 bordercolor=red>";
    echo "<tr>";
    echo "<th>Film</th>";
    echo "<th>Rental Rate</th>";
    echo "</tr>";
    while($row = mysqli_fetch_assoc($select))
    {
        echo "<tr>";
        echo "<td>" . $row['title'] . "</td> <td>" . $row['rental_rate'] . "</td>";
        echo "</tr>";
    }
    echo "</table>";*/
}
?>
<html>
<head>
    <title>TITLE!</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
    <a href="searchFilm.php">click to go back</a>
</body>
</html>

Sorry for the length. Just wanted to make sure that all information was there.

Any insight would be appreciated.

Thanks!

PS. I know my database is very insecure. It's just full of dummy data and run every once in a while on a VM, so I don't really care. :P

1) Try starting the session in addToCart.php

2) As far as I know, $_session won't work, it should be $_SESSION

addToCart.php should call session_start(); and it doesn't as far as I can see.

I believe the issue is that there doesn't appear to be a call to session_start() in the addToCart.php file.

Since you aren't starting a session, none of the previous data is available. Essentially you are creating an array called $_SESSION and adding your cart array to it.

This results in using an array with the same name as PHP's session array, but it is not based off of an existing session.