【问题标题】:My PHP code to search for a first name or last name from a table in a database is not printing anything to my webpage我从数据库中的表中搜索名字或姓氏的 PHP 代码没有在我的网页上打印任何内容
【发布时间】:2018-11-08 21:19:04
【问题描述】:

用户假设在网页的搜索栏中输入名字或姓氏,并且假设将表格中的属性列出到网页中。无论我在搜索栏中输入什么,都不会输出任何内容。我关注了this 视频,了解如何在 php 中搜索。我试着看了一个多小时,但我找不到任何问题。我的网页中没有错误消息。

<?php
$serverName = 'localhost';
$userName = 'root';
$password = '';
$databaseName = 'project3';

$connection = mysqli_connect($serverName, $userName, $password, 
$databaseName);
if (!$connection) {
  die("Connection Failed: " . mysqli_connect_error());
  }
echo "Connected Successfully!! <br>";
$output = '';
if (isset($_Post['search'])) {
  $searchq = $_Post['search'];
  $searchq = preg_replace("#[^0-9a-z]#i", "", $searchq);

  $query = mysqli_query("SELECT * from employee WHERE fname LIKE 
  '%$searchq%' OR"
        . "lname LIKE '%$searchq%") or die("failed");
$count = mysqli_num_rows($query);
if ($count == 0) {
    $output = 'No search results';
} else {
    while ($row = mysqli_fetch_array($query)) {
        $firstname = $row['fname'];
        $lastname = $row['lname'];
        $id = $row['id'];
        $output .= '<div>' . $firstname . '' . $lname . '</div>';
        echo "hi";
    }
 }
}

?>

<!DOCTYPE html>

 <html>
<head>
    <meta charset="UTF-8">
    <title>Database Webpage</title>
<font color ="white">
<h1 style="background-color:black; text-align: center">Datebase Website</h1>

 <font color ="black">
</head>
 <body>

   <form action = "index.php" method = "POST">
      <input type = "text" name ="search" placeholder="Search"/>
      <input type= "submit" value = ">>"/>


     </form>

    <?php print("$output"); ?>
 </body>
 </html>

【问题讨论】:

  • $_Post 应该是$_POST
  • 还有危险的不安全性,请勿在现实世界中使用。 6 岁了,这么老的编程教程都不值钱,语言不断变化
  • 您是否看到“连接失败”或“连接成功”?消息?如果你不这样做,那么$_Post 而不是$_POST 并不是唯一的问题。 (如果你这样做,它可能仍然不是唯一的问题,但如果你不这样做,它肯定不是。)
  • 视频使用 mysql,您刚刚添加了一个 i 并希望它可以工作 - 否

标签: php html sql database webpage


【解决方案1】:

mysqli_query 确实需要 2 个参数,正如 the PHP api 以您拥有的程序形式显示的那样。

因此,对于这一部分,调用应该如下所示:

$query = mysqli_query($connection, "SELECT * from employee WHERE fname LIKE 
           '%$searchq%' OR"
    . "lname LIKE '%$searchq%") or die("failed");

但是,查询会有问题。请注意查询的这一部分:

           '%$searchq%' OR" <--- No space after the OR
    . "lname LIKE '%$searchq%")
       ^--- No space before lname

这两个区域中的任何一个都需要空间。

查询字符串的最后一部分最终将如下所示:

'%$searchq%' ORlname LIKE '%$searchq%
^--- 麻烦(没有空格)       ^---这里有麻烦(缺少单引号)

有时单独设置查询很有用,这样您就可以回显它以检查它在关键字、列、值周围的间距、逗号用法(需要时)、正确引用等方面是否在语法上正确。

考虑这种差异:

$query = "SELECT * from employee WHERE fname LIKE '%$searchq%' OR "
       . "lname LIKE '%$searchq%'";
// query check (delete after validation, or comment-out)
echo $query;

$result = mysqli_query($connection, $query);
// I like to use $result, as the query call will return a result set with SELECT
// or false with failure. (though it can return true for other query types, see the api link)

将错误作为 die 消息的一部分输出也很有帮助:

if (!$result) { // Doh! something wrong...
  die('failed: ' . mysqli_error($connection));
} else {
  $count = mysqli_num_rows($result); // check the result count
  if ($count == 0) {
    $output = 'No search results';
  } else {
    while ($row = mysqli_fetch_array($result)) { // fetch a row
        $firstname = $row['fname'];
        $lastname = $row['lname'];
        $id = $row['id'];
        $output .= '<div>' . $firstname . '' . $lname . '</div>';
        echo "hi";
    }
  }
}

HTH

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-02
    • 1970-01-01
    • 2011-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多