PHP form with MYSQL only posts what I put in. Not the search result -


ok i'm frustrated. cant figure out did wrong. i'm new php , mysql. ok i've got database set up. tables set too. i'm having difficult time php though.
have 15 fields want search. on test run php keeps posting put in box. if put in "seth" posts "seth". that's nothing database. put 1 field in php test it.
put code in here. curly braces in right places. had trouble indenting on site.

the first 1 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 $key => $keyword) {         $where .= " 'keywords' '%$keyword%' ";         if ($key != ($total_keywords - 1)) {             $where .= " and";         }     }     $results = "select 'investigator', 'projecttitle', 'institution' 'studies'          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 index.php. left out file connect 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 search term";     } else if (strlen($keywords) < 3) {         $errors[] = "your search term must 3 or more character";     } else if (search_results($keywords === false)) {         $errors[] = 'your search ' . $keywords . 'returned no results';     }     if (empty($errors)) {         search_results($keywords);     } else {         foreach ($errors $error) {             echo $error;         }     } }?> </body> <html> 

one thing notice putting single quotes around table , column names.

you have written word "where" twice in query.

i think you're trying is:

$results = "select investigator, projecttitle, institution, studies " . $where; 

you should change line checks matching keywords from:

$where .= " 'keywords' '%$keyword%' "; 

to:

$where .= " keywords '%$keyword%' "; 

because again referring column name not string.

also, when try fetch result asking column 'othernotes', in query you're querying 'investigator', 'projecttitle' , 'institution' columns.

so query becomes:

$results = "select investigator, projecttitle, institution, othernotes studies " . $where 

you should change line reads:

foreach($keywords $key=>$keyword) 

to just:

foreach($keywords $keyword) 

and use $keyword variable same way are.

you can change line checks if form has been posted to:

if (!(is_null($_post['keywords']))) 

and make sure there 'keywords' column in studies table :)


Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -