mikebyrne Posted April 9, 2008 Share Posted April 9, 2008 I want to add a purchase button to my cart. I've a rough idea of what I need to put into the fucntion below but im really not sure how to structure it I know I need to insert SQL2= UPDATE $table SET stockamount = stockamount - 1 WHERE ProductNo = $id A purchase button: '<div><button type="submit">Purchase</button></div>'; A Form <form action="cart.php?action=purchase" method="post"> <input type="submit" name="purchase" value="Purchase" /> </form> Any help here would be great <?php function writeShoppingCart() { $cart = $_SESSION['cart']; if (!$cart) { return '<p>You have no items in your shopping cart</p>'; } else { // Parse the cart session variable $items = explode(',',$cart); $s = (count($items) > 1) ? 's':''; return '<p>You have <a href="cart.php">'.count($items).' item'.$s.' in your shopping cart</a></p>'; } } function showCart() { global $db; $cart = $_SESSION['cart']; if ($cart) { $items = explode(',',$cart); $contents = array(); foreach ($items as $item) { $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1; } $output[] = '<form action="cart.php?action=update" method="post" id="cart">'; $output[] = '<table>'; foreach ($contents as $id=>$qty) { $sql = 'SELECT * FROM product WHERE ProductNo = '.$id; $result = $db->query($sql); $row = $result->fetch(); extract($row); $output[] = '<tr>'; $output[] = '<td><a href="cart.php?action=delete&id='.$id.'" class="r">Remove</a></td>'; $output[] = '<td>'.$ProductName.' by '.$Productinfo.'</td>'; $output[] = '<td>€'.$Price.'</td>'; $output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>'; $output[] = '<td>€'.($Price * $qty).'</td>'; $total += $Price * $qty; $output[] = '</tr>'; } $output[] = '</table>'; $output[] = '<p>Grand total: <strong>€'.$total.'</strong></p>'; $output[] = '<div><button type="submit">Update cart</button></div>'; $output[] = '</form>'; } else { $output[] = '<p>You shopping cart is empty.</p>'; } return join('',$output); } ?> Quote Link to comment Share on other sites More sharing options...
mikebyrne Posted April 10, 2008 Author Share Posted April 10, 2008 Any help here would be great Quote Link to comment Share on other sites More sharing options...
tinker Posted April 10, 2008 Share Posted April 10, 2008 What's your actual question, i'm none too clear? What payment method are you eventually using. Other than that you seem to do everything (if duplicate some bits), you got price, qty, name, info, is there a product ref? It generally depends upon what your products are and how you later allow to update the cart, and how that is then transferred into an order, then sent off to the credit site, and how you store their specific info, then once (if they remember to) they return what needs to be done? (p.s. sorry about waffling, just got up!) Quote Link to comment Share on other sites More sharing options...
mikebyrne Posted April 10, 2008 Author Share Posted April 10, 2008 No problem. It not going live its just a project im doing there forefore I dont need the actual payment info etc I just really want the database updated ie stock reduced when the created "Purchase" button is clicked Quote Link to comment Share on other sites More sharing options...
paul2463 Posted April 10, 2008 Share Posted April 10, 2008 something like this?? function reduceItem($id, $qty) { $query = "UPDATE tablename SET quantity = quantity - '$qty' WHERE tableid = '$id'"; if ($result = mysql_query($query)) { $rows = mysql_affected_rows($result);//should be equal to 1 or 0 as an id is being used } else { $rows = 0; // unable to carry out query set affected rows to 0 } return $rows; } $amended = reduceItem(14, 1); //reduce item 14 by 1 echo $amended; //1 if the reduction has happened, 0 if no rows affected or error with the query Quote Link to comment Share on other sites More sharing options...
mikebyrne Posted April 10, 2008 Author Share Posted April 10, 2008 Yeah i'm a little confused with item 14? The primary key is the ProductNo would you be better using that in the reduction? Quote Link to comment Share on other sites More sharing options...
paul2463 Posted April 10, 2008 Share Posted April 10, 2008 yes you would pass the function the ID number of the item you want to change, I chose 14 as it is a nice number and I like it and it has no bearing on your problem Quote Link to comment Share on other sites More sharing options...
mikebyrne Posted April 10, 2008 Author Share Posted April 10, 2008 Ok thanks for your help. One last thing, how would I apply the code to the "purchase" button I'm not to familair with calling functions? A purchase button: '<div><button type="submit">Purchase</button></div>'; <form action="cart.php?action=purchase" method="post"> <input type="submit" name="purchase" value="Purchase" /> </form> Quote Link to comment Share on other sites More sharing options...
paul2463 Posted April 10, 2008 Share Posted April 10, 2008 you would not call the function direct, you would post the values back to the page and have the function at the top of the page, first of all checking to see if the posted value match what is needed if (isset($_POST['purchase'])) { $amended = reduceItem($_POST['id'], $_POST['qty']); if ($amended == 1) { echo "Table updated"; } else { echo "For some reason table not updated"; } unset $_POST['id']; // to prevent refresh unset $_POST['qty']; } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.