Problems with validating data for a search

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
Rovas
Forum Contributor
Posts: 272
Joined: Mon Aug 21, 2006 7:09 am
Location: Romania

Problems with validating data for a search

Post by Rovas »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I want to build a search page for the website I' m working on and I having some trouble with the validation. 
  Here is the scenario for this page: 
  1. user enters the page 
  2. completes or doesn't the search field 
  3 a. in case of errors (the field is empty or it is too short) error messages appear
     b. if the word he search for doesn' t exist in t he database an error message appears
  4. the search results appear 
  
   But it doesn' t work at all: no error messages, no results.
   Here is the code:

Code: Select all

<?php
   //inclusion of the other php functions i need
   //declaration of the variables  $term= the search term, $valid= the indicator that the validation is finished 

   $term='';
   $valid=false;
   //conection to the database
  //first search function in the MySQL 
   function select_cat($p='')
   {
       $int=''";
        //array in which will be put the result of the query
       $q='';
       $rez='';
        //the query
       $int="SELECT...'";
       $q=mysql_query($int) or die(mysql_error());
       echo "<tr><td>\t<h2> Results</h2></td></tr>\r\n";
       while($rez=mysql_fetch_array($q))
       {
           if (count($q)==0) { return false;}
           else
           {
               return true;
               return true;
               echo "\t\t <tr>\r";
               echo "\t\t\t <td colspan=3>" ."<a href='url?subcat=" .$r2['Sc'] ."'>" .$r2['NumeSub'] ."</a>" ."</td>\r";
               echo "\t\t</tr>\r";

           }
       }
   }
 
?>
  <form method='POST' action=' <?php echo $_SERVER['PHP_SELF'] ?> '>
   <table>
   <tr>
       <td>Introduceti termenul pe care- l cautati.<td>
       <td><input type='text' name='termen'></td>
       <td><input type='submit' value='Cauta'></td>
   </tr>
 
<?php
     $term=$_POST["termen"];
  /* if(isset($_POST["submit"]) && $valid==false)
   {

     if(trim($termen)=='') {echo "Message 1"; $valid=false; }
     else if(strlen(trim($termen))<3){echo "Message 2"; $valid=false;}
     else {$valid=true;}
   }
   
   if ($valid==true)
   {
       $term=addslashes($_POST['termen']);
       if ((select_pr$term)==false)&&(select_cat($term)==false))
       {
         echo "<h2>Ne pare rau dar nu avem termenul cautat in baza de date</h2>";
       }
   }*/
 
  </table>
  </form>
   Sf();
?>
What' s wrong with it?


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Apparently you've editted the code before posting it, making it difficult to see potential errors as there is definitely a parse error in the code as-is.
GM
Forum Contributor
Posts: 365
Joined: Wed Apr 26, 2006 4:19 am
Location: Italy

Post by GM »

EDIT: Other than the open quote causing the parse error...

I've noticed a few things that may be the cause of the problem:

1)

Code: Select all

echo "\t\t\t <td colspan=3>" ."<a href='url?subcat=" .$r2['Sc'] ."'>" .$r2['NumeSub'] ."</a>" ."</td>\r"; 

//$r2 is not set - you've used $rez to store your results

//should this be:

echo "\t\t\t <td colspan=3>" ."<a href='url?subcat=" .$rez['Sc'] ."'>" .$rez['NumeSub'] ."</a>" ."</td>\r";

//???
2)

Code: Select all

<form method='POST' action=' <?php echo $_SERVER['PHP_SELF'] ?> '>

//you have whitespace inside the quotes in the action part of the form,

//should this be:
<form method='POST' action='<?php echo $_SERVER['PHP_SELF'] ?>'>

//???
3) You have an entire block of code commented out... it looks like it's the part that controls what gets displayed...

Hope one of these resolves the problem!

EDIT 2: Another potential mistake.

Code: Select all

if ((select_pr$term)==false)&&(select_cat($term)==false)) 

//this doesn't look correct. Should it be:

if ((select_pr($term)==false)&&(select_cat($term)==false)) 

//???
Rovas
Forum Contributor
Posts: 272
Joined: Mon Aug 21, 2006 7:09 am
Location: Romania

Post by Rovas »

I forgot to take out the comments because I was testing the search functions before and during posting the topic. I' ll take note of your corrections and announce what I manage to do.
But now something came up (nothing to do with PHP or MySQL) and I have to fix it.
Rovas
Forum Contributor
Posts: 272
Joined: Mon Aug 21, 2006 7:09 am
Location: Romania

Post by Rovas »

I solved the validation problem (the names where different of the HTML control and the POST variable).
But now I have a different problem I doesn' t show the result of the search (it identifies if the term isn' t in the database).
I tried to fix it by adding two functions one that verifies that there is a similar term in the database and one who displays the results but this following error appears:
[

Code: Select all

Parse error: parse error, unexpected $end in c:\www\cautare_2.php on line 113
All the code has 100 lines and all the paranthesis are closed and so are the PHP tags.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Rovas wrote:All the code has 100 lines and all the paranthesis are closed and so are the PHP tags.
did you check braces and brackets in that too? How about heredocs?
Rovas
Forum Contributor
Posts: 272
Joined: Mon Aug 21, 2006 7:09 am
Location: Romania

Post by Rovas »

Yes I rechecked them find one that got a way, added it' s sister and the result:
it shows twice the result of a search and it doesn' t display any search that were made with the second one :? :cry:
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Rovas wrote:it shows twice the result of a search and it doesn' t display any search that were made with the second one :? :cry:
I'm not sure I fully understand what you've said.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Cleared and unlocked
Rovas
Forum Contributor
Posts: 272
Joined: Mon Aug 21, 2006 7:09 am
Location: Romania

Post by Rovas »

The result is shown like this

Code: Select all

Products
    Brand X Product X1   
    Brand Y Product Y1 
    Brand Z Product Z1
The products shown correspond to the term introduced.
    
  Products
    Brand X Product X1   
    Brand Y Product Y1 
    Brand Z Product Z1
The products shown correspond to the term introduced.

I don' t understand why. The function sel_p which shows the result it' s called only once and the MySQL code that corresponds to the query also shows a result only once.

Code: Select all

... validation code for the term introduced which works fine same as the search function sel_p...

      if ($valid==true)
      {
        $term=addslashes($_POST['term']);
        if ((sel_p($term)==false))
        {
         echo "<h2>We' re sorry the termen introduces doesn' t appear in our database.</h2>";
        }
        else if(sel_p($term)==true){echo "<tr><td><h3>The products shown correspond to the term introduced.</h3> </td></tr>";}
        
      }
Post Reply