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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器