Page 1 of 1

no error displayed - help!

Posted: Thu Sep 09, 2004 10:48 pm
by C_Calav
hey guys, havent been on here for a while, been busy redoing sites in xhtml/css. now have finished all that time to put the php in! just upgraded a script (guess you can call it that?) trying to validate duplicate entry's for stockcode.

now if i put a duplicate error php send a error message. great!

but if the entry is valid.. nothing happens. doesnt get inserted to DB and nothing on screen.

take a look at code anything wrong?

thanx

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"]);
	
	 $sql1 = "select * from planes where P_Stock = '$P_Stock'"; 
     $result1 = mysql_query($sql1,$db) or die("Execution failed: ".mysql_error());
            
      if (!empty($P_Stock) && !empty($P_Name)) 
      { 
			  
			 while ($row=mysql_fetch_array($result1)) 
             { 
        	   if ($row["P_Stock"] == $_POST["P_Stock"]) 
			   { 
               print "<br><b>Duplicate Stockcode</b><br><br>"; 
               } 
			  
			   else 
               { 
			   $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.';
        
	  	       print "<br><b>new aircraft added</b><br><br>";
               } 
             }
      } 
	  else 
      { 
      echo "<br><b>Make sure stock code AND name are filled out</b><br><br>"; 
      } 


        }

?>

Posted: Fri Sep 10, 2004 12:31 am
by feyd
the code inside the while loop will not run if the $P_Stock passed didn't exist yet..

Posted: Fri Sep 10, 2004 12:38 am
by C_Calav
thanx feyd, can you please explain a little more i do not quite understand.

cheers

Posted: Fri Sep 10, 2004 12:42 am
by feyd
if $P_Stock isn't in the table you are selecting from, zero rows are returned. so the first call to mysql_fetch_* will return false, thus skipping the while loop altogether.

Posted: Fri Sep 10, 2004 12:48 am
by C_Calav
ok i understand what you are saying, thanx.

what i understand is (my code)

check is P_Stock is in table,

if it is: error

if it is not: insert


where is it that it is wrong?

thanx feyd

Posted: Fri Sep 10, 2004 12:51 am
by feyd
the basics could be:

if $P_Stock isn't empty, do the select. if zero rows are found, do the insert. Otherwise, say you got a duplicate.

Posted: Fri Sep 10, 2004 1:16 am
by C_Calav
thanx feyd, makes sense.

changed code to this

and still picks up the duplicates

but does not insert.

is this becuase im not saying if: zero rows were found etc?

if so,

can you help me with: if zero rows etc insert ..

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());
	  
			 while ($row=mysql_fetch_array($result1)) 
             { 
        	   if ($row["P_Stock"] == $_POST["P_Stock"]) 
			   { 
               echo "<br><b>Duplicate Stockcode</b><br><br>"; 
               } 
			  
			   else 
               { 
			   $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>Make sure stock code AND name are filled out</b><br><br>"; 
      } 


        }

?>

Posted: Fri Sep 10, 2004 1:39 am
by feyd
remove the whole while loop, moving the insert code out of it first.

check the number of rows found by the select. if it's zero, do the insert; if not, say it's a duplicate.

Posted: Fri Sep 10, 2004 1:51 am
by C_Calav
is this what you kinda mean feyd?

no errors but does not work properly, overwrites entrys if they are present.

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 ($result1 = 1) 
			   { 
               echo "<br><b>Duplicate Stockcode</b><br><br>"; 
               } 
			   else if ($result1 = 0)
               { 
			   $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>Make sure stock code AND name are filled out</b><br><br>"; 
      } 


        }
?>

Posted: Fri Sep 10, 2004 1:57 am
by feyd
== not =

and you might want to read what mysql_query returns for selects... (hint: it's not an integer)

Posted: Fri Sep 10, 2004 1:59 am
by C_Calav
cool thanks will read up, but apart from that is that how the code is ment to be?

your help much appricated!

Posted: Fri Sep 10, 2004 2:29 am
by feyd
basically... minor changes are needed as mentioned.