doulan8054 2015-08-07 10:41
浏览 13
已采纳

PHP PDO将行的id传递给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

  • 写回答

3条回答 默认 最新

  • dou70260 2015-08-07 11:00
    关注

    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!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果