Page 1 of 1

This code will not insert OR delete the records...

Posted: Thu Oct 09, 2003 4:33 am
by steedvlx
I would be ecstatic if someone would help me figure out what is wrong with my code.

When executed, The echo statements in this code reveal that the array is successfully loaded and the data is all where it should be. (when the insert and delete parts are commented out)

This routine is supposed to be an include that just formats the order data and adds it to the ORDERS table and then, if there was no error, deletes the old data from the CART table.

I have pored through the WROX book and the Visual guide to PHP to no avail. They don't tell me what rule(s) I am violating. I have commented out the insert and delete statements and the rest of it runs.


Code: Select all

<?php
for($counter=1; $counter<=$totalRows; $counter++)
{
		echo " - " . $usernum[$counter]; //numeric
		echo " - " . $user[$counter]; //alpha
		echo " - " . $line_num[$counter]; //numeric
		echo " - " . $part_num[$counter]; //alpha
		echo " - " . $e_desc[$counter]; //alpha
	  	echo " - " . $j_desc[$counter]; //alpha
		echo " - " . $line_price[$counter]; //numeric
		echo " - " . $quantity[$counter]; //numeric
		echo " - " . $line_total[$counter]; //numeric
		echo " - CARTID# -" . $cart_id_num[$counter]; //numeric
		echo " - " . $prod_id_index[$counter]; //numeric
		//The following lines insert the line data into the orders table
			$query = "INSERT INTO orders VALUES('$usernum[$counter]','$user[$counter]',NULL,'$line_num[$counter]','$part_num[$counter]','$e_desc[$counter]','$j_desc[$counter]','$line_price[$counter]','$quantity[$counter]','$line_total[$counter]','New','New',NULL,'Standard Air',curdate(),NULL,NULL,'0','0')";
    $result = mysql_query($query);
   if(!$result) { 
   		error_message(sql_error());
		$error_text="ERROR -  Add_Record_To_Order failed. Contact Customer Service";
		echo $error_text;
					}
	//The following lines remove the item from cart only if the above add to order is successful.
if !error_text {
   	$query_cart_del = "DELETE FROM cart WHERE cart_id_num = $cart_id_num[$counter]";
	$result_cart_del = mysql_query($query_cart_del);
	 if(!$result_cart_del) { 
	 			error_message(sql_error());
				$error_text = "ERROR - Delete_From_Cart failed. Contact Customer Service";
				echo $error_text;
									}
					}
		} 
?>


Thanks for any help
------------------------------------
SteedVLX
I know my code is amateurish, Because that's what I am

Posted: Thu Oct 09, 2003 5:20 am
by pootergeist
I presume sql_error is defined elsewhere, though would advise just using echo mysql_error() as an aid to debugging. try also echoing the $query string to check the quote syntaxes.

You may also note that numeric inserts should not really have the quotes around them so VALUES ($usernum[$counter],'$user[$counter]', etc would be better.

Posted: Thu Oct 09, 2003 5:50 am
by DuFF
Heres an example of what was stated by pootergeist

Code: Select all

<?php
$result=mysql_query($query) or die("Error: " . mysql_error());
?>
Also, are you getting any outputted errors, or does the script just not input any values into the database?

Posted: Thu Oct 09, 2003 6:03 am
by steedvlx
Thanks for the suggestions. I made the changes per your example.

The result of echoing the INSERT QUERY was perfect. Testing it in MySQL Control Center went perfectly. So far so good....

Right now, I am crying bitter tears. As I have spent all afternoon fighting this when I knew at least one version should have worked. Your suggestion to echo out the mysql_error was as obvious as it was revealing....Although I didn't know how to do it correctly. (Thanks DuFF)

Error: insert command denied to user: 'customer@127.0.0.1' for table 'orders'
8O

I think I'm going to get a drink now... A STRONG one.

Let's see if I can change permissions for this table in MySQL now and get on with my life. :roll:

Thanks for your time
----------------------------------------
SteedVLX