php - Add items in shopping basket to database -
i trying insert items in shopping basket table userorders within database. fields in mysql productid, quantity , ordertotal aware should using ssl.
i relatively new please kind me, appreciated.
the shopping basket:
<h1>view shopping basket</h1> <div class="container-fluid"> <div class="row"> <div class="col-lg-6"> <form method="post" value="placeorder" action="<?php echo htmlspecialchars($_server['php_self']); ?>" autocomplete="off"> <form method="post" value="update" action="checkout.php? page=cart"> <table class="table-responsive"> <thead> <tr> <th>productid</th> <th>name</th> <th>quantity</th> <th>price</th> <th>total</th> </tr> </thead> <?php //select products id in session $sql="select * products productid in ("; //for each session append id , add comma's seperate foreach($_session['cart'] $id => $val) { $sql.=$id.","; } //subtract last comma id's & append last bracket prevent error $sql=substr($sql, 0, -1).") order name asc"; $query=mysql_query($sql); $totalprice=00.00; $quantity =0; $productid = 'productid'; while($row=mysql_fetch_array($query)){ //running total $subtotal=$_session['cart'][$row['productid']] ['quantity']*$row['price']; //total price added each loop $totalprice+=$subtotal; ?> <tbody> <tr> <!--hidden productid--> <td><?php echo $row['productid'] ?></td> <!--display product name--> <td><?php echo $row['name'] ?></td> <!--display quantity--> <!--take 'productid' & 'quantity' rows, --> <td><input type="text" name="quantity[<?php echo $row['productid'] ?>]" size="2" value="<?php echo $_session['cart'] [$row['productid']]['quantity'] ?>" /></td> <!--display price--> <td><?php echo $row['price'] ?>£</td> <!--products price == quantity of productid in session * price --> <td><?php echo $_session['cart'][$row['productid']] ['quantity']*$row['price'] ?>£</td> </tr> <?php } ?> <tr> <td colspan="4" style="text-align:right">total price: <? php echo $totalprice ?></td> </tr> </tbody> </table> </div> </div> </div> <br /> <button type="submit" value="update" name="update">update shopping basket</button> <br /> <button type="submit" value="placeorder" name="placeorder">place order</button> </form> <br /> <p style="text-align:center">to remove item set quantity 0. </p> <a href="shopsesh.php?page=products"><p style="text-align:left">continue shopping</a></p> update quantity:
<?php //check form submitted, if yes & value ==0 unset session. if(isset($_post['submit'])){ foreach($_post['quantity'] $key => $val) { if($val==0) { unset($_session['cart'][$key]); //if form submit , value =! 0 update quantity }else{ $_session['cart'][$key]['quantity']=$val; } } } ?> insert query:
<?php //add items orders table in db if (isset($_post['placeorder'])) { //if no error if( !$error ) { $productid = $_post['productid']; $quantity = $_post['quantity']; //$_post['$totalprice']; //insert order database $query = "insert userorders(productid,quantity,ordertotal) values('$productid','$quantity','$totalprice')"; $res = mysql_query($query); if ($res) { $errtyp = "success"; $errmsg = "items added database"; } else { $errtyp = "danger"; $errmsg = "something went wrong, try again later..."; } } } ?>
Comments
Post a Comment