点击链接

时间:2016-07-11 14:48:04

标签: php mysql

好人我遇到了一个我以前在这里讨论过的问题。我将mysql数据显示为链接,并希望在用户单击链接时从数据库加载更多数据,我查看了之前的所有问题和答案,但我无法对此进行排序。这是我设法拖延自己的距离:

的index.php:

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

$sql = "SELECT topic FROM steps";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
     // output data of each row
while($row = $result->fetch_assoc()) {
$topic = isset($_get['topic']);
$id = isset($_get['id']);

echo '<a href="moreinfo.php?id=' . $id . '">'.$row['topic'].'</a><br/>';
}
}
$conn->close();
?>

moreinfo.php:

$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
     die("Connection failed: " . $conn->connect_error);
}
$id = isset($_get['id']);
$sql = "SELECT * FROM steps WHERE id='$id'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
     // output data of each row
     while($row = $result->fetch_assoc()) {
     $topic = isset($_get['topic']);
     echo "Description: " . $row['topic'] . "<br />";


        }
}
else{
      echo "123";
}
$conn->close();
?> 

这并没有给我任何错误,只是这条线上的超链接 echo '<a href="moreinfo.php?id=' . $id . '">'.$row['topic'].'</a><br/>'; 在index.php上没有附加id($ id)到超链接,以便moreinfo.php可以正确执行。 我匆匆祈祷我在这里有所作为......

2 个答案:

答案 0 :(得分:0)

  1. 使用$_GET['id']
  2. $_get['id']内容
  3. isset的正确usege看起来像这样 $id = isset($_GET['id'])?$_GET['id']:false

  4. 始终检查您下次使用的参数是否真正通过了

    $id = isset($_GET['id'])?$_GET['id']:false; if(!$id) die('id was not passed');

  5. 最后,您使用来自GET的数据覆盖了db中的变量,这些数据应位于第一页(可能是复制和过去)

    $topic = isset($_get['topic']); $id = isset($_get['id']);

  6. 这是您的固定代码: 的的index.php

    <?php
        $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
        if ($conn->connect_error) {
            die("Connection failed: ".$conn->connect_error);
        }
    
        $sql    = "SELECT * FROM steps";
        $result = $conn->query($sql);
    
        if ($result->num_rows > 0) {
            // output data of each row
            while ($row = $result->fetch_assoc()) {
    
                echo '<a href="moreinfo.php?id='.$row['id'].'">'.$row['topic'].'</a><br/>';
            }
        }
        $conn->close();
    ?>
    

    <强> moreinfo.php

    <?php
        $conn = new mysqli($servername, $username, $password, $dbname);
    
    // Check connection
        if ($conn->connect_error) {
            die("Connection failed: ".$conn->connect_error);
        }
        $id     = isset($_GET['id'])?$_GET['id']:false;
        if(!$id)
            die('id was not passed');
    
        $sql    = "SELECT * FROM steps WHERE id='$id'";
        $result = $conn->query($sql);
    
        if ($result->num_rows > 0) {
            // output data of each row
            while ($row = $result->fetch_assoc()) {
                $topic = isset($_get['topic']);
                echo "Description: ".$row['topic']."<br />";
            }
        } else {
            echo "123";
        }
        $conn->close();
    ?>
    

    sql table create statement

    CREATE TABLE `steps` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `topic` varchar(45) DEFAULT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;
    

答案 1 :(得分:-1)

首先,首先要覆盖你的id

$id = isset($_get['id']);

所以当你尝试使用它时它保持bool值(true或false)而不是实际的$ id。你可以在两个文件中完成它。