Newbie---PhP MySQL INSERT query syntax problem...why?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
steedvlx
Forum Contributor
Posts: 122
Joined: Wed Jun 11, 2003 10:54 pm
Location: Osaka, Japan

Newbie---PhP MySQL INSERT query syntax problem...why?

Post by steedvlx »

I have written the following code for a simple shopping cart add function. (This is a learning site, and I know the code isn't really tight).

The problem I'm fighting now is that I keep getting a syntax error on the insert statement on line 33. The insert values are from the $row_cart_add array, and I believe they are assigned properly... Anyway could someone take a look and tell me why it keeps choking at the INSERT statement?

If you want to see it in action, the URL is http://www.sgthost.com. just log in as U=test P=test and try to add something to the cart. (The URL is on my personal server in asia, and make not load very quickly at all...sorry)

Any suggestions appreciated.

Here is the PHP code.

Code: Select all

<?php
<?php require_once('../Connections/kb_conn.php'); ?>
<?php
$colname_cart_add = "1";
if (isset($HTTP_GET_VARS['prod_id_index'])) {
	global $colname_cart_add;
  $colname_cart_add = (get_magic_quotes_gpc()) ? $HTTP_GET_VARS['prod_id_index'] : addslashes($HTTP_GET_VARS['prod_id_index']);
}
mysql_select_db($database_kb_conn, $kb_conn);
$query_cart_add = sprintf("SELECT * FROM products_tbl WHERE prod_id_index = %s", $colname_cart_add);
$cart_add = mysql_query($query_cart_add, $kb_conn) or die(mysql_error());
$row_cart_add = mysql_fetch_assoc($cart_add);
$totalRows_cart_add = mysql_num_rows($cart_add);
?>
<?php
			   global $error_text;
			   global $error_state;
			if (!$session_usernumber) {
			   		$error_state="1";		
					$error_text="You Must Log In to Use the Shopping Cart";
					echo "$error_text";
					}
			else {
				$query_cart = sprintf("SELECT * FROM cart WHERE cart_prod_id_index = $colname_cart_add and usernumber = $session_usernumber");
				$cart = mysql_query($query_cart, $kb_conn) or die(mysql_error());
				$row_cart = mysql_fetch_assoc($cart);
				$totalRows_cart = mysql_num_rows($cart);
			if ($totalRows_cart > 0) {
				$error_state="2";
				$error_text="That Item is Already in Your Cart or Wishlist";
				echo "$error_text";}
			else {
			$line_total = 'cart_add.cart_quantity' * 'cart_add.prod_sell_price_jpy';
			$query = "INSERT INTO cart VALUES (NULL, $session_usernumber,$session_user,'0',$row_cart_add[prod_id_index],$row_cart_add[prod_kb_part_num],'1',$row_cart_add[prod_sell_price_jpy],$row_cart_add[prod_e_desc],$row_cart_add[prod_j_desc],$line_total,curdate(),$row_cart_add[prod_discount_percent]";
			$result = mysql_query($query)or die(mysql_error());
			$error_state="0";
				$error_text="Successful!! Item has been added to your cart.";
				echo "$error_text";
			}
				} 
?>
?>
Thanks

---------------------
SteedVLX
User avatar
trollll
Forum Contributor
Posts: 181
Joined: Tue Jun 10, 2003 11:56 pm
Location: Round Rock, TX
Contact:

Post by trollll »

You may just have a problem with the datatype of $line_total. When doing math functions on variables, you don't want them enclosed in quotes. Otherwise, they'll get treated as strings.

So instead of:

Code: Select all

$line_total = 'cart_add.cart_quantity' * 'cart_add.prod_sell_price_jpy';
you'd have:

Code: Select all

$line_total = cart_add.cart_quantity * cart_add.prod_sell_price_jpy;
Also, I don't think you need quite so many "<?php"s and "?>"s. I don't think they caused a problem for you if it made it through over 30 lines of code, but just for readability's sake... :)
Last edited by trollll on Sat Jun 21, 2003 9:37 pm, edited 1 time in total.
User avatar
steedvlx
Forum Contributor
Posts: 122
Joined: Wed Jun 11, 2003 10:54 pm
Location: Osaka, Japan

Post by steedvlx »

Thanks for that trolll,

I fixed that according to your example. But, the main problem still exists. Anything wrong with the INSERT statement that you can see?

Thanks
------------------
SteedVLX
rfisk
Forum Newbie
Posts: 1
Joined: Sat Jun 21, 2003 10:16 pm

Re: Newbie---PhP MySQL INSERT query syntax problem...why?

Post by rfisk »

steedvlx wrote:$query = "INSERT INTO cart VALUES (NULL, $session_usernumber,$session_user,'0',$row_cart_add[prod_id_index],$row_cart_add[prod_kb_part_num],'1',$row_cart_add[prod_sell_price_jpy],$row_cart_add[prod_e_desc],$row_cart_add[prod_j_desc],$line_total,curdate(),$row_cart_add[prod_discount_percent]";
Am I missing something or are you?

Where's the closing paren?
User avatar
trollll
Forum Contributor
Posts: 181
Joined: Tue Jun 10, 2003 11:56 pm
Location: Round Rock, TX
Contact:

Post by trollll »

There we go! You see, the amount that I need to scroll to the right infuences just how useful (or useless) my postings end up as...
User avatar
steedvlx
Forum Contributor
Posts: 122
Joined: Wed Jun 11, 2003 10:54 pm
Location: Osaka, Japan

Post by steedvlx »

Thx rfisk et. al.

The code below seems to work correctly.

Code: Select all

<?php
$query = "INSERT INTO cart VALUES (NULL, $session_usernumber,'$session_user','0',$row_cart_add[prod_id_index],'$row_cart_add[prod_kb_part_num]','1',$row_cart_add[prod_sell_price_jpy],'$row_cart_add[prod_e_desc]','$row_cart_add[prod_j_desc]',$line_total,curdate(),$row_cart_add[prod_discount_percent])";
?>
It seems that this big, expensive book (Beginning PHP) leaves quite a bit of the 'beginner' details for you to work out yourself. I decided that 1) if the variable is inserted as a text type value, then I would try using single quotes around the variable name... 2) If the data is numeric, then I would use no quotes. I can't find it written anywhere (which means nothing actually) but, it seems to be the rule I was looking for.

Am I correct in this analysis of the situation or is something else going on?

------------------------
SteedVLX
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

correct

Post by phpScott »

no you would be correct in those assumptions.

there are sometimes little things that aren't explained properly and you will eventually figure it out or some one will help you with your problem.

phpScott
User avatar
steedvlx
Forum Contributor
Posts: 122
Joined: Wed Jun 11, 2003 10:54 pm
Location: Osaka, Japan

Post by steedvlx »

OK, thanks. I'm adding that syntax rule to my margin notes.
Post Reply