Page 1 of 1

help me in sql queries

Posted: Fri Mar 04, 2005 8:47 am
by elle_girl

Code: Select all

if ($result) { 
			
			$data = "SELECT itemId, qty, price, cartId FROM cart ";
			$result1 = @mysql_query($data) or die (mysql_error());
			 
			 if ($row = mysql_fetch_array($result1)) {
				$itemId = $rowї'itemId'];
				$qty = $rowї'qty'];
				$price = $rowї'price'];
				$cartId = $rowї'cartId'];
             				$query1 = "INSERT INTO view (itemId, price, qty, cartId) VALUES ('$itemId','$price','$qty','$cartId' )"; 
            				$result2 = mysql_query($query1) or die(mysql_error());// Run the query. 

				if ($result2) {
					$insert = "UPDATE view SET account_no = '$aa', token_id = '$t', name = '$n', address = '$ad', state = '$s', country = '$c' WHERE cartId = '$cartId'";
					$result_insert = mysql_query ($insert) or die(mysql_error());
				} else {
					echo '<p><font color="red" size="+1">Cannot update to the database</font></p>';
	
				&#125;
				
				// Successful add the new customer. 
         				echo '<h3>Successful the transaction</h3>'; 
        				include ('includes/header_bank.html'); 
         				exit(); 
			&#125; else &#123;
				echo '<p><font color="red" size="+1">Unsucessful transaction</font></p>';
			&#125;
			           				             
         			
		

            		&#125; else &#123; // The account number does not match with token ID. 
         			echo '<p><font color="red" size="+1">The account number does not match with the reference ID in the database</font></p>'; 
      		&#125;
My syntax is correct. No error encountered. The problem is that ..
in my cart table it has 2 data but when I want to insert the cart table to view table it just appear 1 data... it does not insert all the data. Can you please help me in the sql queries to solve my problem? I really appreciated your help.

Posted: Fri Mar 04, 2005 8:58 am
by feyd
you only ask for 1 record. You need a loop.

Posted: Fri Mar 04, 2005 2:44 pm
by calcop
To get all the records from a mysql query use this example:

Code: Select all

$query = "select * from records where cust_id=$cust_id;";
$result = mysql_query( $query );
while ($rows = mysql_fetch_array( $result )) &#123;
   // Enter your code here for each record.
&#125;

Posted: Sun Mar 06, 2005 12:40 am
by elle_girl
I make a little modification.

Code: Select all

// Make sure the token available. 
      		$query = "SELECT * FROM customer_bank WHERE token_id = '$t' AND account_no = '$aa' "; 
      		$result = @mysql_query ($query); 
		
                  
      		if ($result) &#123; 
			
			$data = "SELECT itemId, price, qty FROM cart ";
			$result1 = @mysql_query($data) or die (mysql_error());
			
			 
			 while($row = mysql_fetch_row($result1)) &#123;
				
				$comp = "SELECT COUNT (*) FROM cart ";
				$comparison = @mysql_query($data) or die (mysql_error());
				$itemId = $row&#1111;'itemId']; // line 93
				$price = $row&#1111;'price']; // line 94
				$qty = $row&#1111;'qty']; // line 95
				$cartId = $row&#1111;'cartId']; // line 96
				
			
				if ($comparison == "1") &#123;
					
					$query1 = "INSERT INTO view (itemId, price, qty, cartId) VALUES ('$itemId','$price','$qty','$cartId' )"; 
            					$result2 = mysql_query($query1) or die(mysql_error());// Run the query. 
					if ($result2) &#123;
						$insert = "UPDATE view SET account_no = '$aa', token_id = '$t', name = '$n', address = '$ad', state = '$s', country = '$c' WHERE cartId = '$cartId'";
						$result_insert = mysql_query ($insert) or die(mysql_error());
						// Successful add the new customer. 
         						echo '<h3>Successful the transaction</h3>'; 
        						include ('includes/header_bank.html'); 
         						exit(); 
					&#125; else &#123;
						echo '<p><font color="red" size="+1">Cannot update to the database</font></p>';
	
					&#125;
				&#125; else if ($comparison == "2")&#123;
		
					$query2 = "INSERT INTO view (itemId, price, qty, cartId) VALUES ('$itemId','$price','$qty','$cartId' )"; 
            					$result3 = mysql_query($query2) or die(mysql_error());// Run the query. 
					if ($result3) &#123;
						$insert1 = "UPDATE view SET account_no = '$aa', token_id = '$t', name = '$n', address = '$ad', state = '$s', country = '$c' WHERE cartId = '$cartId'";
						$result_insert1 = mysql_query ($insert1) or die(mysql_error());
					&#125; else &#123;
						echo '<p><font color="red" size="+1">Cannot update to the database</font></p>';
	
					&#125;
					$query3 = "INSERT INTO view (itemId, price, qty, cartId) VALUES ('$itemId','$price','$qty','$cartId' )"; 
            					$result4 = mysql_query($query3) or die(mysql_error());// Run the query. 
					if ($result4) &#123;
						$insert2 = "UPDATE view SET account_no = '$aa', token_id = '$t', name = '$n', address = '$ad', state = '$s', country = '$c' WHERE cartId = '$cartId'";
						$result_insert2 = mysql_query ($insert2) or die(mysql_error());
						// Successful add the new customer. 
         						echo '<h3>Successful the transaction</h3>'; 
        						include ('includes/header_bank.html'); 
         						exit(); 
					&#125; else &#123;
						echo '<p><font color="red" size="+1">Cannot update to the database</font></p>';
It appear this error


Notice: Undefined index: itemId in C:\Program Files\Apache Group\Apache2\htdocs\change_checkout.php on line 93

Notice: Undefined index: price in C:\Program Files\Apache Group\Apache2\htdocs\change_checkout.php on line 94

Notice: Undefined index: qty in C:\Program Files\Apache Group\Apache2\htdocs\change_checkout.php on line 95

Notice: Undefined index: cartId in C:\Program Files\Apache Group\Apache2\htdocs\change_checkout.php on line 96




can you please help me to solve this problem? I really appreciated your help?

Posted: Sun Mar 06, 2005 6:54 am
by JAM
Try replacing...

Code: Select all

while($row = mysql_fetch_row($result1)) &#123;
with...

Code: Select all

while($row = mysql_fetch_assoc($result1)) &#123;
...or...

Code: Select all

while($row = mysql_fetch_array($result1)) &#123;

Posted: Sun Mar 06, 2005 6:35 pm
by elle_girl
i try to do with

Code: Select all

while($row = mysql_fetch_array($result1)) &#123;
It does not appear the error. But it appear the result


Cannot purchase more than 2 product for each transaction


Is this because the way I do the coding for the comparison is wrong?In the comparison I want to count how many row are there in the table so that I can insert all the data to the other table?Pleasehelp me? I really appreciate your help.