Jump to content

SQL cannnot update data from php session


gongpex
 Share

Recommended Posts

Hello everyone, please see these images on bellow : first-step.gif When I click "BUY NOW", then I login into account it's successful to added to database. And this the result : before.gif But when I buy same or other product like this : sec-step.gif it's result like this : after.gif it cause the product quantity and total price became 0 and sadly the new product cannot be added. and this my code :

if($_SESSION['cart']) {   $show_order = mysql_query("select * from order where o_user='$m_user'and o_status='O'");	$order_all = mysql_num_rows($show_order);    foreach($_SESSION['cart'] as $product_id => $quantity) {  	$sql = sprintf("SELECT p_id, p_title, p_price, p_image FROM product WHERE p_id = %d;",		$product_id);		$result = mysql_query($sql);  	 if(mysql_num_rows($result) > 0 && $order_all == 0 && $_SESSION['m_user']) {		 list($p_id, $p_title, $p_price, $p_img) = mysql_fetch_row($result);  	 $line_cost = $p_price * $quantity;	 $total = $total + $line_cost;		 mysql_query("insert into order(pid,o_user,o_title,o_img,o_price,o_qty,o_total)					values				   ('$p_id','$m_user','$p_title','$p_img','$p_price','$quantity','$line_cost')");	}	else if(mysql_num_rows($result) > 0 && $order_all !== 0){			  mysql_query("update order set o_qty ='$quantity',o_total='$line_cost'	 where o_user='$m_user' and o_status='O'");	}     }}

E : I know the cause of change the price and quantity into 0 and the new product can't be added because this code:

if(mysql_num_rows($result) > 0 && $order_all == 0 && $_SESSION['m_user']) {  	 list($p_id, $p_title, $p_price, $p_img) = mysql_fetch_row($result);  	 $line_cost = $p_price * $quantity;	 $total = $total + $line_cost;  	 mysql_query("insert into order(pid,o_user,o_title,o_img,o_price,o_qty,o_total)					values				   ('$p_id','$m_user','$p_title','$p_img','$p_price','$quantity','$line_cost')");	}	else if(mysql_num_rows($result) > 0 && $order_all !== 0){			  mysql_query("update order set o_qty ='$quantity',o_total='$line_cost'	 where o_user='$m_user' and o_status='O'");	}

Q : But how to fix this code, in short how to create sql/mysql code so that : Case1 : if the product not exist on user account / database , it will do : mysql_query("insert into order(pid,o_user,o_title,o_img,o_price,o_qty,o_total) values ('$p_id','$m_user','$p_title','$p_img','$p_price','$quantity','$line_cost')"); so that when user buy new product it can be added though user has buy some product before. and Case2 : if the product already exist on user account / database , it will do : mysql_query("update order set o_qty ='$quantity',o_total='$line_cost' where o_user='$m_user' and o_status='O'"); so that when user buy same product it can be change the quantity and total price, based on product that user had buy before. ?? Important : I buy product when in condition not login in other word as anonymous, and this image shown after I logged as user. so, image on above displayed based on database not on session. please help me...... Thanks

Link to comment
Share on other sites

We probably can't give you the answer you need except to say your code is doing what you told it to do not what you intended it to do unless you're getting an error message. . So, when I have similar situations, I make sure the expected value inputs are present. If so, go through each line of code to decide why the math doesn't add-up. If this is your code it shouldn't be too hard.

Edited by niche
Link to comment
Share on other sites

You're checking if the user has any products at all in the order. If they do, you only update, never insert. So if they have 2 products already, and add another one, you're only updating and not inserting. You need to check if each product is in the order instead of checking if there are any products in the order at all.

Link to comment
Share on other sites

You need to check if each product is in the order instead of checking if there are any products in the order at all.
Yes of course, but I don't know how to check it, if I using : mysql_query("select * from order where o_user='$m_user'and o_status='O' and p_id='$product code'"); note : $product code just an example. it can only check once, Q : how about product that ordered by user on account if the product more than 1? please help me Thanks
Link to comment
Share on other sites

I think I need to check my code, yesterday I found some code that wrong.____________________________________________________________________________ off : your photo on avatar is in your home or other place like your office or etc? thanks

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...