0

I'm using a form to perform a database search and display the result.

I don't know much about programming but I found a solution doing online searches.

It is working fine, but I would like to also display a message when there is nothing found in the database.

Just like a regular search page would output "No search results found."

Any assistance would be appreciated :)

This is my form:

<form method="post">
<div>
      <div>
         <input type="text" name="Referencia" style="text-align: center; margin-bottom: 5px;">
      </div>
      <div style="text-align: center;">
      <input type="submit" name="submit" value="Suchen" >
      </div>
</div>
<br/>
</form>

<?php if(isset($_POST["Referencia"])) {
global $wpdb;
$name = $_POST["Referencia"];
 $resultsap = $wpdb->get_results( $wpdb->prepare( "
SELECT * 
  FROM test_table 
 WHERE Referencia = %s
", $name ) );
foreach ($resultsap as $row) {
echo '<br/>';
echo '<div style="text-align: center;">';
   echo '<div>';
       echo '<b>Referenz: </b>' . $row->Referencia;
   echo '</div>';
   echo '<div style="margin-bottom: 10px;">';
       echo '<b>Stadt: </b>' . $row->City;
   echo '</div>';
   echo '<div>';
        echo '<b>Datum: </b>' . date('d.m.Y', strtotime($row->FecServicio));
   echo '</div>';
   echo '<div style="font-size: 20px; font-weight: bold; margin-top: 10px;">';
      echo 'Uhrzeit: ' . $row->Hora_recogida . ' Uhr';
   echo '</div>';
echo '</div>';
}
}
?>
Jonas Eberle
  • 2,835
  • 1
  • 15
  • 25
Nicole
  • 11
  • 1
    run your query directky without php. and chelc if your name is actually there.as we don't know your tables and data, we can't tell you if we would do something different – nbk Feb 21 '21 at 15:42
  • I think the data you have provided is insufficient to solve your problem. And your code needs some refining and tuning as well. Its completely fine as a beginner. If I have understood you correct, your code is working fine and displaying the results as required but you want to display an error message when no results are found. If so why don't you try this? `if(mysqli_num_rows(resultsap ) == 0){ echo "

    No results found

    ";}`

    – Rifky Niyas Feb 21 '21 at 16:18
  • If there's some aspect of the revised question that my answer doesn't address, see [Why should I provide an MCRE for what seems to me to be a very simple SQL query](http://meta.stackoverflow.com/questions/333952/why-should-i-provide-an-mcve-for-what-seems-to-me-to-be-a-very-simple-sql-query) – Strawberry Feb 21 '21 at 18:47

2 Answers2

0

As per the information you have provided here, you have mentioned that your code works and displaying the results as you expect. And what you are looking for is to display a message to the user when there are no matching records. Assuming that what you have mentioned is true, you can add an if condition to check whether there are records existing in the table as below...

if(mysqli_num_rows($resultsap) == 0){ 
  echo "<p>No results found<p>";
}

Adding this portion to your php code it will finally look something like this...

<?php 
if(isset($_POST["Referencia"])) {
  global $wpdb;
  $name = $_POST["Referencia"];
  $resultsap = $wpdb->get_results( $wpdb->prepare( "
    SELECT * 
    FROM test_table 
   WHERE Referencia = %s
    ", $name ) );
if(mysqli_num_rows($resultsap) == 0){ 
  echo "<p>No results found<p>";
}else{
foreach ($resultsap as $row) {
   echo '<br/>';
   echo '<div style="text-align: center;">';
   echo '<div>';
       echo '<b>Referenz: </b>' . $row->Referencia;
   echo '</div>';
   echo '<div style="margin-bottom: 10px;">';
       echo '<b>Stadt: </b>' . $row->City;
   echo '</div>';
   echo '<div>';
        echo '<b>Datum: </b>' . date('d.m.Y', strtotime($row->FecServicio));
   echo '</div>';
   echo '<div style="font-size: 20px; font-weight: bold; margin-top: 10px;">';
      echo 'Uhrzeit: ' . $row->Hora_recogida . ' Uhr';
   echo '</div>';
echo '</div>';
  }
 }
}
?>

Check this question for more.

Rifky Niyas
  • 1,737
  • 10
  • 25
-2

E.g.:

<?php

/*
DROP TABLE IF EXISTS my_table;
CREATE TABLE my_table
(team VARCHAR(12) NOT NULL
,date DATE NOT NULL
,qty_sales INT NOT NULL
,PRIMARY KEY(team,date)
);

INSERT INTO my_table VALUES
('inbound','2020-01-01',3),
('inbound','2020-02-01',2),
('inbound','2020-03-01',2),
('inbound','2020-04-01',7),
('sales','2020-03-01',1);

SELECT * FROM my_table;

+---------+------------+-----------+
| team    | date       | qty_sales |
+---------+------------+-----------+
| inbound | 2020-01-01 |         3 |
| inbound | 2020-02-01 |         2 |
| inbound | 2020-03-01 |         2 |
| inbound | 2020-04-01 |         7 |
| sales   | 2020-03-01 |         1 |
+---------+------------+-----------+
*/

require('path/to/pdo/connection/stateme.nts');

$query = "
SELECT team
     , date
     , qty_sales 
  FROM my_table 
 WHERE qty_sales > 7 
 ORDER 
    BY team
     , date
";

if ($data = $pdo->query($query)->fetchAll(PDO::FETCH_GROUP)) {
print_r($data);
} else {
   echo 'nothing to see here';
}
?>

Outputs: nothing to see here

Strawberry
  • 33,750
  • 13
  • 40
  • 57