【问题标题】:tried to remove the item from the cart but the code is not working [duplicate]试图从购物车中删除该项目,但代码不起作用[重复]
【发布时间】:2013-10-10 06:01:36
【问题描述】:

在这个 index_to_remove 是通过表单中的隐藏输入类型来的...... 我创建了一个带有删除按钮的表单,并通过一个隐藏的输出字段传递了我想从购物车中删除的项目的索引并实现了这段代码。但它不起作用.......

     <?php
         /////////////////////////////////////////////////////////
        // if user wants to remove an item from cart
        ////////////////////////////////////////////////////////
     if(isset($_POST['index_to_remove']) && $_POST['index_to_remove']="" )
      {
      //access the array and rum code to remove that array index
        $key_to_remove=$_POST['index_to_remove'];
              if(count($_SESSION['cart_array'])<=1)
              {
                   unset($_SESSION['cart_array']);
                   sort($_SESSION['cart_array']);
               }
       else
          {
                   unset($_SESSION["cart_array"][$key_to_remove]);
                   sort($_SESSION['cart_array']);
                   echo count($_SESSION['cart_array']);
         }
     }

        ?>

【问题讨论】:

  • 我不懂 PHP,但$_POST['index_to_remove']="" 对我来说似乎是一项任务。
  • 在顶部的'if' 语句中,使用$_POST['index_to_remove']!=""
  • 请定义您所说的“不工作”是什么意思,回复人们的 cmets/answers,而不是发布相同的问题 twicethrice

标签: php session multidimensional-array


【解决方案1】:
&& $_POST['index_to_remove']=""

应该是:

&& $_POST['index_to_remove'] == ""
  • = 单个等于用于为任何变量分配一些值
  • == 比较变量的值是否相等
  • === 检查两个变量是否属于同一类型并且具有相同的值

【讨论】:

    【解决方案2】:

    改变这一行:

    if(isset($_POST['index_to_remove']) && $_POST['index_to_remove']="" ) // WRONG
    

    对此:

    if(isset($_POST['index_to_remove']) && $_POST['index_to_remove'] != "" )
    

    请看THIS

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-02-18
      • 1970-01-01
      • 2023-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多