user3479267
user3479267

Reputation: 232

Searching multiple rows in MySQL database with PHP

I am trying to create an "explore" page on a site I am developing at the minute. Here are images of the page and the database for visual reference.

Database: http://gyazo.com/a48eb00458d3a93845bc666af23775c1 Webpage: http://gyazo.com/69d0889c77d784d12448827b603891be

Here is my current PHP Script:

$connect = mysql_connect("localhost","root","root");

if (!$connect) {
    die('lel' . mysql_error());
}

$search = $_GET['make'];

mysql_select_db("dapper");

$query = mysql_query("SELECT * FROM posts WHERE make = '$search'");

while ($rows = mysql_fetch_array($query)) {
    $id = $rows['id'];
    $photographer = $rows['photographer'];
    $carowner = $rows['car-owner'];
    $make = $rows['make'];
    $event = $rows['event'];
    $author = $rows['author'];
    $date = $rows['date'];

    echo "
    <article>
        <img src='uploads/$id.jpg'>
    </article>";
}

fclose($connect);

?>

And here is my HTML markup for searching:

    <ul>
        <h4>Make</h4>
        <a href="explore.php?make=vw"><li>Vw</li></a>
        <a href="explore.php?make=audi"><li>Audi</li></a>
    </ul>
    <ul>
        <h4>Event</h4>
        <a href="explore.php?event=h2oi"><li>H20i</li></a>
        <a href="explore.php?event=titanic dubs"><li>Titanic Dubs</i></li></a>
        <a href="explore.php?event=dubshed"><li>Dubshed</li></a>
        <a href="explore.php?event=players show"><li>Players Show</i></li></a>
    </ul>

As you can see from the webpage image the user should be able to search for events, makes and photographers. However I have only managed to get the script to successfully search for one thing. I feel I haven't explained this the best but I am NOT trying to allow the user to search for eg: photographers and events in the same search.

Thanks in advance.

Jamie McArdle, PHP Noob

Upvotes: 0

Views: 511

Answers (1)

Funk Forty Niner
Funk Forty Niner

Reputation: 74217

As per OP to close the question:

$search2 = $_GET['event']; - WHERE make = '$search' OR event = '$search2' or by replacing OR with AND if your rows match what you have in your present rows for those criterias.


A suggestion:

You may be best using dropdown menus or radio buttons, then using the values instead of multiple href's.

That way, your page won't be filled with links, should the criterias grow larger over time.


Footnotes:

mysql_* functions deprecation notice:

http://www.php.net/manual/en/intro.mysql.php

This extension is deprecated as of PHP 5.5.0, and is not recommended for writing new code as it will be removed in the future. Instead, either the mysqli or PDO_MySQL extension should be used. See also the MySQL API Overview for further help while choosing a MySQL API.

These functions allow you to access MySQL database servers. More information about MySQL can be found at » http://www.mysql.com/.

Documentation for MySQL can be found at » http://dev.mysql.com/doc/.


Edit:

For results or not:

if($query){

while ($rows = mysql_fetch_array($query)) {
    $id = $rows['id'];
    $photographer = $rows['photographer'];
    $carowner = $rows['car-owner'];
    $make = $rows['make'];
    $event = $rows['event'];
    $author = $rows['author'];
    $date = $rows['date'];

    echo "
    <article>
        <img src='uploads/$id.jpg'>
    </article>";
}
    }
    else{
    echo "No results.";
    }

Upvotes: 2

Related Questions