duansao6776 2012-02-06 21:35
浏览 32
已采纳

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

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

  • 写回答

3条回答 默认 最新

  • doudui6756 2012-02-06 21:40
    关注

    1) Try starting the session in addToCart.php

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程