PHP form with MYSQL only posts what I put in. Not the search result
Ok so I'm frustrated. I cant figure out what I did wrong. I'm new to PHP
and MYSQL. Ok so I've got a database all set up. The tables are all set up
too. I'm having a difficult time with the PHP though.
The I have 15 fields that I want to search. But on the test run it the PHP
keeps posting what I put in the box. If I put in "Seth" It just simply
posts "Seth". That's it nothing from the database. I only put one field in
the PHP just to test it.
I put my code in here. The curly braces are in the right places. I had
trouble with indenting on this site.
The first one is my php.func.inc.
<?php
include 'db.inc.php';
function search_results($keywords) {
$returned_results = array();
$where = "";
$keywords = preg_split('/[\s]+/', $keywords);
$total_keywords = count($keywords);
foreach($keywords as $key=>$keyword){
$where .= " 'keywords' LIKE '%$keyword%' ";
if($key != ($total_keywords - 1))
{
$where .= " AND";
}
}
$results = "SELECT 'Investigator', 'ProjectTitle', 'Institution' FROM
'Studies' WHERE
where";
$results_num = $results = mysql_query($results) ?
mysql_num_rows($results) : 0;
if($results_num === 0)
{
return false;
}else {
while ($results_row = mysql_fetch_assoc($results)) {
echo $results_row['OtherNotes'];
}
}
}
?>
The next is my Index.php. I left out the file where I connect to the
Database.
<?php include 'func.inc.php'; ?>
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<h2> Search </h2>
<form action = "" method = "POST">
<p>
<input type = "text" name = "keywords" /> <input type = "submit" value =
"search" />
</p>
</form>
<?php
if(isset($_POST['keywords']))
{
$keywords =
mysql_real_escape_string(htmlentities(trim($_POST['keywords'])));
echo $keywords;
$errors = array();
if(empty($keywords)){
$errors[] = "Please enter a search term";
}else if (strlen($keywords) < 3) {
$errors[] = "Your search term must be a 3 or more character";
}else if (search_results($keywords === false)){
$errors[] = 'Your search for ' . $keywords . 'returned no results';
}
if(empty($errors)){
search_results($keywords);
} else {
foreach ($errors as $error) {
echo $error;
}
}
}
?>
</body>
<html>
No comments:
Post a Comment