如何在现有值中添加数量值

时间:2019-02-10 15:23:54

标签: php html

我在php中添加了购物车代码。我创建了一个代码,当用户在购物车中选择一个现有产品时,现有产品的数量和输入的新数量将相互添加

    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "pos";
    $id=$name =$price=$quantity_order=$old_quantity_order = "";

    $conn = mysqli_connect($servername, $username, $password, $dbname);


    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        $id = $_POST['id'];
        $name = $_POST['name'];                 
        $price = $_POST['price'];
        $quantity_order = $_POST['quantity_order'];
        $subtotal = $price * $quantity_order;
    }
    $check="SELECT * FROM cart WHERE id = $id";
    $sql = mysqli_query($conn,$check) or die(mysql_error());
    if (mysqli_num_rows($sql) > 0) {
        while($res = mysqli_fetch_row($sql))
    {
        $old_quantity_order=$res['quantity_order'];
    }               
        $new_quantity_order =$quantity_order +  $old_quantity_order;
        $up = mysqli_query($conn, "UPDATE cart SET quantity_order='$new_quantity_order' WHERE id=$id");
    }
    else {

        $sql =mysqli_query($conn,"INSERT INTO cart (id,name, price,quantity_order,subtotal)
        VALUES ('$id', '$name', '$price','$quantity_order','$subtotal')");
    }

        header("Location:pos.php")

我试图获取数据库中的现有数量并将其重命名为$ old_quantity_order,然后将其添加到输入的数量中,即$ quantity_order。

但是进入我的数据库的唯一值是$ quantity_order的值,$ old_quantity_order是0。

这是我用来向购物车添加订购数量的表格。

<div class="form-group">        
<form method="post">
    <input type="hidden" name="id" value="<?php echo $id; ?>"/>
    <input type="hidden" name="name" value="<?php echo $name; ?>"/>
    <input type="hidden" name="price" value="<?php echo $price; ?>"/>
    <input type="hidden" name="quantity" value="<?php echo $quantity; ?>"/>
    <label>Quantity</label>
    <input class="form-control" type="number" name="quantity_order" placeholder="quantity"  min="1" max="<?php echo $quantity; ?>" />
    <input class="btn btn-primary" type="submit" name="add_to_cart" formaction="add_to_cart.php" id="add_to_cart" value="Add to Cart"  OnClick="return mess();">
    <input class="btn btn-success" type="submit" name="process" formaction="process_frompos.php" value="Process"></a>
</form>
</div>

1 个答案:

答案 0 :(得分:0)

运行以下代码,看看是否有PHP Errors

$servername = "localhost";
//it is BAD practise to connect to your DB as root. 
$username = "pubuser";
$password = "";
$dbname = "pos";
$id = $price = $quantity_order = $old_quantity_order = 0;
$name = "";

$conn = mysqli_connect($servername, $username, $password, $dbname);

//
// $_SERVER["REQUEST_METHOD"] is so common, and I don't like it as it's 
// not a clear check. Intead check that the array is populated. 
//
if (\is_array($_POST) && \count($_POST) > 0) {

    // Id is usually a digit number.  
    // So force to type. NEVER trust user input!
    $id = (int) $_POST['id'];

    $name = $_POST['name'];     
    // $price may be a float or an int?             
    $price = (float) $_POST['price'];
    $quantity_order = (int) $_POST['quantity_order'];
    $subtotal = $price * $quantity_order;
}

//
// So what happens here, if POST has not been set?
//

// Only grab the rows you need from the database:
$check="SELECT quantity_order FROM cart WHERE id = $id";

// 1) Mysqli_error needs the connection variable.
// 2) Errors shold be logged to an error log, NOT thrown to output.  
// 3) USE PREPARED STATEMENTS
$sql = mysqli_query($conn,$check) or error_log(__LINE__." : ".mysqli_error($conn));

if (mysqli_num_rows($sql) > 0) {

    // Because you're calling only one row, you don't need to loop it. 
    $res = mysqli_fetch_row($sql);

    $new_quantity_order = $quantity_order +  $res['quantity_order'];

    $up = mysqli_query($conn, "UPDATE cart SET quantity_order =
          ".(int)$new_quantity_order." WHERE id = ".$id );
}
else {
    // Numerical column values in SQL do not need to be encased in quotes 
    // and this slows down transactions.
    $sql = "INSERT INTO cart (id, name, price, quantity_order, subtotal)
    VALUES ( ".$id.", '".$name."', ".$price.", ".(int)$quantity_order." ,".$subtotal.")";

    mysqli_query($conn, $sql) or error_log(__LINE__." : ".mysqli_error($conn));
}

    // your header needs to have a semicolon ; at the end. 
    header("Location:pos.php");
    // header Locations MUST then be followed by an exit/die statement. 
    exit;

有用的链接: