[SOLVED] query help

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
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

[SOLVED] query help

Post by C_Calav »

hi guys,

i had this problem a while ago and Feyd told me to look up what a
query_string returns and i have done that but still not working.

i want it to validate for duplicate entrys, but its not, its just inserting them.

thanx guys.

Code: Select all

<?php
 
    $db = mysql_pconnect('**', '**', '**') or die ("Could not connect to database");

    # mysql_select_db('models') or die ("Could not select database!"); 

	# this is processed when the form is submitted
	# back on to this page (POST METHOD)
	if ($_SERVER['REQUEST_METHOD'] == "POST") 
        {
		# escape data and set variables
 		mysql_select_db('models') or die ("Could not select database!");

		$P_Stock = stripslashes($_POST["P_Stock"]);
		$P_Name = stripslashes($_POST["P_Name"]);
 		$P_Cat = stripslashes($_POST["P_Cat"]);
		$P_Scale = stripslashes($_POST["P_Scale"]);
		$P_Length = stripslashes($_POST["P_Length"]);	
		$P_Span = stripslashes($_POST["P_Span"]);
		$P_Price = stripslashes($_POST["P_Price"]);
		$P_Desc = stripslashes($_POST["P_Desc"]);
	
	
            
      if (!empty($P_Stock) && !empty($P_Name)) 
      { 
			 $sql1 = "select * from planes where P_Stock = '$P_Stock'"; 
    		 $result1 = mysql_query($sql1,$db) or die("Execution failed: ".mysql_error());
	  
			   if (!$sql1);
               { 
			   $sql  = "INSERT INTO planes (P_Stock, P_Name, P_Cat, P_Scale, P_Length, P_Span, P_Price, P_Desc) VALUES ('$P_Stock','$P_Name','$P_Cat','$P_Scale','$P_Length','$P_Span','$P_Price','$P_Desc')";
               $result = mysql_query($sql, $db) or die ("Execution failed."); 

	    	   move_uploaded_file($_FILES['file']['tmp_name'], '/var/users/modelair/modelaircraft.co.nz/htdocs/Pics/'.$_FILES['file']['name']); 
	      	   echo 'File has been stored in your uploads directory.';
        
	  	       echo "<br><b>new aircraft added</b><br><br>";
               }
			   else
			   { 
               echo "<br><b>Duplicate Stockcode</b><br><br>"; 
               } 
           
      } 
	  else 
      { 
      echo "<br><b>Make sure stock code AND name are filled out</b><br><br>"; 
      } 


        }

?>
JCMNetmedia
Forum Newbie
Posts: 7
Joined: Sat Sep 11, 2004 1:50 pm
Location: Wilsonville, Oregon
Contact:

Post by JCMNetmedia »

This may not solve your problem, but if you replace

Code: Select all

$_POST["P_Desc"]);

Code: Select all

$_REQUEST["P_Desc"]);
you can get both POST and GET variables from the browser. If you want to see what the browser is using try [php_man]print_r[/php_man] and see what you have.
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post by C_Calav »

hey thanx for your reply, REQUEST did not chnage anything, i will try the print_r thing and see also.

thanx again
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

if (!$sql1);
to

Code: Select all

if(!mysql_num_rows($sql1))
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post by C_Calav »

thanx feyd,

i now have this error:

Parse error: parse error, unexpected T_ELSE in /var/users/modelair/modelaircraft.co.nz/htdocs/admin/insert.php on line 192

this is the line with the 'else' on it after the line i just changed, what does this mean?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

did you remove the semicolon from the if line?
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post by C_Calav »

nope! ill do that, i didnt know it had to be removed!
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post by C_Calav »

hi feyd, its now giving me this error:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /var/users/modelair/modelaircraft.co.nz/htdocs/admin/insert.php on line 182

line 182 is: if (!mysql_num_rows($sql1))

Code: Select all

<?php
        
      if (!empty($P_Stock) && !empty($P_Name)) 
      { 
			 $sql1 = "select * from planes where P_Stock = '$P_Stock'"; 
    		 $result1 = mysql_query($sql1,$db) or die("Execution failed: ".mysql_error());
	  
	           if (!mysql_num_rows($sql1))
               { 
			   $sql  = "INSERT INTO planes (P_Stock, P_Name, P_Cat, P_Scale, P_Length, P_Span, P_Price, P_Desc) VALUES ('$P_Stock','$P_Name','$P_Cat','$P_Scale','$P_Length','$P_Span','$P_Price','$P_Desc')";
               $result = mysql_query($sql, $db) or die ("Execution failed."); 

	    	   move_uploaded_file($_FILES['file']['tmp_name'], '/var/users/modelair/modelaircraft.co.nz/htdocs/Pics/'.$_FILES['file']['name']); 
	      	   echo 'File has been stored in your uploads directory.';
        
	  	       echo "<br><b>new aircraft added</b><br><br>";
               }
			   else
			   { 
               echo "<br><b>Duplicate Stockcode</b><br><br>"; 
               } 
           
      } 
	  else 
      { 
      echo "<br><b>Make sure stock code AND name are filled out</b><br><br>"; 
      } 

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

simple change

Post by phpScott »

you almost got it
change#

Code: Select all

<?php
if (!mysql_num_rows($sql1))

?>
to

Code: Select all

<?php
if (!mysql_num_rows($result1))

?>
you are trying to get how many rows your query string is giving and not how many results are being returned.
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post by C_Calav »

thanks very much phpScott! problem sovled! thanx also feyd for your help!

cheers guys
Post Reply