Jump to content

Updating mysql form


graham23s

Recommended Posts

Hi Guys,

 

Th eshopping cart page im making is working well, i am now trying to update the quantity (as the user selects) they press update cart and then i was going to update mysql accordingly, first the code:

 

<?php
session_start();

include("inc/dbconnection.php");
include("inc/online.php");
include("inc/header.php");
include("inc/navigation.php");
?>
<?php
// standard header //
print("<div class=\"subheader\"><div id=\"title\">Home > <span class=\"blue\">Items you have added to your shopping cart</span></div>You can delete and update your shopping cart as you wish.</div>");

// grab the cart update info //
//if(isset($_POST['submit']))
//{

// vars //
// $the_product_id = $_POST['q'];
// echo $the_product_id;

//} 

// check for the users logged in session if there is none no items in the cart //
if(!isset($_SESSION['id']))
{

  print("<div id=\"shopping_login_error\">We are sorry but you need to be logged in to add orders. you can login <a href=\"login.php\">here</a> or register <a href=\"register.php\">here</a>.");
  include("inc/footer.php");
  exit;
  
} else {
  
// they are logged in so insert and display the cart items // 

// firstly insert the items into the database //

// vars //
$product_id = $_GET['productid'];
$quantity = $_GET['orderquantity'];
$var_loggedinuserid = $_SESSION['id'];

// is the isset set //
//if(isset($product_id) && isset($quantity) && isset($var_loggedinuserid))
//{

// do a quick check to see if the product has already been added if so just update the quantity //
$queryalreadyinserted = "SELECT `product_id`,`qty` FROM `fcp_orders` WHERE `product_id`='$product_id'";
$resultsalreadyinserted = mysql_query($queryalreadyinserted);

// already in the cart ? //
$already_in_cart = mysql_num_rows($resultsalreadyinserted);

// do the math //
$rowqty = mysql_fetch_array($resultsalreadyinserted);

// current quantity //
$cur_qty = $rowqty['qty'];

// new quantity //
$new_quantity = $cur_qty + $quantity;

if($already_in_cart > 0)
{

// just update the quantity //
$queryqty = mysql_query("UPDATE `fcp_orders` SET `qty`='$new_quantity' WHERE `product_id`='$product_id'");

} else {

if($product_id == 0)
{

//

} else {

// insert the products into mysql //
$queryinsertion = "INSERT INTO `fcp_orders` (`id`,`customer_id`,`product_id`,`qty`,`date`) VALUES ('','$var_loggedinuserid','$product_id','$quantity',now())";
$resultsinsertion = mysql_query($queryinsertion);

}

}

// now query the database to see what the logged in user has added //
$querycart = "SELECT * FROM `fcp_orders` WHERE `customer_id`='$var_loggedinuserid'";
$resultcart = mysql_query($querycart);

// any results back //
$num_products = mysql_num_rows($resultcart);

// display if there is or isn't //
if($num_products == 0)
{

print("<div align=\"left\" id=\"empty_shopping_cart\">Your shopping cart is empty. <img src=\"images/cartempty.gif\" alt=\"Your shopping cart is empty!\"></div>");
include("inc/footer.php");
exit;
  
} else {

// -- UPDATE --//
if($_GET['action'] == 'update')
{
  $quantity_to_update = $_POST['q'];
  echo $quantity_to_update;
}

// there is products there so display them //
print("<form action=\"shoppingcart.php?action=update\" method=\"post\">");
print("<table width=\"95%\" class=\"shop_table\" border=\"1\" bordercolor=\"#3399CC\" cellpadding=\"5\" cellspacing=\"0\">\n");
print("<tr>\n");
print("<td align=\"center\" class=\"shop_header\">Image</td><td align=\"center\" class=\"shop_header\">Description</td><td align=\"center\" class=\"shop_header\" >Quantity</td><td align=\"center\" class=\"shop_header\">Total Price</td><td align=\"center\" class=\"shop_header\">Delete</td>\n");
print("</tr>\n");
print("<tr>\n");

// loop //
while($row = mysql_fetch_array($resultcart))
{

$product_id = $row['product_id'];
$product_qty = $row['qty'];

// get the product information //
$queryproductinformation = "SELECT * FROM `fcp_products` WHERE `id`='$product_id'";
$resultproductinformation = mysql_query($queryproductinformation);

// make an array //
$rows = mysql_fetch_array($resultproductinformation);

// more vars //
$product_name = $rows['product_name'];
$product_price = $rows['product_price'];

// math //
$total_price = $product_price * $product_qty;

// session in  avar //
$session_id = $_SESSION['id'];

// put the total quantity in mysql besides the order //
$finalupdate = mysql_query("UPDATE `fcp_orders` SET `qty_total`='$total_price' WHERE `product_id`='$product_id'");

print("<td width=\"5%\" align=\"center\"><img src=\"images/image.gif\" alt=\"Product Image\" title=\"Product Image\" /></td><td align=\"center\"><a href=\"productinformation.php?productid=$product_id\">$product_name</a></td><td align=\"center\"><input type=\"text\" name=\"q\" size=\"5\" value=\"$product_qty\"></td><td width=\"10%\" align=\"center\">£$total_price</td><td align=\"center\"><a href=\"shoppingcart.php?action=remove\"><img src=\"images/button_delete.gif\" border=\"0\"></a></td></tr>\n");

}

// get the total price due //
$queryprice = "SELECT SUM(qty_total) as `total` FROM `fcp_orders` WHERE `customer_id`='$session_id'";
$resultsprice = mysql_query($queryprice);
$r = mysql_fetch_array($resultsprice);

// vars //
$total_due = $r['total'];

// shipping //
$shipping_costs = 10;

// total owed //
$total_owed = $total_due + $shipping_costs;

// end the table //
print("<tr>");
print("<td colspan=\"5\" align=\"right\"><input type=\"submit\" name=\"submit\" value=\"Update Cart\"></td>");
print("</tr>");
print("<tr>");
print("<td colspan=\"4\" align=\"right\">Sub Total -</td><td align=\"left\"><b>£$total_due</b></td>");
print("</tr>");
print("<tr>");
print("<td colspan=\"4\" align=\"right\">Shipping -</td><td align=\"left\"><b>£$shipping_costs</b></td>");
print("</tr>");
print("<tr>");
print("<td colspan=\"4\" align=\"right\">Total -</td><td align=\"left\"><b>£$total_owed</b></td>");
print("</tr>");
print("</table></form>");

//}


} // end the first if //

} // end quantity else //

// this code deletes orders from the database older than 1 day //
$yesterday = date('Y-m-d H:i:s',mktime(0,0,0, date('m'), date('d') - 1, date('Y')));
$deletecartentries = mysql_query("DELETE FROM `fcp_orders` WHERE `date` < '$yesterday'");
?>
<?php
// include the footer //
include("inc/footer.php");
?>

 

when i grab the GET to update:

 

 // -- UPDATE --//
if($_GET['action'] == 'update')

 

only the last entry is shown regardless of what quantity is amnually updated, is there a way i can only update the quantity changed by the user at all?

 

thanks for any help

 

Graham

Link to comment
https://forums.phpfreaks.com/topic/91137-updating-mysql-form/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.