help me in sql queries

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
elle_girl
Forum Newbie
Posts: 23
Joined: Sun Feb 20, 2005 10:02 am

help me in sql queries

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you only ask for 1 record. You need a loop.
calcop
Forum Newbie
Posts: 20
Joined: Fri Mar 04, 2005 2:13 pm

Post 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;
elle_girl
Forum Newbie
Posts: 23
Joined: Sun Feb 20, 2005 10:02 am

Post 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?
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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;
elle_girl
Forum Newbie
Posts: 23
Joined: Sun Feb 20, 2005 10:02 am

Post 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.
Post Reply