douxian4376 2018-02-27 19:00
浏览 49
已采纳

PHP,MYSQL和超链接 - 从一个页面链接到一个详细页面(同一个表)[重复]

Not sure why this page doesn't seem to be working?

Trying to get the ID from the URL and use that to filter a table.

Example URL: http://example.com/page.php?id=123G

I'm getting 0 results when I type in that URL even though I know there is a match. Any ideas?

<html>
<head>
<style>table, th, td {border: 1px solid black;}</style>
</head>

<?php

$id = $_GET["id"];

$servername = "INSERTSERVER";
$username = "INSERTUSER";
$password = "INSERTPASSWORD";
$dbname = "INSERTDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} 


$pd = "

SELECT fac_id, pd, phone_pd 
FROM ft_location_db
WHERE fac_id = $id

";


$result = $conn->query($pd);

if ($result->num_rows > 0) {
echo "<table cellpadding=5 bgcolor=#FFFFFF><tr><th>PD</th><th>Phone</th>
</tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
    echo "<tr><td>" . $row["pd"]. "</td><td>" . $row["phone_pd"]. "</td>
</tr>";
}
echo "</table>";
} else {
echo "0 results";
}






$conn->close();
?> 

</body>
</html>
</div>
  • 写回答

1条回答 默认 最新

  • dqs66973 2018-02-27 19:16
    关注

    Please, see the note in the bottom of the answer.

    If fac_id is a string datatype, it should be wrapped.

    $pd = "
    SELECT fac_id, pd, phone_pd 
    FROM ft_location_db
    WHERE fac_id = '$id'
    ";
    

    You have to use single quotes, because your query is written inside double quotes.

    Using single quotes :

    $pd = '
    SELECT fac_id, pd, phone_pd 
    FROM ft_location_db
    WHERE fac_id = "' . $id . '"
    ';
    

    But (and -very- important), I suggest you to take a look to How can I prevent SQL injection in PHP? to secure your queries.

    $stmt = $conn->prepare('
        SELECT fac_id, pd, phone_pd 
        FROM ft_location_db
        WHERE fac_id = ?');
    $stmt->bind_param('s', $id);
    $stmt->execute();
    $result = $stmt->get_result();
    

    See also : bind_param()

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 51寻迹小车定点寻迹
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含
  • ¥15 麒麟V10桌面版SP1如何配置bonding
  • ¥15 Marscode IDE 如何预览新建的 HTML 文件