2

I have a database that I am searching and displaying results from, each php query is in its own file being called by using require_once(ect) in the body.

They are all linked to the same submit button but I would like to only run the one that has text entered into it.

So I have 4 search box's each with there own file like this, which just searches a different column in the database.

I am wanting to only display 'no results found' or the amount of results found if the user has typed anything into that search box and submitted.

What is happening is the code is working but i get the other 3 'no results found' from the other files even though the user has not tried to search in that field.

I feel like there is an easy solution but I just cant figure it out, thanks in advance.

<?php

$stmtArtist = $mysqli->prepare("SELECT artist, genre, event, venue, eventdate FROM events WHERE artist = ?");
$stmtArtist->bind_param('s', $_GET['searchArtist']);
$stmtArtist->execute(); 
$stmtArtist->bind_result($artist, $genre, $event, $venue, $eventdate); 
$stmtArtist->store_result();
$numRows = $stmtArtist->num_rows;

if (isset($_GET['submit'])) {

    if ($numRows <= 0) {

        echo "no results found!";

    } else {

        echo "$numRows "." results found";

        while ($stmtArtist->fetch()) { 
            $timestampDate = strtotime($eventdate);
            $displayDate = date("D d M Y", $timestampDate);

            echo "<table border=1>"; 
            echo "<tr> <th>Artist</th> <th>Genre</th> <th>Venue</th> <th>Date</th> <th>Event</th> </tr>";
            echo "<tr> <td>$artist</td> <td>$genre</td> <td>$venue</td> <td>$eventdate</td> <td>$event</td></tr>";
            echo "</table>";

        }
    }
}
?>
9
  • 2
    Use empty() and maybe even isset() on your textarea POST var. Commented Nov 15, 2014 at 16:29
  • please don't use snippets with PHP this is useless. We don't see what you see Commented Nov 15, 2014 at 16:41
  • @SuperDJ I've removed the snippet. I think it's Stack's new feature method in order to make sure that an OP who doesn't have experience indenting code for a properly formatted question, is well indented. Stack will need to improve on that feature. Commented Nov 15, 2014 at 16:42
  • 1
    @fred -ii- do I sense a meta post here? Commented Nov 15, 2014 at 17:02
  • 1
    @DarylGill You've got yourself a deal ;) shake on it Commented Nov 15, 2014 at 17:43

2 Answers 2

4

Try to actually process the form when you have submitted and it has entered a value:

<?php
if (isset($_GET['submit']) && !empty($_GET['searchArtist'])) {

    $stmtArtist = $mysqli->prepare("SELECT artist, genre, event, venue, eventdate FROM events WHERE artist = ?");
    $stmtArtist->bind_param('s', $_GET['searchArtist']);
    $stmtArtist->execute(); 
    $stmtArtist->bind_result($artist, $genre, $event, $venue, $eventdate); 
    $stmtArtist->store_result();

    $numRows = $stmtArtist->num_rows;

    if ($numRows <= 0) {
        echo "no results found!";
    } else {
        echo "$numRows results found <br/>";

        echo "<table border=1>"; 
        echo "<tr> <th>Artist</th> <th>Genre</th> <th>Venue</th> <th>Date</th> <th>Event</th> </tr>";
        while ($stmtArtist->fetch()) { 

            $timestampDate = strtotime($eventdate);
            $displayDate = date("D d M Y", $timestampDate);

            echo "<tr> <td>$artist</td> <td>$genre</td> <td>$venue</td> <td>$eventdate</td> <td>$event</td></tr>";

        }
        echo "</table>";
    }
}
?>
Sign up to request clarification or add additional context in comments.

6 Comments

Hm, I'd go more with an !empty() for searchArtist in conjunction with an isset() just to be sure.
@Fred-ii- yeah better idea, since you'll take up values in there
I usually like to use empty(), as using isset() IMO, doesn't check if something was entered or not. +1 ;)
@Fred-ii- yeah, isset($_GET['submit'], $_GET['searchArtist']) is still true on ?submit=&searchArtist= which should be false
@James im glad this helped, credit goes to Fred as well.
|
0

Use empty instead, it does both isset (checks if variable/index is set) and checks if string is empty or not, and I've heard it's actually faster then isset too... You also want to make sure that the user input is sane/sanitize it. I just called a function here called "sane", you must write it yourself.

<?php
if (isset($_GET['submit']) && !empty($_GET['searchArtist']) && sane($_GET['searchArtist'])) {
    $stmtArtist = $mysqli->prepare("SELECT artist, genre, event, venue, eventdate FROM events WHERE artist = ?");
    $stmtArtist->bind_param('s', $_GET['searchArtist']);
    $stmtArtist->execute(); 
    $stmtArtist->bind_result($artist, $genre, $event, $venue, $eventdate); 
    $stmtArtist->store_result();
    $numRows = $stmtArtist->num_rows;
    if ($numRows <= 0) {
        echo "no results found!";
    } else{
        echo "$numRows "." results found";
        while ($stmtArtist->fetch()) { 
            $timestampDate = strtotime($eventdate);
            $displayDate = date("D d M Y", $timestampDate);
            echo "<table border=1>"; 
            echo "<tr> <th>Artist</th> <th>Genre</th> <th>Venue</th> <th>Date</th> <th>Event</th> </tr>";
            echo "<tr> <td>$artist</td> <td>$genre</td> <td>$venue</td> <td>$eventdate</td> <td>$event</td></tr>";
            echo "</table>";
        }
    }
}
?>

3 Comments

Using if (!empty($_GET['submit'])) doesn't make sense. OP wants to check if something is entered in textarea or not. The submit will always remain "not empty" when executed. Therefore it's best to use isset() on the submit button and an !empty() on the textarea, even checking if it is set also.
It's not a question of me being happy, it's just the way PHP works. Also, this sane($_GET - what is that? sane() isn't a core PHP function.
"You also want to make sure that the user input is sane/sanitize it. I just called a function here called "sane", you must write it yourself."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.