首页 > 解决方案 > 从sql php动态删除加载iterm

问题描述

我正在尝试为学校项目制作一个非常基本的推车。这些项目是从数据库动态加载的,我希望能够在 POST 时动态删除它们,但我不知道如何动态跟踪 name 属性,所以我可以在我的删除查询中发布。

 <?php 

    require_once('dbconnection.php');
    session_start(); //starts seesion for username this needs to be at the top of every page.

    // head
    $page_title = 'Search Results';
    require_once('layouts/head.php');

    //Handles deleted items
    if (isset($_POST['delete'])) {
        $remove = $mysqli->prepare("DELETE cart.* FROM `cart` 
                                    inner join 'product'
                                    on product.id = cart.product_id
                                    WHERE `product_name` = ?");

        //TODO:get the post for the specific item name and add to bind method
        $remove->bind_param('s', $name);

        if(!$remove->execute() === true) {
            echo $mysqli->error;               
        }
    }


 <body>
    <!-- inlclude nav -->
    <?php require_once('layouts/nav.php'); ?>

    <main role="main">

      <!-- include jumbo -->
      <?php 
        require_once('layouts/jumbotron.php');
      ?>

      <div class="container">
        <!-- Container Heading -->
        <h1>
            <?php 
                if(isset($_SESSION['username'])){
                    echo $_SESSION['username']. "'s  Cart <hr>" ;

                }
                else{
                    echo "Your Cart <hr>";
                }
            ?>

        </h1>




        <ul class="panel panel-default">

            <?php 

                global $globalArr;
                $sum = 0;

                // TODO: check if user has an active cart if they do then display itemes
                $sql = "SELECT user.username, status.status_type, product.product_name, product.price, product.id  from cart
                        inner join status
                        on status.id = cart.status_id
                        inner join user
                        on user.id = cart.user_id
                        inner join product
                        on product.id = cart.product_id
                        WHERE status_type = 'purchased' AND username = '".$_SESSION['username']."'";

                $result = $conn->query($sql);

                // TODO: add a remove from cart
                if ($result->num_rows > 0) {
                        // output data of each row
                        echo "<h3>" .$result->num_rows. " item(s) in cart </h3>";


                        while($row = $result->fetch_assoc()) {

                            echo '<from action="cart.php" method="post"><li class="list-group-item" name="'.$row["product_name"].'">'
                                    .$row["product_name"]. ' - $'. $row["price"].'  <button class="badge" type="submit" name="delete"> X </button> </li></form>';
                            array_push($globalArr, $row["product_name"]);
                            $sum += floatval($row['price']);

                        }//END WHILE

                        echo $sum;

                } else {

                    echo "<h3> YOUR CART IS EMPTY! BUY SOMETHING! </h3>";
                }

                echo '</div>'


            ?>


        </ul>
        <hr>
      </div> 
      <!-- /container -->

    </main>

<!-- include footer -->
      <?php 
        require_once('layouts/footer.php');
      ?>

    <!-- Placed at the end of the document so the pages load faster -->
      <?php 
        require_once('layouts/scripts.php');
      ?>

  </body>
</html>

我认为我的帖子很清楚,但我被要求添加更多细节,所以这里有一些更酷的细节可能会有所帮助。我很难解决这个问题。您可能拥有的任何解决方案将不胜感激。感谢您抽出宝贵的时间变得如此出色,而不是让我的帖子发火。再次感谢!

标签: phpmysql

解决方案


我不确定我是否理解您的问题,但如果您想通过 POST 请求发送购物车项目的名称以删除,您可以为此使用隐藏输入

while($row = $result->fetch_assoc()) {
    echo '<from action="cart.php" method="post">'
        .'<li class="list-group-item" name="'.$row["product_name"].'">'
            .$row["product_name"]. ' - $'. $row["price"]
            .'<input type="hidden" name="item_name" value="'.$row["product_name"].'">'
            .'<button class="badge" type="submit" name="delete"> X </button>'
        .'</li>'
    .'</form>';
    array_push($globalArr, $row["product_name"]);
    $sum += floatval($row['price']);
}//END WHILE

然后使用$remove->bind_param('s', $_POST['item_name']);

希望这可以帮助


推荐阅读