动态页面。获取数据有问题

时间:2015-02-05 11:07:00

标签: php html

我看起来有20多个链接:



<div>
<a href='writer.php?id=1'>1st Writer</a>
<a href='writer.php?id=2'>2nd Writer</a>
<a href='writer.php?id=3'>3d Writer</a>
<a href='writer.php?id=4'>4th Writer</a>
<a href='writer.php?id=5'>5th Writer</a>
</div>
&#13;
&#13;
&#13;

我怎样才能得到&#34; id&#34;来自每个href =&#39; writer.php?id =&#39; &#39;并将其传递给$ id writer.php?

writer.php:

&#13;
&#13;
<?$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test.com";
$conn = mysqli_connect($servername, $username, $password, $dbname);

if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$sql = "SELECT * FROM articles WHERE id='$id'";
$result3 = mysqli_query($conn, $sql);


if (mysqli_num_rows($result3) > 0) {

    while ($row = mysqli_fetch_assoc($result3)) {

        echo $row[text];
    }
}
mysqli_close($conn);?>
&#13;
&#13;
&#13;

我只想点击链接并在新页面显示(writer.php)我的数据来自数据库,其中id =来自href的数字。 但我不知道如何......

2 个答案:

答案 0 :(得分:0)

writer.php?ID = 1

这可以通过PHP代码中的$ _GET ['id']来访问

  $id = mysqli_real_escape_string($_GET['id']);

然后根据此ID

编写您的查询
    $sql = "SELECT * FROM articles WHERE id='$id'";
    $result3 = mysqli_query($conn, $sql);

我建议不要用面向对象的方式编写程序方式来尝试mysqli

   $con = new mysqli("host","user","password","database");
   $id = $con->real_escape_string($_GET['id']);
   $result3 = $con->query("SELECT * FROM articles WHERE id='$id'");

答案 1 :(得分:-1)

$id = htmlspecialchars((int)$_GET['id']);
$sql = "SELECT * FROM articles WHERE id='$id'";

http://phpform.net/dynamic.php