0

我正在尝试为我的购物车制作一个数量框,但不知道如何将值存储在我的数据库中。

<?php 
     $db = mysqli_connect('localhost', 'root', '', 'store');
     $select_product = mysqli_query($db, "SELECT * FROM cart WHERE id = 0");
     $select_product_values = mysqli_fetch_assoc($select_product);
     $product_quantity = select_product_values['quantity'];

     echo "<input type='text' maxlength='2' name='quantity' value=".$product_quantity." class='quantity_box'/>";
     echo "<form action='checkout.php' method='post'><button type='submit' id='checkout'>Checkout</button></form>";
     mysqli_close($db);
?>

结帐时最简单的方法是什么updatevaluequantity

4

1 回答 1

3

更新 -

要获取数量,您可以这样做:(固定 HTML):

 echo '
    <form action='checkout.php' method='post'>
        <input type="text" maxlength="2" name="quantity" value="'.$product_quantity.'" class="quantity_box"/>
        <button type="submit" id="checkout">Checkout</button>
    </form>';

检查从表单发送的值:

if(isset($_POST['quantity']) && !empty($_POST['quantity'])){
    $quantity = $_POST['quantity'];
    $updateCartSQL = "Update yourTable set quantity = '" . mysql_real_escape_string($quantity) . "'";
}

重要的!

确保在将数据插入数据库之前清除所有用户输入。更好的是,查看mysqli 准备好的语句以更加安全!

于 2013-04-24T01:31:43.377 回答