搜索结果不起作用 - mysql,php

时间:2014-02-12 12:42:29

标签: php mysql search

我是php和MySQL的新手,但是我试图运行一个查询来搜索我的数据库中的表,然后从该条目的某些列中恢复结果。

e.g。搜索邮政编码并带回:姓名,地址,联系电话,邮政编码。

任何人都可以指出我正确的方向,即我失踪或错误的地方。

以下是详细信息

最新更新

表格

<td><form action="searchresults.php" method="post" name="form1" id="form1">
  <table width="100%" border="0" cellspacing="1" cellpadding="3">
    <tr>
      <td colspan="3"><strong>Find a Active Physio</strong></td>
    </tr>
    <tr>
      <td width="100">Physio Reference</td>
      <td width="301"><input name="PhysioReference" type="text" id="PhysioReference" /></td>
    </tr>
    <tr>
      <td>Name of Physio</td>
      <td><input name="Physio" type="text" id="Physio" /></td>
    </tr>
    <tr>
      <td>Contact Number</td>
      <td><input name="Number" type="text" id="Number" /></td>
    </tr>
    <tr>
      <td>Address</td>
      <td><input name="PhysiosAddress" type="text" id="PhysiosAddress" /></td>
    </tr>
    <tr>
      <td>Postcode</td>
      <td><input name="postcode" value="" type="text" id="postcode" />
        <input type="submit" name="submit" value="Search" /></td>
    </tr>
    <tr>
      <td>Physios Email</td>
      <td><input name="PhysiosEmail" type="text" id="PhysiosEmail" /></td>
    </tr>
    <tr>
      <td colspan="3" align="center">&nbsp;</td>
    </tr>
   </table>
   </form></td>

搜索结果

<?php

require_once('auth.php');

$host=""; // Host name 
$username=""; // Mysql username
$password=""; // Mysql password 
$db_name=""; // Database name 
$tbl_name="Physio"; // Table name 

 // Connect to server and select database.
   mysql_connect($host, $username, $password)or die("cannot connect"); 
   mysql_select_db($db_name)or die("cannot select DB");

    if(!isset($_POST['postcode'])) {
  header ("location:index.php");
 }
 $search_sql="SELECT * FROM `Physio` WHERE Postcode like '%".$_POST['postcode']."%'";
 $search_query=mysql_query($search_sql);
 $search_rs= mysql_num_rows($search_query) ;
 echo "<p> Results </p>" ;
 echo $_POST['{postcode'] ;
  if ($search_rs > 0)
   {
  echo "<p>".$search_rs['Postcode'] ."</p>" ;

   } else {
   echo "NO Results found";
   }
   ?>

3 个答案:

答案 0 :(得分:0)

使用mysql_query()代替mysql_quesry()

试试这个,

$link = mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name", $link)or die("cannot select DB");

$search_sql="SELECT * FROM `Physio` WHERE Postcode like '%".$_POST['physiopostcode']."%'";
$search_query=mysql_query ($search_sql);

?>
<p> Results </p>
<?php 
if (mysql_num_rows($search_query)>0) {
    while($search_rs=mysql_fetch_assoc($search_query)){
        //your code here
    }
} else {
    echo "NO Results found";
}

答案 1 :(得分:0)

实际上你并不是与数据库的连接。

更改此

 mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
 mysql_select_db("$db_name")or die("cannot select DB");

 mysql_connect($host, $username, $password)or die("cannot connect"); 
 mysql_select_db($db_name)or die("cannot select DB");

并修复了克里什所说的话。

修改

试试这个

 <p> Results </p>
 <?php if ($search_rs= mysql_num_rows($search_query) !=0)
  {
 echo "<p>".$search_rs['postcode'] ."</p>" ;

 } else {
 echo "NO Results found";
 }
 ?>

整个代码应该是那样的

  require_once('auth.php');

   $host=""; // Host name 
   $username=""; // Mysql username
   $password=""; // Mysql password 
   $db_name=""; // Database name 
   $tbl_name="Physio"; // Table name 

   // Connect to server and select database.
   mysql_connect($host, $username, $password)or die("cannot connect"); 
   mysql_select_db($db_name)or die("cannot select DB");

    if(!isset($_POST['physiopostcode'])) {
      header ("location:index.php");
     }
     $search_sql="SELECT * FROM `Physio` WHERE Postcode like '%".$_POST['physiopostcode']."%'";
     $search_query=mysql_query($search_sql);
     $search_rs= mysql_num_rows($search_query) ;
     echo "<p> Results </p>" ;
  if ($search_rs > 0)
   {
  echo "<p>".$search_rs ."</p>" ;

   } else {
   echo "NO Results found";
   }
   ?>

EDIT2:

将您的输入更改为此

  <input name="postcode" value="" type="text" id="postcode" />

目前

  <td>Postcode</td>
      <td><input name="postcode" value="" type="text" id="postcode" />
        <input type="submit" name="submit" value="Search" /></td>
    </tr>

EDIT3:

您正在拼写表格中的列名称,请注意,列名称区分大小写,带有大字母或小写字母。

将此更改为$search_rs['postcode']$search_rs

答案 2 :(得分:-1)

更好的方法是使用存储过程并转义POST。

$connection= mysqli_connect("localhost", "root", "pw","database") or die(mysqli_error("error connecting to database")); 
$postcode = htmlentities($_POST['physiopostcode']);   // Remove HTML tags from user entry
$sql = "SELECT * FROM Physio WHERE Postcode = ?"; // ? is our placeholder
$stmt = $connection->prepare($sql);
$stmt->bind_param('s', $postcode);  // Give our placeholder our user entered value
$stmt->execute();                   // Execute MySQL query
$result = $stmt->get_result();      // Grab results
if (isset($result)) {               // Print results (if any)
    while($row = $result->fetch_array(MYSQLI_ASSOC)) {
        echo $row['name'];
        echo $row['address'];
        echo $row['contactnumber'];
        echo $row['postcode'];
    }
} else {
echo "NO Results found";
};

希望有所帮助!