Page 1 of 2
PHP Search Form
Posted: Thu Feb 19, 2009 9:31 am
by fionaom87
Hey i am trying to do a php search form and this error is appearing
Notice: Undefined variable: searching in C:\wamp\www\search.php on line 18
Also it does not seem to realise action="<?=$PHP_SELF?>"> as it does not seem to be coming back to the page again.
Thanks
F
Code: Select all
<h2>Search</h2>
<form name="search" method="post" action="<?=$PHP_SELF?>">
Seach for: <input type="text" name="find" /> in
<Select NAME="field">
<Option VALUE="fname">First Name</option>
<Option VALUE="lname">Last Name</option>
<Option VALUE="info">Profile</option>
</Select>
<input type="hidden" name="searching" value="yes" />
<input type="submit" name="search" value="Search" />
</form>
<?php include 'config.php'; ?><!-- DB connection -->
<?php
//This is only displayed if they have submitted the form
if ($searching =="yes") //line 18
{
echo "<h2>Results</h2><p>";
//If they did not enter a search term we give them an error
if ($find == "")
{
echo "<p>You forgot to enter a search term";
exit;
}
$find = strtoupper($find);
$find = strip_tags($find);
$find = trim ($find);
$data = mysql_query("SELECT * FROM customers WHERE upper($field) LIKE'%$find%'");
while($result = mysql_fetch_array( $data ))
{
echo $result['name'];
echo " ";
echo $result['address'];
echo "<br>";
echo $result['telephone'];
echo "<br>";
echo "<br>";
}
$anymatches=mysql_num_rows($data);
if ($anymatches == 0)
{
echo "Sorry, but we can not find an entry to match your query<br><br>";
}
//And we remind them what they searched for
echo "<b>Searched For:</b> " .$find;
}
?>
Re: PHP Search Form
Posted: Thu Feb 19, 2009 10:03 am
by mattpointblank
You have to use $_POST['name_of_form_field'] to access form variables.
Re: PHP Search Form
Posted: Thu Feb 19, 2009 10:11 am
by fionaom87
This is my code now but still doesnt seem to work
Code: Select all
<html>
<body>
<h2>Search</h2>
<form name="search" method="post" action="<?PHP=$PHP_SELF?>">
Seach for: <input type="text" name="find" /> in
<Select NAME="field">
<Option VALUE="name">First Name</option>
<Option VALUE="address">Address</option>
<Option VALUE="telephoneno">Profile</option>
</Select>
<input type="hidden" name="searching" value="yes" />
<input type="submit" name="search" value="Search" />
</form>
<?php error_reporting (E_ALL ^ E_NOTICE); ?>
<?php include 'config.php'; ?><!-- DB connection -->
<?php
//This is only displayed if they have submitted the form
$name=$_POST['name'];
$address=$_POST['address'];
$telephoneno=$_POST['telephoneno'];
if ($searching =="yes")
{
echo "<h2>Results</h2><p>";
//If they did not enter a search term we give them an error
if ($find == "")
{
echo "<p>You forgot to enter a search term";
exit;
}
$find = strtoupper($find);
$find = strip_tags($find);
$find = trim ($find);
$data = mysql_query("SELECT * FROM customers WHERE upper($field) LIKE'%$find%'");
while($result = mysql_fetch_array( $data ))
{
echo $result['name'];
echo " ";
echo $result['address'];
echo "<br>";
echo $result['telephoneno'];
echo "<br>";
echo "<br>";
}
$anymatches=mysql_num_rows($data);
if ($anymatches == 0)
{
echo "Sorry, but we can not find an entry to match your query<br><br>";
}
//And we remind them what they searched for
echo "<b>Searched For:</b> " .$find;
}
?>
</body>
</html>
Re: PHP Search Form
Posted: Thu Feb 19, 2009 10:45 am
by mattpointblank
I mean for this line:
if ($searching =="yes")
You're not defining $searching, so you need to do:
$searching = $_POST['searching'];
before.
Conventionally though, rather than testing for a hidden form field, you should test to see if the submit button was pressed, eg:
Code: Select all
if(isset($_POST['search'])) {
// do search stuff here
}
This uses the isset() function, which tests if a variable exists. In this case it's testing that the $_POST array (which contains anything posted to the script from a form) has an element called 'search' (the name of your submit button). If it's been set, it'll do stuff. Make sense?
Re: PHP Search Form
Posted: Thu Feb 19, 2009 10:53 am
by fionaom87
sorry im slightly confused.
i put in that search variable but when i click it just comes up webpage cannot be found.
Code: Select all
<html>
<body>
<h2>Search</h2>
<form name="search" method="post" action="<?PHP=$PHP_SELF?>">
Seach for: <input type="text" name="find" /> in
<Select NAME="field">
<Option VALUE="name">First Name</option>
<Option VALUE="address">Address</option>
<Option VALUE="telephoneno">Profile</option>
</Select>
<input type="hidden" name="searching" value="yes" />
<input type="submit" name="search" value="Search" />
</form>
<?php error_reporting (E_ALL ^ E_NOTICE); ?>
<?php include 'config.php'; ?><!-- DB connection -->
<?php
//This is only displayed if they have submitted the form
if(isset($_POST['search']))
{
echo "<h2>Results</h2><p>";
//If they did not enter a search term we give them an error
if ($find == "")
{
echo "<p>You forgot to enter a search term";
exit;
}
$find = strtoupper($find);
$find = strip_tags($find);
$find = trim ($find);
$data = mysql_query("SELECT * FROM customers WHERE upper($field) LIKE'%$find%'");
while($result = mysql_fetch_array( $data ))
{
echo $result['name'];
echo " ";
echo $result['address'];
echo "<br>";
echo $result['telephoneno'];
echo "<br>";
echo "<br>";
}
$anymatches=mysql_num_rows($data);
if ($anymatches == 0)
{
echo "Sorry, but we can not find an entry to match your query<br><br>";
}
//And we remind them what they searched for
echo "<b>Searched For:</b> " .$find;
}
?>
</body>
</html>
Re: PHP Search Form
Posted: Thu Feb 19, 2009 11:17 am
by mattpointblank
Change
action="<?PHP=$PHP_SELF?>">
to
action="<?php echo $_SERVER['PHP_SELF']; ?>">
you have to actually output/echo these values to make them display.
Re: PHP Search Form
Posted: Thu Feb 19, 2009 11:28 am
by fionaom87
i am now just getting
"You forgot to enter a search term"
even though i have entered a search term.
upper($field)--- must i change this to a field in my database?
Thanks
F
Code: Select all
<html>
<body>
<h2>Search</h2>
<form name="search" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Seach for: <input type="text" name="find" /> in
<Select NAME="field">
<Option VALUE="name">First Name</option>
<Option VALUE="address">Address</option>
<Option VALUE="telephoneno">Profile</option>
</Select>
<input type="hidden" name="searching" value="yes" />
<input type="submit" name="search" value="Search" />
</form>
<?php error_reporting (E_ALL ^ E_NOTICE); ?>
<?php include 'config.php'; ?><!-- DB connection -->
<?php
//This is only displayed if they have submitted the form
if(isset($_POST['search']))
{
echo "<h2>Results</h2><p>";
//If they did not enter a search term we give them an error
if ($find == "")
{
echo "<p>You forgot to enter a search term";
exit;
}
$find = strtoupper($find);
$find = strip_tags($find);
$find = trim ($find);
$data = mysql_query("SELECT * FROM customers WHERE upper($field) LIKE'%$find%'");
while($result = mysql_fetch_array( $data ))
{
echo $result['name'];
echo " ";
echo $result['address'];
echo "<br>";
echo $result['telephoneno'];
echo "<br>";
echo "<br>";
}
$anymatches=mysql_num_rows($data);
if ($anymatches == 0)
{
echo "Sorry, but we can not find an entry to match your query<br><br>";
}
//And we remind them what they searched for
echo "<b>Searched For:</b> " .$find;
}
?>
</body>
</html>
Re: PHP Search Form
Posted: Thu Feb 19, 2009 11:40 am
by p3rk5
You did not define the $find variable before checking if it existed:
Code: Select all
<html>
<body>
<h2>Search</h2>
<form name="search" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Seach for: <input type="text" name="find" /> in
<Select NAME="field">
<Option VALUE="name">First Name</option>
<Option VALUE="address">Address</option>
<Option VALUE="telephoneno">Profile</option>
</Select>
<input type="hidden" name="searching" value="yes" />
<input type="submit" name="search" value="Search" />
</form>
<?php error_reporting (E_ALL ^ E_NOTICE); ?>
<?php include 'config.php'; ?><!-- DB connection -->
<?php
$find = strtoupper(strip_tags(trim($find)));
if(isset($_POST['search']))
{
echo "<h2>Results</h2><p>";
//If they did not enter a search term we give them an error
if ($find == "")
{
echo "<p>You forgot to enter a search term";
exit;
}
$data = mysql_query("SELECT * FROM customers WHERE upper($field) LIKE'%$find%'");
while($result = mysql_fetch_array( $data ))
{
echo $result['name'];
echo " ";
echo $result['address'];
echo "<br>";
echo $result['telephoneno'];
echo "<br>";
echo "<br>";
}
$anymatches=mysql_num_rows($data);
if ($anymatches == 0)
{
echo "Sorry, but we can not find an entry to match your query<br><br>";
}
//And we remind them what they searched for
echo "<b>Searched For:</b> " .$find;
}
?>
</body>
</html>
Re: PHP Search Form
Posted: Thu Feb 19, 2009 12:38 pm
by watson516
$_POST['find']
I might be mistaken but you have to either specify $_POST, $_GET, or $_REQUEST to access the form field variables. Accessing them using just $find I believe has to be set up on the server and is probably discouraged by most people.
Re: PHP Search Form
Posted: Sat Feb 21, 2009 6:15 am
by fionaom87
hey still doesnt seem to be working for me. When i enter a search term example Fiona for FirstName.... its just comes up you have You forgot to enter a search term
any help would be great
F.
Code: Select all
<html>
<body>
<h2>Search</h2>
<form name="search" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Seach for: <input type="text" name="find" /> in
<Select NAME="field">
<Option VALUE="name">First Name</option>
<Option VALUE="address">Address</option>
<Option VALUE="telephoneno">Profile</option>
</Select>
<input type="hidden" name="searching" value="yes" />
<input type="submit" name="search" value="Search" />
</form>
<?php error_reporting (E_ALL ^ E_NOTICE); ?>
<?php include 'config.php'; ?><!-- DB connection -->
<?php
$find = strtoupper(strip_tags(trim($find)));
if(isset($_POST['search']))
{
echo "<h2>Results</h2><p>";
//If they did not enter a search term we give them an error
if ($find == "")
{
echo "<p>You forgot to enter a search term";
exit;
}
$data = mysql_query("SELECT * FROM customers WHERE upper($field) LIKE'%$find%'");
while($result = mysql_fetch_array( $data ))
{
echo $result['name'];
echo " ";
echo $result['address'];
echo "<br>";
echo $result['telephoneno'];
echo "<br>";
echo "<br>";
}
$anymatches=mysql_num_rows($data);
if ($anymatches == 0)
{
echo "Sorry, but we can not find an entry to match your query<br><br>";
}
//And we remind them what they searched for
echo "<b>Searched For:</b> " .$find;
}
?>
</body>
</html>
Re: PHP Search Form
Posted: Sat Feb 21, 2009 6:33 am
by susrisha
Code: Select all
if(isset($_POST['search']))
{
$find= $_POST['find'];
//then your code please
Re: PHP Search Form
Posted: Sat Feb 21, 2009 6:52 am
by fionaom87
following errors occured
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\wamp\www\search.php on line 36
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\wamp\www\search.php on line 47
Code: Select all
<html>
<body>
<h2>Search</h2>
<form name="search" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Seach for: <input type="text" name="find" /> in
<Select NAME="field">
<Option VALUE="name">First Name</option>
<Option VALUE="address">Address</option>
<Option VALUE="telephoneno">Profile</option>
</Select>
<input type="hidden" name="searching" value="yes" />
<input type="submit" name="search" value="Search" />
</form>
<?php error_reporting (E_ALL ^ E_NOTICE); ?>
<?php include 'config.php'; ?><!-- DB connection -->
<?php
$find = strtoupper(strip_tags(trim($find)));
if(isset($_POST['search']))
{
$find= $_POST['find'];
echo "<h2>Results</h2><p>";
//If they did not enter a search term we give them an error
if ($find == "")
{
echo "<p>You forgot to enter a search term";
exit;
}
$data = mysql_query("SELECT * FROM customers WHERE upper($field) LIKE'%$find%'");
while($result = mysql_fetch_array( $data )) //line 36
{
echo $result['name'];
echo " ";
echo $result['address'];
echo "<br>";
echo $result['telephoneno'];
echo "<br>";
echo "<br>";
}
$anymatches=mysql_num_rows($data);
if ($anymatches == 0) //line 47
{
echo "Sorry, but we can not find an entry to match your query<br><br>";
}
//And we remind them what they searched for
echo "<b>Searched For:</b> " .$find;
}
?>
</body>
</html>
Re: PHP Search Form
Posted: Sat Feb 21, 2009 3:22 pm
by fionaom87
Re: PHP Search Form
Posted: Sat Feb 21, 2009 4:11 pm
by jayshields
Change this:
Code: Select all
$data = mysql_query("SELECT * FROM customers WHERE upper($field) LIKE'%$find%'");
to:
Code: Select all
$data = mysql_query("SELECT * FROM customers WHERE upper($field) LIKE'%$find%'") or die(mysql_error());
This is simple MySQL debugging, and has been discussed many times before.
Re: PHP Search Form
Posted: Sat Feb 21, 2009 6:25 pm
by Citizen
var_dump(get_defined_vars()); and mysql_query($sql) or die(mysql_error() . $sql) are your bread and butter for learning the ropes. If you're unsure of the content of your queries or the result of our out put, use them.