Jump to content

[SOLVED] Search MySql help please?


TheStalker

Recommended Posts

Hi ive been following a book to try and create a search page that searchs my database table and displays the results in a table. Im having trouble with making it work i think there maybe a problem with the form im trying to use? all i get is a page in IE showing this with the url http://localhost/test4/%3C?=$PHP_SELF?> would some one be able to point to what might be wrongin my code please?

 

  The website declined to show this webpage

HTTP 403 

   Most likely causes:

This website requires you to log in.

 

   What you can try:

     Go back to the previous page.

 

     More information

 

 

here is the code im using

 

 

<html>


<h2>Search</h2> 
<form name="search" method="post" action="<?=$PHP_SELF?>">
Seach for: <input type="text" name="find" /> 
<Select NAME="field">
<Option VALUE="fname">surname</option>
<Option VALUE="lname">forname</option>
<Option VALUE="info">postcode</option>
</Select>

<input type="hidden" name="searching" value="yes" />
<input type="submit" name="search" value="Search" />
</form>

 

 

<? php
//if they have submitted the form 
if ($searching =="yes") 
{ 
echo "<h2>Results</h2><p>"; 

//did not enter a search term we give  an error 
if ($find == "") 
{ 
echo "<p>You forgot to enter a search term"; 
exit; 
} 

// connect to our Database 
mysql_connect("localhost", "root", "") or die(mysql_error()); 
mysql_select_db("randv") or die(mysql_error()); 

// preform filtering 
$find = strtoupper($find); 
$find = strip_tags($find); 
$find = trim ($find); 

//Now we search the table and filed 
$data = mysql_query("SELECT * FROM customers WHERE upper($field) LIKE'%$find%'"); 


//display the results 
echo "<table width=90% align=center border=1><tr>";
while($result = mysql_fetch_array( $data )) 
{ 
$surname = $row["surname"]; 
$forename = $row["forename"]; 
$addressLine1 = $row["addressLine1"]; 
$addressLine2 = $row["addressLine2"]; 
$postcode = $row["postcode"]; 
echo "<tr> 
<td>$surname</td> 
<td>$forname</td> 
<td>$addressLine1</td> 
<td>$addressLine2</td>
<td>$postcode</td></tr> 
<tr></td> 
</tr></table>"; 
} 

//This counts the number or results 
$anymatches=mysql_num_rows($data); 
if ($anymatches == 0) 
{ 
echo "Sorry, but we can not find an entry to match the search<br><br>"; 
} 

//what was searched for
echo "<b>Searched For:</b> " .$find; 
} 
?> 

</html>

 

 

 

any ideas would be great thanks.

 

Link to comment
Share on other sites

<html>


<h2>Search</h2> 
<form name="search" method="post" action="<?php=$PHP_SELF?>">
Seach for: <input type="text" name="find" /> 
<Select NAME="field">
<Option VALUE="fname">surname</option>
<Option VALUE="lname">forname</option>
<Option VALUE="info">postcode</option>
</Select>

<input type="hidden" name="searching" value="yes" />
<input type="submit" name="search" value="Search" />
</form>


<?php 
//if they have submitted the form 
if ($_POST['searching'] == "yes")
{ 
echo "<h2>Results</h2><p>"; 

//did not enter a search term we give  an error 
if ($find == "") 
{ 
echo "<p>You forgot to enter a search term"; 
exit; 
} 

// connect to our Database 
mysql_connect("localhost", "root", "") or die(mysql_error()); 
mysql_select_db("randv") or die(mysql_error()); 

// preform filtering 
$find = strtoupper($find); 
$find = strip_tags($find); 
$find = trim ($find); 

//Now we search the table and filed 
$data = mysql_query("SELECT * FROM customers WHERE upper($field) LIKE'%$find%'"); 


//display the results 
echo "<table width=90% align=center border=1><tr>";
while($result = mysql_fetch_array( $data )) 
{ 
$surname = $row["surname"]; 
$forename = $row["forename"]; 
$addressLine1 = $row["addressLine1"]; 
$addressLine2 = $row["addressLine2"]; 
$postcode = $row["postcode"]; 
echo "<tr> 
<td>$surname</td> 
<td>$forname</td> 
<td>$addressLine1</td> 
<td>$addressLine2</td>
<td>$postcode</td></tr> 
<tr></td> 
</tr></table>"; 
} 

//This counts the number or results 
$anymatches=mysql_num_rows($data); 
if ($anymatches == 0) 
{ 
echo "Sorry, but we can not find an entry to match the search<br><br>"; 
} 

//what was searched for
echo "<b>Searched For:</b> " .$find; 
} 
?> 

</html>

Link to comment
Share on other sites

hi,

 

