Dany
Dany

Reputation: 481

Store input value into database

I'm trying to make a quantity box for my shopping cart but don't know how to store the value in my 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);
?>

What would be the simplest way to update the value of quantity on checkout?

Upvotes: 0

Views: 685

Answers (1)

What have you tried
What have you tried

Reputation: 11148

Update -

To grab the quantity, you would do this: (Fixed 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>';

Check the value sent from the form:

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

Important!

Make sure you clean any user input before inserting the data into the database. Better yet, check out mysqli prepared statements to be extra secure!

Upvotes: 3

Related Questions