dongshuobei1037 2015-01-19 11:05
浏览 9
已采纳

根据条件保留所有先前的数据

I am trying to display two product names depending on a counter. I've declared a session variable 'counter'. I want to show the first product name if the button is clicked, and I want to show two product names if the button is clicked again.

Dell

Mac

However, if I click the button the second time, the second product name replaces the first product name; I want both products to display.

How would I fix this?

My code:

<?php
session_start();
include('connect.php');

// if counter is not set, set to zero
if (!isset($_SESSION['counter'])) {
    $_SESSION['counter'] = 0;
}

// if button is pressed, increment counter
if (isset($_POST['button'])) {
    ++$_SESSION['counter'];
    // Get data according to id 

    $userid2 = $_POST['id2'];
    $query3 = "select * from product where  id='" . $userid2 . "'";
    $result3 = mysql_query($query3) or die(mysql_error());
    $data3 = mysql_fetch_array($result3);

    if ($_SESSION['counter'] == 1) {
        echo $data3['name'] . "<br>";
    }

    if ($_SESSION['counter'] == 2) {
        echo $data3['name'];
    }
}

// reset counter
if (isset($_POST['reset'])) {
    $_SESSION['counter'] = 0;
}

$query2 = "select * from product ";
$result = mysql_query($query2) or die(mysql_error());

$data = mysql_fetch_array($result);

$sr = $data['sr'];
$name = $data['name'];
$price = $data['price'];
$qunt = $data['quantity'];

$total += $data['price'];

while ($data = mysql_fetch_array($result)) {
    ?>
    <form method="POST" action="">
        <table border="1">
            <tr>
                <?PHP
                echo "<tr><td>" . $data['id'] . "</td><td>" . $sr . "</td><td >" . $name . "</a></td><td>" . $price . "</td><td>" . $qunt . "</td> ";
                ?>
            <tr>
        </table>
        <input type="hidden" name="id2" value="<?php echo $data['id'] ?>"/>

        <input type="hidden" name="counter" value="<?php echo $_SESSION['counter']; ?>"/>
        <input type="submit" name="button" value="Counter"/>
        <input type="submit" name="reset" value="Reset"/>

        <br/><?php echo $_SESSION['counter']; ?>
    </form>
<?php }
?>
  • 写回答

2条回答 默认 最新

  • douben1891 2015-01-19 16:17
    关注

    When you make your second request, you're losing your current state; you no longer have access to the first product name. One way to fix this is to add a second session variable to keep track of products, as such:

    <?php
    session_start();
    include('connect.php');
    
    // if counter is not set, set to zero
    if (!isset($_SESSION['counter']) || !isset($_SESSION['products'])) {
        $_SESSION['counter'] = 0;
        // Create a new array in your session
        $_SESSION['products'] = array();
    }
    
    // if button is pressed, increment counter
    if (isset($_POST['button'])) {
        ++$_SESSION['counter'];
        // Get data according to id
    
        $userid2 = $_POST['id2'];
        $query3 = "select * from product where  id='" . $userid2 . "'";
        $result3 = mysql_query($query3) or die(mysql_error());
        $data3 = mysql_fetch_array($result3);
    
        // Add to your products session array
        $_SESSION['products'][] = $data3['name'];
    
        // Reduce to two products
        $_SESSION['products'] = array_slice($_SESSION['products'], -2, 2);
    
        // Print each product in your products array, max of two
        foreach($_SESSION['products'] as $name) {
            echo $name . "<br />";
        }
    }
    
    // reset counter
    else if (isset($_POST['reset'])) {
        $_SESSION['counter'] = 0;
        $_SESSION['products'] = array();
    }
    
    $query2 = "select * from product ";
    $result = mysql_query($query2) or die(mysql_error());
    
    $data = mysql_fetch_array($result);
    
    $sr = $data['sr'];
    $name = $data['name'];
    $price = $data['price'];
    $qunt = $data['quantity'];
    
    $total += $data['price'];
    
    while ($data = mysql_fetch_array($result)) {
        ?>
        <form method="POST" action="">
            <table border="1">
                <tr>
                    <?= "<tr><td>" . $data['id'] . "</td><td>" . $sr . "</td><td >" . $name . "</a></td><td>" . $price . "</td><td>" . $qunt . "</td> " ?>
                <tr>
            </table>
            <input type="hidden" name="id2" value="<?= $data['id'] ?>"/>
    
            <input type="hidden" name="counter" value="<?= $_SESSION['counter'] ?>"/>
            <input type="submit" name="button" value="Counter"/>
            <input type="submit" name="reset" value="Reset"/>
    
            <br/><?= $_SESSION['counter']; ?>
        </form>
    <?php }
    ?>
    

    Something to keep in mind: the use of mysql_* functions is deprecated, and frankly, dangerous to use in new development. You can't use prepared statements with those functions, which means that anyone can seriously mess with your database. Read more about it here.

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

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