Kaylee Smith
Kaylee Smith

Reputation: 25

PHP PDO passing id of row to url

I need help! for the life of me, I can't figure out what I am doing wrong... I'm trying to build a simple search script that returns some results from a mysql database, and from those results i want to be able to click each individual one and pass it through to a new page, lets say results.php, where the details of that result is loaded, similar to an ecommerce site i suppose, where a result of products are displayed, and on clicking one, that product and its additional details are loaded in a new page. I know i need to pass the id into the url, but the problem i keep getting is that it is only passing the last id from the search result into the url... however collects all the correct id's in the search results...?

I am extremely new to php pdo etc. I have tried to find a solution online, spent days looking, but I keep either getting the same result, or I get and error, which would suggest I didnt do it right... Anyway, this is what I have so far:

<?php

require('inc/connect/config.php');

$output = "";

if(!isset($_POST['search'])) {
$output = "";
}
if(isset($_POST['search'])) {
$search_query = $_POST['search'];
$search_query = preg_replace("#[^0-9a-z]#i", "", $search_query);

try {
    $query = $db->prepare('SELECT * FROM clients WHERE name LIKE "%' . $search_query . '%" OR town LIKE "%' . $search_query . '%"');
    $query->execute();

    while($r = $query->fetch(PDO::FETCH_ASSOC)) {
        $bname = $r['name'];
        $btown = $r['town'];
        $id = $r['id'];

        $output .= $bname . '<br>' . $btown . '<br>' . $id . '<br><br>';
    }
    if($query->rowCount() < 1) {
        $output = "no results found";
    }

} catch (PDOException $e) {
    echo "failed search";
}
}

?>

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>search</title>
<link rel="stylesheet" href="css/style.css" />
</head>
<body>
<div style="padding: 20%; text-align: center;">
    <h3>search</h3>
    <p>or search by <a href="categories.php">category</a></p><br>

    <form method="post" action="search-test.php">
        <label>&lt;3 </label><input type="text" name="search" id="search" placeholder="search by brand or town..." style="width: 40%;"><br><br>
        <input type="submit" value="search">
    </form>
    <br>
    <a href="results.php?id=<?php echo $id; ?>"><?php echo $output; ?></a>
</div>


</body>

</html>

When performing a search, the results returned are like this:

business1
town
1

business2
town
2

business3
town
3

but for the url link through to the results.php page, it assigns each result with the id of 3... it only ever passes the last id in the results set to the url... what am i doing wrong? please help!!

thanks in advance

Upvotes: 0

Views: 2221

Answers (3)

Julian Laval
Julian Laval

Reputation: 1250

Firstly, I would suggest refactoring a few bits of your current code, namely how you're preparing your statements. Instead of including your parameters directly in the prepare statement, you should abstract these to the params method; not doing so defeats the purpose of preparing your statements and opens you up to SQL injections!

$query = $db->prepare('SELECT * FROM clients WHERE name LIKE :query OR town LIKE :query');
$params = array(':query' => '%' . $search_query . '%');
$query->execute($params);

Secondly, PHP PDO has a fetchAll method over which you can iterate, rather than calling fetch a number of times.

$results = $query->fetchAll(PDO::FETCH_ASSOC);
if(count($results) < 1) {
    $output = "no results found";
}
else {
    for($results as $r) {
        $bname = $r['name'];
        $btown = $r['town'];
        $id = $r['id'];

        $output .= $bname . '<br>' . $btown . '<br>' . $id . '<br><br>';
    }
}

Finally, to answer your bug: $id is being reset each time you go through the while loop, and will exit with the value 3. You're then outputting your content directly to the HTML, and thus will simply use the current value of $id (and the link will wrap all of your content rather than each result individually). Instead, I would advise you to add the link tag to your loop so as to create individual links for each result:

$output .= '<a href="results.php?id=' . $id . '">' . $bname . '<br>' . $btown . '<br>' . $id . '<br><br></a>';

And then simply output to the HTML via <?php echo $output; ?>

Good luck!

Upvotes: 1

Xebax
Xebax

Reputation: 470

The link to the result page is <a href="results.php?id=<?php echo $id; ?>"> and $id has the value it had when existing the while loop.

In the loop, you should generate one link per result or concatenate all the id in one ids parameter that you would pass to the result page, for example: ids=1,2,3.

Upvotes: 0

dlegall
dlegall

Reputation: 412

$id is rewrote each time you're passing to the while loop. So, its final result is your last id, that's true.

Your first choice is to store your SQL results into array and then loop on it on the view. Your second choice is the following :

Replace :

$output .= $bname . '<br>' . $btown . '<br>' . $id . '<br><br>';

By :

$output .= "<a href=\"results.php?id=$id\">".$bname . '<br>' . $btown . '<br>' . $id . '</a><br><br>';

And :

<a href="results.php?id=<?php echo $id; ?>"><?php echo $output; ?></a>

By :

<?php echo $output; ?>

Upvotes: 2

Related Questions