[英]Store input value into database
我正在嘗試為購物車制作一個數量框,但不知道如何將值存儲在數據庫中。
<?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
quantity
value
的最簡單方法是什么?
要獲取數量,您可以執行以下操作:(固定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准備好的語句以提高安全性!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.