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!
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:
<?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();
?>
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]
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.
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";
//???
<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...
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))
//???
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.
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:
[
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
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.
... 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>";}
}