保存会话数据不起作用.PHP中的会话


Saving Session Data is not working . SESSIONS IN PHP

这段代码应该在我点击保存按钮后保存数据,但它没有保存,你知道发生了什么吗?

脚本名称为:prueba.php

 <?php
 session_start();
 $save=$_POST['save'];

这部分是计算两个输入相加的函数

 function calculate_price($cart)
 {
  $price = 0.0;
     if(is_array($cart))
     {
             foreach($cart as $isbn => $qty)
             {
                 $price +=$qty;
              }
      }
   return $price;
  }

也就是说,如果没有点击按钮保存

   if(!$save)    
  {
  $cart=array("jonathan" => 30, "andrea" => 40);
  $_SESSION["cart"]=$cart;
  }

这是为了防止我们点击"保存"按钮

  if($save)    
  {
    foreach($_SESSION["cart"] as $isbn => $qty) 
    {
             echo " ISBN : ".$isbn;
        if($qty=="0")
            { echo "borraste";
             unset($_SESSION["cart"][$isbn]);}
         else
            $_SESSION["cart"][$isbn] =$qty; 
     }

   }  

这是我用来输入值的表格,所以点击"保存"后可以更改

   foreach ($_SESSION["cart"] as $isbn => $qty)
      {
   ?>
        <table>
        <form action ="prueba.php" method ="post">
        <tr>
        <td>Value of <?php echo $isbn ?></td>
        <td><input type = "text" name ="<?php $isbn ?>" value ="<?php echo $_SESSION["cart"]        [$isbn] ?>" size ="3"></td>
        </tr>
  <?php
        }

    ?>
    <tr><td><input type="submit" name="save" value="Save"></td></tr>
        </form></table>
   <?php

这只是为了检查会话是否存在,并且在阵列上是否有值

   if($_SESSION['cart']&&array_count_values($_SESSION['cart']))
   {
   $_SESSION["total_price"] = calculate_price($_SESSION["cart"]);
   echo "The total amount is :  ".$_SESSION["total_price"];
   }
   ?>

我真的很感谢你帮我检查一下,并找到解决方案,我想错误可能在表格上,感谢您的回答

问题是你没有得到你的post值。更好的解决方案是创建一个分组数组名称,这样更易于管理。例如:qty[<?php echo $isbn; ?>]。这是工作样本

// initial setup
if(!isset($_SESSION['cart'])) {
    $_SESSION['cart'] = array("jonathan" => 30, "andrea" => 40);
}
if(isset($_POST['save'])) { // is button is saved
    $qty = $_POST['qty']; // get the grouping array
    foreach($qty as $isbn => $value) { // loop it
        if(isset($_SESSION['cart'][$isbn])) { // so now you can use they keys as isbn
            if($value == 0) { // if zero then unset
                unset($_SESSION['cart'][$isbn]);
                continue;
            }
            $_SESSION['cart'][$isbn] = $value; // if not then set qty
        }
    }
}
?>
<form method="POST" action="">
    <table border="0">
        <?php foreach($_SESSION['cart'] as $isbn => $value): ?>
        <tr>
            <td>Value of <?php echo $isbn; ?></td>
            <td>
                <input style="text-align: center;" type="text" name="qty[<?php echo $isbn; ?>]" value="<?php echo $value; ?>" size="3" />
            </td>
        </tr>
        <?php endforeach; ?>
        <tr>
            <td colspan="2" align="right">
                <input type="submit" name="save" value="Save" />
            </td>
        </tr>
    </table>
</form>
<div class="total">
    <!-- total -->
    <p>The total is: <?php echo array_sum($_SESSION['cart']); ?></p>
</div>

您的代码中几乎没有错误首先,您在循环中打印表单操作,这会导致无效的html代码其次,在会话中存储数据时,您使用的是cookie中已经存在的旧值我修改了代码并测试了

             <?php
             session_start();
             $save=$_POST['save'];
             function calculate_price($cart)
             {
              $price = 0.0;
                 if(is_array($cart))
                 {
                         foreach($cart as $isbn => $qty)
                         {
                             echo $qty . "<br>";
                             $price +=$qty;
                          }
                  }
               return $price;
              }

              if(!$save)    
              {
              $cart=array("jonathan" => 30, "andrea" => 40);
              $_SESSION["cart"]=$cart;
              }
             if($save)    
              {
                $cart=array("jonathan" => $_POST['jonathan'], "andrea" => $_POST['andrea']);
                foreach($cart as $isbn => $qty) 
                {
                         echo " ISBN : ".$isbn;
                    if($qty=="0")
                        { echo "borraste";
                         unset($_SESSION["cart"][$isbn]);}
                     else
                     {
                        //$cart=array($isbn => $qty);
                        $_SESSION["cart"][$isbn] = $qty; 
                        echo $qty;
                     }
                 }
               }
               ?>

                <form action ="prueba.php" method ="post">
                    <table>
              <?php
              foreach ($_SESSION["cart"] as $isbn => $qty)
                  {
               ?>
                    <tr>
                    <td>Value of <?php echo $isbn ?></td>
                    <td><input type = "text" name ="<?php echo $isbn ?>" value ="<?php echo $_SESSION["cart"]        [$isbn] ?>" size ="3"></td>
                    </tr>
              <?php
                    }

                ?>
                <tr><td><input type="submit" name="save" value="Save"></td></tr>
                    </table>
                    </form>
               <?php
               if($_SESSION['cart'] && array_count_values($_SESSION['cart']))
               {
               $_SESSION["total_price"] = calculate_price($_SESSION["cart"]);
               echo "The total amount is :  ".$_SESSION["total_price"];
               }
               ?>