Jump to content

Data Search error?


Onedgill

Recommended Posts

I created an Intranet and I have a telephone directory where you can search for an employee's contact info. When I do a seach to display all the data in the table, it works but when I search by a specific name the php code returns no data from the table. Not sure why: This is the form code on the html page where the user types the data to search for: <form action= "directory02.php" method="post">NAME: <input type="text" name="find"/><input type="submit" value="Search"/></form> This is the php code on the directory02.php page: <?php$con = mysql_connect("localhost","intranet","sysmgr");if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("distrdirectory", $con);$find=$_POST['find'];$result = mysql_query("SELECT * FROM listingWHERE Name LIKE '$find'"); echo "<table width='800' cellpadding='0' cellspacing='0'><tr height= '20' bgcolor='#CCCCCC'><th align ='left'>Extension</th><th align ='left'>Name</th><th align ='left'>Position</th><th align ='left'>Department</th></tr>"; while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['Extension'] . "</td>"; echo "<td>" . $row['Name'] . "</td>"; echo "<td>" . $row['Position'] . "</td>"; echo "<td>" . $row['Department'] . "</td>"; echo "</tr>"; }echo "</table>"; mysql_close($con);?> directory.htmldirectory02.php

Style.css

Link to comment
Share on other sites

are you familiar with phpMyAdmin? can your print out the query to check that it looks right?

echo"SELECT * FROM listing WHERE Name LIKE '$find'");

you can try it directly in phpMyAdmin and then play around with it too. Also, you may need to use wildcards. have you reviewed this page in the tutorials at all?http://www.w3schools.com/sql/sql_like.asp

Edited by thescientist
  • Like 1
Link to comment
Share on other sites

If you want to find any name that contains what they typed then you need to include the "%" wildcard character in the search string: $result = mysql_query("SELECT * FROM listing WHERE Name LIKE '%$find%'"); Otherwise, it will only returns results that match exactly.

  • Like 1
Link to comment
Share on other sites

  • 2 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...