【问题标题】:Search results not working - mysql, php搜索结果不起作用 - mysql、php
【发布时间】:2014-02-12 12:42:29
【问题描述】:

我对 php 和 MySQL 还很陌生,但我正在尝试运行一个查询,该查询将在我的数据库中搜索一个表,并从该条目中带回包含某些列的结果。

例如搜索邮政编码并带回:姓名、地址、联系电话、邮政编码。

谁能指出我缺少什么或哪里出错的正确方向。

以下是详细信息

最新更新

表格

<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";
   }
   ?>

【问题讨论】:

标签: php mysql search


【解决方案1】:

实际上你并没有连接到数据库。

改变这个

 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");

并修正 krish 所说的内容。

编辑:

试试这个

 <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:

您拼错了 table 中的列名,请注意,列名区分大小写,大写字母或小写字母。

改变这个:$search_rs['postcode']$search_rs

【讨论】:

  • 我不认为这种变化有什么不同。 $host === "$host" 因为双引号会解析字符串,还是我遗漏了什么?
  • 直接来自 phpmyadmin 参考、姓名、Line1、Line2、Line3、Line4、Line5、邮政编码、电话、手机、传真、电子邮件
  • 好的,我已经这样做了,现在它只说结果,没有别的。你想让我再给你看一下我在代码中的内容吗?#
  • @echo_Me 我已经用最新的代码更新了原帖
  • 不确定我最初是否做错了,但有人要求我使用 mysql_fetch_array 而不是 mysql_num_rows 并且它有效。我现在只想将我的结果放入表格中。谢谢你
【解决方案2】:

使用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";
}

【讨论】:

  • 这解决了问题,但没有找到任何结果,即使应该有....感谢您对拼写错误的敏锐观察
  • 看起来代码正在运行,但没有找到任何结果。我知道有搜索结果。我是否使用了错误的字段或表单中的任何内容?
【解决方案3】:

更好的方法是使用存储过程并转义您的 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";
};

希望有帮助!

【讨论】:

  • 我刚刚试过了,没用我把顶部的凭据替换了,但是出现页面无法显示
  • 这很奇怪,代码对我有用.. 请参阅collabedit.com/yhjch - 不要忘记编辑代码中的连接详细信息,并更改代码中对数据库列的所有引用以匹配您在数据库中的列。
  • 我将详细信息更改为我的,但没有运气,您要我显示您发送给我的那个链接上的代码吗?
  • @user3301611 它给你一个错误吗?您是否记得更改了 while 循环中的所有字段以匹配您在数据库中的字段?
  • 我现在试试,我已经更新了原帖上的代码,以防你想看看表单部分是否正确。
猜你喜欢
  • 2015-07-09
  • 1970-01-01
  • 1970-01-01
  • 2015-10-12
  • 2023-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多