Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7410453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:14:12+00:00 2026-05-29T06:14:12+00:00

Ran into an issue today that I have not been able to resolve. I

  • 0

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. 😛

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T06:14:12+00:00Added an answer on May 29, 2026 at 6:14 am

    1) Try starting the session in addToCart.php

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Today I ran into an issue with LINQ to objects (not SQL) that popped
I ran into an issue today and I have been stumped for some time
I ran into an interesting issue today. I have canvas elements that I am
I'm automating some tasks and ran into an issue that I haven't been able
I ran into an interesting (and very frustrating) issue with the equals() method today
I have ran into an interesting issue while trying to create a more usable
I have ran into an issue where I am trying to ignore properties within
Has anyone ran into this issue? We have a mobile site that can't use
I migrated an SVN server today and ran into an issue. I have a
Today I ran into a scenario where I have to create a method that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.