【问题标题】:Search mysqli table with form and display results用表单搜索 mysqli 表并显示结果
【发布时间】:2014-11-29 23:40:05
【问题描述】:

我正在搜索一个表格,其结果可能来自 3 个选项(cata、catb 或 catc)中的任何一个(cata、catb 或 catc),然后显示结果,但是我的错误警告不断弹出并且无法显示结果?

我被困住了……

<?php

include("config.php");

$cata = $_POST['cata'];
$catb = $_POST['catb'];
$catc = $_POST['catc'];

$query = "SELECT * FROM photos WHERE cata=? OR catb=? OR catc=?";
$conn = $db->prepare($query);
$conn->bind_param("sss", $cata, $catb, $catc);

    if ($conn->execute()) {
    $result_db = $db->query($query) or die('Error perform query!');
    }
?>
<table border="1">
<tr>
    <th>cata</th>
    <th>catb</th>
    <th>catc</th>
</tr>
<?php
while ($r = $result_db->fetch_object()) {

    echo '<tr>';
    echo '<td>' . $r->cata . '</td>';
    echo '<td>' . $r->catb . '</td>';
    echo '<td>' . $r->catc . '</td>';
    echo '</tr>';
}
$db->close();
?> 

【问题讨论】:

    标签: php html mysql mysqli


    【解决方案1】:

    试试这个。与准备好的语句配合得很好,并使用 Bootstrap CSS 来显示结果。首先连接您的数据库文件并包含它。接下来布置 html 容器和表头。我将给出一个非常大的示例,其中包含 10 列和一个链接到每个联系人的动态按钮(这是来自我构建的数据库应用程序),因此用户可以在找到联系人后查看它并进行编辑等。

    1. 从要发布到的表单开始

       <form class="" method="post" action="#">
       <div class="form-group">
       <input class="form-control mr-sm-2 form-adjust" type="text" name="query" 
        placeholder="Please Enter Search Term"
        required></div>
        <button class="btn btn-outline-success my-2 my-sm-0 form-adjust" type="submit" 
         name="search-submit">Start Search</button></form>
      
    2. 准备好表头

       <div class="table-container">
       <h5 class="table-title">Below are the search results:</h5>
       <table class="table table-striped">
        <thead>
        <tr>
          <th></th>
          <th>Business or Name</th>
          <th>Name</th>
          <th>Last Name</th>
          <th>Address</th>
          <th>City</th>
          <th>State</th>
          <th>ZIP</th>
          <th>Phone</th>
          <th>Email</th>
         </tr>
         </thead>
         <tbody>
      
    3. 为脚本打开 php。服务器寻找布尔值以在 POST 上执行

        <?php
      
        if (isset($_POST['search-submit'])) {
         $query = $_POST['query'];
      
        $param = "%{$query}%";
        $stmt = $conn->prepare("SELECT * FROM `contacts_main` WHERE `BUSINESS OR NAME` 
        LIKE ? OR `NAME` LIKE ? OR `LAST NAME` LIKE ? OR `ADDRESS` LIKE ? OR `ADDRESS` 
        LIKE ? OR `CITY` LIKE ? OR `STATE` LIKE ? OR `ZIP` LIKE ? OR `PHONE` LIKE ? OR 
        `EMAIL` LIKE ?");
      $stmt->bind_param("ssssssssss", $param, $param, $param, $param, $param, $param, $param, $param, $param, $param);
      $stmt->execute();
      $stmt->store_result();
      if($stmt->num_rows === 0) exit('No Search Results');
      $stmt->bind_result($id, $businessorname, $name, $lastname, $address, $city, $state, $zip, $phone, $email);
      $stmt->fetch();
      
      while ($stmt->fetch()) {
      
        $showhtml .= "<tr><td>";
        $showhtml .= "<a href = \"view-contact.php?ContactID=$id\" class=\"btn btn-info\">View</a>";
        $showhtml .= "</td><td>";
        $showhtml .= $businessorname;
        $showhtml .= "</td><td>";
        $showhtml .= $name;
        $showhtml .= "</td><td>";
        $showhtml .= $lastname;
        $showhtml .= "</td><td>";
        $showhtml .= $address;
        $showhtml .= "</td><td>";
        $showhtml .= $city;
        $showhtml .= "</td><td>";
        $showhtml .= $state;
        $showhtml .= "</td><td>";
        $showhtml .= $zip;
        $showhtml .= "</td><td>";
        $showhtml .= $phone;
        $showhtml .= "</td><td>";
        $showhtml .= $email;
        $showhtml .= "</td></tr>";
      
        }
      
          echo $showhtml;
      
        }
         ?>
      
    4. 关闭 php 并关闭表。并完成。

       </tbody>
       </table>
       </div>
      

    【讨论】:

      【解决方案2】:

      不,您创建了一个准备好的语句,然后您使用了具有占位符的普通查询,这就是它不起作用的原因。执行准备好的语句,然后从准备好的语句中获取结果。

      $query = "SELECT * FROM photos WHERE cata=? OR catb=? OR catc=?";
      $conn = $db->prepare($query);
      $conn->bind_param("sss", $cata, $catb, $catc);
      $conn->execute();
      $conn->bind_result($cata, $catb, $catc);
      ?>
      <table border="1">
      <tr>
          <th>cata</th>
          <th>catb</th>
          <th>catc</th>
      </tr>
      <?php
      while ($conn->fetch()) {
      
          echo '<tr>';
          echo '<td>' . $cata . '</td>';
          echo '<td>' . $catb . '</td>';
          echo '<td>' . $catc . '</td>';
          echo '</tr>';
      }
      

      或者如果你有mysqlnd(mysql原生驱动/或者你不会有那个未定义的函数),你也可以使用get_result()

      $query = "SELECT * FROM photos WHERE cata=? OR catb=? OR catc=?";
      $conn = $db->prepare($query);
      $conn->bind_param("sss", $cata, $catb, $catc);
      $conn->execute();
      $results = $conn->get_result(); // i like this better
      ?>
      <table border="1">
      <tr>
          <th>cata</th>
          <th>catb</th>
          <th>catc</th>
      </tr>
      <?php
      while ($row = $results->fetch_assoc()) {
      
          echo '<tr>';
          echo '<td>' . $row['cata'] . '</td>';
          echo '<td>' . $row['catb'] . '</td>';
          echo '<td>' . $row['catc'] . '</td>';
          echo '</tr>';
      }
      
      ?>
      

      【讨论】: