Page 1 of 1

[SOLVED] query help

Posted: Mon Sep 13, 2004 10:01 pm
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>"; 
      } 


        }

?>

Posted: Mon Sep 13, 2004 10:30 pm
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.

Posted: Mon Sep 13, 2004 10:36 pm
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

Posted: Mon Sep 13, 2004 11:18 pm
by feyd

Code: Select all

if (!$sql1);
to

Code: Select all

if(!mysql_num_rows($sql1))

Posted: Mon Sep 13, 2004 11:28 pm
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?

Posted: Mon Sep 13, 2004 11:43 pm
by feyd
did you remove the semicolon from the if line?

Posted: Mon Sep 13, 2004 11:49 pm
by C_Calav
nope! ill do that, i didnt know it had to be removed!

Posted: Tue Sep 14, 2004 2:16 am
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>"; 
      } 

?>

simple change

Posted: Tue Sep 14, 2004 3:15 am
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.

Posted: Tue Sep 14, 2004 3:28 am
by C_Calav
thanks very much phpScott! problem sovled! thanx also feyd for your help!

cheers guys