简体   繁体   English

每次在 php 中单击按钮时如何增加变量中的数字

[英]How to increase number in variable each time a button is clicked in php

I am creating a shopping cart which I have created using php.我正在创建一个使用 php 创建的购物车。 I have two buttons that when clicked allow the user to increase or decrease the quantity of the item.我有两个按钮,单击时允许用户增加或减少项目的数量。

When the user clicks the plus button the quantity goes up by one, however if they click it again nothing happens.当用户单击加号按钮时,数量会增加一,但是如果他们再次单击它,则不会发生任何事情。 is there a way that I can allow the user to say click the button until the quantity goes up to 20?有没有一种方法可以让用户说单击按钮直到数量达到 20?

Here is my code:这是我的代码:

This first part of my code checks to see if the original add to cart button has been clicked on an item.我的代码的第一部分检查是否已单击项目上的原始添加到购物车按钮。 If it is it then gets the ID of the item.如果是,则获取项目的 ID。 Once the item ID has been revived it them checks if a cart session has been created.恢复项目 ID 后,他们会检查是否已创建购物车 session。 If no shopping cart session has been found then it will create one.如果没有找到购物车 session,那么它将创建一个。 Once the session has been create it will then check for the quantity that has been add to the form.创建 session 后,它将检查已添加到表单中的数量。 Next it creates an array call item which will hold the ID and quantity for the item.接下来,它会创建一个数组调用项,其中将保存该项的 ID 和数量。

if(isset($_POST['add_to_cart']) && isset($_GET['id'])) {
    $id = $_GET['id'];

    // Check if there is a session for the cart set already, if not then set one.
    if(!isset($_SESSION['shoppingcart'])) {
        $_SESSION['shoppingcart'] = [];
    }

    $quantity = $_POST["amount"];

    $item = [
        "id"        => $id,
        "quantity"  => $quantity
    ];
    }

Next on the cart file the code checks if there is a cart session and that there is at least one item.接下来在购物车文件中,代码检查是否有购物车 session 并且至少有一件商品。 If there is at least one item in the cart then the code will one a for each loop to get each item from the database.如果购物车中至少有一件商品,那么代码将为每个循环一个 a 以从数据库中获取每件商品。

if(isset($_SESSION['shoppingcart']) && count($_SESSION['shoppingcart']) != 0) {
    $list = $_SESSION['shoppingcart'];
$total = 0;
    foreach($list as $item) {
        $id = $item['id'];

        $query = "SELECT * FROM item WHERE ItemID = '$id'";

        $result = mysqli_query($mysqli, $query);
        
    $row = $query_result->fetch_assoc();

        if (mysqli_num_rows($result) == 1){
            $row = mysqli_fetch_array($result);

            $id = $row['ItemID'];
            $name = $row['ItemName'];
            $image = $row['ItemImage'];
            $price = $row['Price'];
            $size = $row['Size'];
            $quantity = $item['quantity'];

            
        $subtotal = $price*$quantity;
        $total += $subtotal;
            
            ?>

HTML: HTML:

<form action="#" method="post">
  <h4>Quantity:  </h4>
  <input type="submit" value="+" name="plus" id="plus" /> 
  <input type="text" class="form-control" id="quanitiy" name="quantity" value= <?php echo "$quantity"; ?>>
  <button type="submit" name="minus"><i class="bi bi-dash"></i></button>
</form>

PHP: PHP:

 <?php if(isset($_POST['plus']) && ($quantity < 20)){ $_SESSION['quantity'] = $quantity++; }?>

I know that this code is in victim of sql injections, I will be fixing it once I get this part working.我知道这段代码是 sql 注入的受害者,一旦我让这部分工作,我将修复它。

$quantity doesn't seem to actually be defined anywhere in your code. $quantity似乎实际上并没有在您的代码中的任何地方定义。 It's a bit unclear how even the first increment works, based on what you've shown.根据您所展示的内容,甚至第一个增量如何工作也有点不清楚。 Perhaps you omitted some detail from the question?也许您从问题中省略了一些细节?

Based on the information available though, this would seem to be more logical:但是,根据可用的信息,这似乎更合乎逻辑:

<?php
session_start();

if(isset($_POST['plus'])) {
   $quantity = (isset($_SESSION['quantity']) ? $_SESSION['quantity'] : 0);

   if ($quantity < 20) {
     $quantity++; 
     $_SESSION['quantity'] = $quantity; 
   }
}

This will check for an existing value, and use that as the starting point to increment from.这将检查现有值,并将其用作增量的起点。 If there's no existing value it will start from 0.如果没有现有值,它将从 0 开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM