【问题标题】:sql php results and searchsql php 结果和搜索
【发布时间】:2015-07-14 19:37:16
【问题描述】:

我有一个问题,对其他人来说很小,但对我来说却很大。自今年 3 月 15 日以来,我一直在从事一个项目。我不是网页设计师,但这只是我的一个爱好。

我的问题是:

  1. 当我调用这个程序来获取数据时,我会收到记录,但它只有在我搜索完整的邮政编码时才有效 (EX 1:n = no results EX 2:nn12ab = 5 results displayed)

  2. 我必须按某种顺序排列结果 (我的结果 = abcdabcdabcdabcdnn12ababcdabcdabcdabcdnn12ababcdabcdabcdabcdnn12ab, 我试图让他们的方式 名字/姓氏/电子邮件/邮政编码。

我已经检查了 w3schools 和所有其他模式,但我仍然在问这个问题。 :( 我完全知道它没有黑客保护,我只是想让它工作。 知道我需要在哪里放置任何作品吗? 提前TXT! HTML 搜索

<form method="post" action="search.php">
    <center>
      <h1>My Search Engine</h1>
      <input type="text" value="Search..." name="query" />
      <input type="submit" value="Find" name="list" />
    </center>
  </form>

PHP 搜索并显示代码

<?php
$servername = "localhost";
$username = "abcd";
$password = "******";
$dbname = "abcd";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT * FROM wfuk";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    echo "<table><td><tr><th>ID</th></td></tr>
    <th>Name</th></td></tr>
    <th>postcode</th</td>></tr>
    <th>trade</th></td></tr>
    <th>telephone</th></td></tr>
    <th>comments</th></td></tr></table>
    ";
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "<table><tr><td>"
         .$row["id"].
         "</td><td>"
         .$row["first_name"]
         .$row["last_name"].
         "</td></tr>".

         "<tr><td>"
         .$row["post_code"].
         "</td></tr>".

         "<tr><td>"
         .$row["trade"].
         "</td></tr>".

         "<tr><td>"
         .$row["telephone"].
         "</td></tr>".

         "<tr><td>"
        .$row["comments"].
        "</td></tr></table>"
        ;
    }
    echo "</table>";
} else {
    echo "0 results";
}
$conn->close();
?>

【问题讨论】:

  • 这里甚至没有任何搜索代码
  • 请不要在 cmets 中转储代码。编辑您的原始帖子以添加任何新信息。
  • 你没有做任何事情来过滤/限制查询结果。
  • 在查询中使用'like'子句,并为表格设置边框,问题解决了:)干杯

标签: php sql search


【解决方案1】:

替换这一行:

$sql = "SELECT * FROM wfuk";

通过

$sql = "SELECT * FROM wfuk where name like " . $_POST["query"] . " order by  first_name, last_name, email, postcode";

我假设表 wfuk 中的列具有您所说的名称。如果没有,请通过列名更改它们。

这不是进行搜索的最佳方式,因为它可能会引发 SQL 注入攻击。但是以您目前的知识水平,您可能还没有准备好接受其他解决方案。 稍后请自学有关此类操作的更好实践。

【讨论】:

    【解决方案2】:

    没什么好担心的,只是基本的困惑。 第一个问题的答案: 不要像这样使用 = 登录查询:

    Select * from table where postcode='.$variable.'

    使用like子句:

    Select * from table where postcode like '%.$variable.%'

    第二个问题的答案:

    为您的表格放置边框:

    <table border="1">
    

    【讨论】:

      【解决方案3】:

      这里有几件事

      1. 使用一些好的教程,不要相信w3school(有些人打电话 它是傻瓜)
      2. 从不使用用户Select * from table,而是指定列名 类似Select firstname, lastname from table
      3. 如果你想基于整数进行搜索,用户=签名例如where rollunme=134

      4. 如果要搜索某些文本/字符字段,请使用LIKE 运算符 例如firstname LIKE %zaffar%

      这些是应该可以帮助你的基本技巧......

      附言

      问题已编辑,但这些提示仍然适用,因为它们本质上非常通用,应该对您有所帮助

      【讨论】:

        【解决方案4】:

        是的,不幸的是,它没有使用此代码,但从听到我得知我错过了 THX ALL 的照片。 我使用的代码

        <?php 
        //load database connection
            $host = "localhost";
            $user = "change my";
            $password = "change my";
            $database_name = "chage my database name";
            $pdo = new PDO("mysql:host=$host;dbname=$database_name", $user, $password, array(
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
            ));
        // Search from MySQL database table
        $search=$_POST['search'];
        $query = $pdo->prepare("select * from change_table_name where change_title LIKE '%$search%' OR change_author LIKE '%$search%'  LIMIT 0 , 10");
        $query->bindValue(1, "%$search%", PDO::PARAM_STR);
        $query->execute();
        // Display search result
                 if (!$query->rowCount() == 0) {
        		 		echo "Search found :<br/>";
        				echo "<table style=\"font-family:arial;color:#333333;\">";	
                   // if need to multiply check clousley <tr> and </td> make shure they are on the right order
                        echo "<tr>
                        <td style=\"border-style:solid;border-width:1px;border-color:#98bf21;background:#98bf21;\">Change_Title_Books</td>
                        <td style=\"border-style:solid;border-width:1px;border-color:#98bf21;background:#98bf21;\">Change_Author</td>
                        <td style=\"border-style:solid;border-width:1px;border-color:#98bf21;background:#98bf21;\">change_Price</td></tr>";				
                    while ($results = $query->fetch()) {
                      // if need to multiply check clousley <tr> and </td> make shure they are on the right order
        				echo "<tr><td style=\"border-style:solid;border-width:1px;border-color:#98bf21;\">";			
                        echo $results['Chage_title'];
        				echo "</td><td style=\"border-style:solid;border-width:1px;border-color:#98bf21;\">";
                        echo $results['Change_author'];
        				echo "</td><td style=\"border-style:solid;border-width:1px;border-color:#98bf21;\">";
                      // if not needit delete "$". from bellow
                        echo "$".$results['change_price'];
        				echo "</td></tr>";				
                    }
        				echo "</table>";		
                } else {
                    echo 'Nothing found';
                }
        ?>
        <html>
        <head>
        <title> How To Create A Database Search With MySQL & PHP Script | Tutorial.World.Edu </title>
        </head>
        <body>
        <form action="search-database.php" method="post">
        Search: <input type="text" name="search" placeholder=" Search here ... "/>
        <input type="submit" value="Submit" />
        </form>
        <p><a href="http://tutorial.world.edu/web-development/how-to-create-database-search-mysql-php-script/">PHP MySQL Database Search</a> by <a href="http://tutorial.world.edu">Tutorial.World.Edu</a></p>
        </body>
        </html> 

        我发现了一个不同的代码,我会发布它以供将来参考,但你们让我理解我无法理解的想法

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-08-03
          • 1970-01-01
          • 1970-01-01
          • 2012-07-05
          • 2013-02-02
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多