Search mysqli table with form and display results

I'm searching a table with a form where the result could be from any of the 3 choices (cata, catb or catc) and then display the results, however my error warning keeps popping up and can't display the result?

I'm stuck...

<?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();
?> 
Asked By: Tatters
||

Answer #1:

No, you created a prepared statement, then you used the normal query which has the placeholders, thats why its not working. Execute the prepared statement, then fetch the result from that prepared statement.

$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>';
}

Or if you have the mysqlnd (mysql native driver / or you will not have that undefined function), you can also use 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>';
}

?>
Answered By: Kevin

Answer #2:

Try this. Works very well with prepared statement and uses Bootstrap CSS to display results. First connect your database file and include it. Next lay out the html container and table headers. I will give a very large example with 10 columns and a dynamic button to link to each contact (this is from a database app i built) so the user can view the contact once they find it and edit, etc.

  1. Start with form to post to

     <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. Have table headers ready

     <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. Open up php for script. Server looking for Boolean to execute on 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. Close php and close table. and done.

     </tbody>
     </table>
     </div>
    
Answered By: Garrick Crouch
The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .



# More Articles