Searchable MySQL table

Hello,

I have some code that previously was working on my website, and after a Wordpress update, no longer works. I have been fiddling with this for the past month. PHPHelp.com has been helpful in making progress, but I’m not there yet. The end product I desire is to be able to type in a query (e.g. “plover”) into the box on this webpage (http://maavianrecords.com/database-ryan/) and return a table of results. Currently I only get the header for the table. I would deeply appreciate any direction you can provide. Below is my code:

echo "<form method=post action=''><input type=hidden name=todo value=search>
<input type=text name=search_text ><input type=submit value=Search><br>
</form>
";

$todo=$_POST['todo'];

// Get keyword passed from Query String ?keyword=gull

if($_REQUEST['keyword']=="")
{
// no keyword entered
// echo "You did not enter a keyword.";
}
else
{
$search_text = $_REQUEST['keyword'];
}

// Get keyword from input box typed in

if($_POST['search_text']=="")
{
// no keyword entered
// echo "You did not enter a keyword.";
}
else
{
$search_text=$_POST['search_text'];
}

if((isset($todo) and $todo=="search") or $search_text <> "" )
{
$search_text=ltrim($search_text);
$search_text=rtrim($search_text);
$q="species like '%$search_text%' or county like '%$search_text%' or observers like 
'%$search_text%'
or marc_number like '%$search_text%'";
$query="SELECT * FROM database; where $q order by pkid";

//Setup connection variables, such as database username and password
$hostname="#";
$username="##";
$password="###";
$dbname="####";

mysqli_connect($hostname, $username, $password, $dbname);

/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}

//Run the Query
$result = mysqli_query($query);	
mysqli_close();
echo mysqli_error();
echo "<table style='width:100%' border='2' cellspacing='5' cellpadding='5'>";
echo "<tr>";
echo "<th><font face='Times New Roman, Helvetica, sans-serif'>MARC number</font></th>";
echo "<th><font face='Times New Roman, Helvetica, sans-serif'>Species</font></th>";
echo "<th><font face='Times New Roman, Helvetica, sans-serif'>#</font></th>";
echo "<th><font face='Times New Roman, Helvetica, sans-serif'>Location</font></th>";
echo "<th><font face='Times New Roman, Helvetica, sans-serif'>County</font></th>";
echo "<th><font face='Times New Roman, Helvetica, sans-serif'>Arrival date</font></th>";
echo "<th><font face='Times New Roman, Helvetica, sans-serif'>Departure date</font></th>";
echo "<th><font face='Times New Roman, Helvetica, sans-serif'>Observers</font></th>";
echo "<th><font face='Times New Roman, Helvetica, sans-serif'>Report</font></th>";
echo "</tr>";

while($row=mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td><font face='Times New Roman, Times New Roman, Times New Roman'>";
echo "$row[marc_number]</font></td>";
echo "<td><font face='Times New Roman, Times New Roman, Times New Roman'>";
echo "$row[species]</font></td>";
echo "<td><font face='Times New Roman, Times New Roman, Times New Roman'>";
echo "$row[count]</font></td>";
echo "<td><font face='Times New Roman, Times New Roman, Times New Roman'>";
echo "$row[location]</font></td>";
echo "<td><font face='Times New Roman, Helvetica, sans-serif'>";
echo "$row[county]</font></td>";
echo "<td><font face='Times New Roman, Times New Roman, Times New Roman'>";
echo "$row[arrival_date]</font></td>";
echo "<td><font face='Times New Roman, Times New Roman, Times New Roman'>";
echo "$row[departure_date]</font></td>";
echo "<td><font face='Times New Roman, Times New Roman, Times New Roman'>";
echo "$row[observers]</font></td>";
echo "<td><font face='Times New Roman, Times New Roman, Times New Roman'>";
echo "$row[report]</font></td>";
}
echo "</table>";
//If the query returned results, loop through
// each result


}
else{}

echo the $query and paste it into phpmyadmin and see if it finds anything.

use mysql_num_rows to see how many records are found when php runs the $query.

Sponsor our Newsletter | Privacy Policy | Terms of Service