thanks for the quick reply. Ive changed that but im still having the same problem :(

 

 

search2.php

<html>


<h2>Search</h2> 
<form name="search" method="post" action="<?php=$PHP_SELF?>">
Seach for: <input type="text" name="find" /> 
<Select NAME="field">
<Option VALUE="fname">surname</option>
<Option VALUE="lname">forname</option>
<Option VALUE="info">postcode</option>
</Select>

<input type="hidden" name="searching" value="yes" />
<input type="submit" name="search" value="Search" />
</form>


<?php 
//if they have submitted the form 
if ($_POST['searching'] == "yes")
{ 
echo "<h2>Results</h2><p>"; 

//did not enter a search term we give  an error 
if ($find == "") 
{ 
echo "<p>You forgot to enter a search term"; 
exit; 
} 

// connect to our Database 
mysql_connect("localhost", "root", "") or die(mysql_error()); 
mysql_select_db("randv") or die(mysql_error()); 

// preform filtering 
$find = strtoupper($find); 
$find = strip_tags($find); 
$find = trim ($find); 

//Now we search the table and filed 
$data = mysql_query("SELECT * FROM customers WHERE upper($field) LIKE'%$find%'"); 


//display the results 
echo "<table width=90% align=center border=1><tr>";
while($result = mysql_fetch_array( $data )) 
{ 
$surname = $row["surname"]; 
$forename = $row["forename"]; 
$addressLine1 = $row["addressLine1"]; 
$addressLine2 = $row["addressLine2"]; 
$postcode = $row["postcode"]; 
echo "<tr> 
<td>$surname</td> 
<td>$forname</td> 
<td>$addressLine1</td> 
<td>$addressLine2</td>
<td>$postcode</td></tr> 
<tr></td> 
</tr></table>"; 
} 

//This counts the number or results 
$anymatches=mysql_num_rows($data); 
if ($anymatches == 0) 
{ 
echo "Sorry, but we can not find an entry to match the search<br><br>"; 
} 

//what was searched for
echo "<b>Searched For:</b> " .$find; 
} 
?> 

</html>

Link to comment
Share on other sites

 

ive just tried changing this

 

search2.php

<form name="search" method="post" action="<?php=$PHP_SELF?>">

 

to this

<form name="search" method="post" action="<?php echo $_SERVER['PHP_SELF']?>">

 

  but i get this "Results

 

You forgot to enter a search term"

 

Link to comment
Share on other sites

my suggestion would be just to change action=<? $_SERVER['PHP_SELF'] ?>

to

action='fileName.php'

 

dosnt seem to make any diffrence?

 

im thinking it must be some thing within this bit of code but cant work out what

 

<html>


<h2>Search</h2> 
<form name="search" method="post" action="<?php $_SERVER['PHP_SELF']?>">

Seach for: <input type="text" name="find" /> 
<Select NAME="field">
<Option VALUE="surname">surname</option>
<Option VALUE="forename">forename</option>
<Option VALUE="postcode">postcode</option>
</Select>

<input type="hidden" name="searching" value="yes" />
<input type="submit" name="search" value="Search" />
</form>


<?php 
//if they have submitted the form 
if ($_POST['searching'] == "yes")
{ 
echo "<h2>Results</h2><p>"; 

//did not enter a search term we give  an error 
if ($find == "") 
{ 
echo "<p>You forgot to enter a search term"; 
exit; 
} 

// connect to our Database 
mysql_connect("localhost", "root", "") or die(mysql_error()); 
mysql_select_db("randv") or die(mysql_error()); 

// preform filtering 
$find = strtoupper($find); 
$find = strip_tags($find); 
$find = trim ($find); 

//Now we search the table and filed 
$data = mysql_query("SELECT * FROM customers WHERE upper($field) LIKE'%$find%'"); 

Link to comment
Share on other sites

<?php
//did not enter a search term we give  an error 
if ($find == "") 
{ 
echo "<p>You forgot to enter a search term"; 
exit; 
} 
?>

should be

<?php
//did not enter a search term we give  an error 
if ($_POST['find'] == "") 
{ 
echo "<p>You forgot to enter a search term"; 
exit; 
} 
?>

Anytime you pass info from a form you need to access it either via $_POST or $_GET depending on the method you use on the form.

Link to comment
Share on other sites

ok well ive changed a fair few things but am still can not get the results to display in the table? i get this message Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\wamp1\www\test4\search2.php on line 54

 

 

 




<html>
<h2>Search</h2> 
<form name="search" method="post" action="<?php $_SERVER['PHP_SELF']?>">

Seach for: <input type="text" name="find" /> 
<Select NAME="field">
<Option VALUE="Surname">Surname</option>
<Option VALUE="Forename">Forename</option>
<Option VALUE="Postcode">Postcode</option>
</Select>

<input type="hidden" name="searching" value="yes" />
<input type="submit" name="search" value="Search" />
</form>

 

<?php 
//if they have submitted the form 
if ($_POST['searching'] == "yes")
{ 
echo "<h2>Results</h2><p>"; 

//did not enter a search term we give  an error 
if ($_POST['find'] == "") 
{ 
echo "<p>You forgot to enter a search term"; 
exit; 
} 

// connect to our Database 
mysql_connect("localhost", "root", "") or die(mysql_error()); 
mysql_select_db("randv") or die(mysql_error()); 

// preform filtering 
$find = strtoupper($find); 
$find = strip_tags($find); 
$find = trim ($find); 

 

// search the table and filed 
$data = mysql_query("SELECT * FROM customers WHERE upper($field) LIKE'%$find%'"); 

echo "<table border='1' width = '1200' align='center'>
<br>
<tr>
<th>CustomerID</th>
<th>Surname</th>
<th>Forename</th>
<th>AddressLine1</th>
<th>AddressLine2</th>
<th>Postcode</th>
</tr>";

//And we display the results 
while($row = mysql_fetch_array($data)) 

{ 
 echo "<tr>";
 echo "<td><center>" . $row['CustomerID'] . "</td>";
 echo "<td><center>" . $row['Surname'] . "</td>";
 echo "<td><center>" . $row['Forename'] . "</td>";
 echo "<td><center>" . $row['AddressLine1'] . "</td>";
 echo "<td><center>" . $row['AddressLine2'] . "</td>";
 echo "<td><center>" . $row['Postcode'] . "</td>";
 echo "</tr>";
} 



//what was searched for
echo "<b>Searched For:</b> " .$find; 
} 
?> 

</html>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.