php problems in search function -


im trying add search function. want work that: exmple if have 5 field, , user wrote in 2, search based on 2 field. mean not neccesary write information in 5 field, want search happen in filled fields.

and works if write in fields (for have 2). , if write in 1 field doesnt show anything.

code:

 <html>  <head>  <link rel="stylesheet" type="text/css" href="style.css">  </head>  <body>  <div id="container">  <h1>Поиск</h1>  <form action="search_form.php" method="post">  <p>Направление</p>  <input type="text" name="course" />  <p>Форма обучения</p>  <input type="text" name="form" />  <input type="submit" value="Искать">  </form>  <?php  $db = mysql_connect("localhost", "root", "") or die (mysql_error ());  mysql_select_db("university", $db) or die(mysql_error());  $w = array('true');  if (isset($_post['course']))     {     $course = $_post['course'];     $w[] = "course='$course'";     }  if (isset($_post['form']))     {     $form = $_post['form'];     $w[]= "form='$form'";     }  $where = implode($w, ' , ');   $query = ('select * news '.$where);  $result = mysql_query($query,$db);  if($result !== false)     {       $news = mysql_fetch_array($result);       while($news = mysql_fetch_assoc($result)) {?>       <tr>       <td><?=$news['id']?></td>       <td><?=$news['program']?></td>       </tr><?       }       }  else       {       echo 'sorry, didn't find anything.';       }?>   </div>             </body> </html> 

you vulnerable sql injection attacks. learn , fix before else code.

plus logic faulty: mysql_query() returns false on errors. query has no results not error, , still returns valid query handle. it'll have 0 rows on it.

$result = mysql_query($query); if ($result === false) {    die(mysql_error()); } else if (mysql_num_rows($result) == 0) {    die("no results"); } else {    ... display results } 

Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -