我正在尝试为我的购物车制作数量框,但不知道如何将该值存储在我的数据库中。
<?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);
?>
结帐时update
value
quantity
的最简单方法是什么?
答案 0 :(得分: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 prepared statements以确保安全!