创建URL和未定义的索引ID

时间:2016-05-02 22:42:38

标签: php mysql undefined-index

我创建了一个链接到我的Facebook帐户的博客网站,我想让用户看到更老的博客。因此,我创建了一个循环,根据标题输出网址,然后根据blog_id动态生成新页面。但是我有两个问题。

  1. 我只输出一个超链接,因此无法正确循环
  2. title.php页面上没有生成任何内容,我通过网址提交数据时得到未定义的索引blog_id
  3. ==============================

    $query="SELECT title FROM admin WHERE blog_id = $blog_id";
    $result=mysqli_query($conn, $query) or die(mysqli_error());
    $rstitle=mysqli_fetch_assoc($result);
    
    
    mysqli_close($conn);
    
    
    do { ?>
    <a href="title.php?blog_id= <?php echo $rstitle['blog_id']; ?> ">
    <ul>
    <li id = "title"> <?php echo $rstitle['title']; ?> </li><br />
    </ul>
    </a>
    
    <?php } while ($rstitle=mysqli_fetch_assoc($result)) ?>
    

    指向title.php页面的链接

    <?php
    $servername = "localhost";
    $dbusername = "root";
    $dbpassword = "";
    $dbname = "blog";
    
    // create connection
    $conn = mysqli_connect($servername, $dbusername, $dbpassword, $dbname);
    
    // check connection
    if (!$conn)
    {
        die("Connection failed: " . mysqli_connect_error());
    }
    // we get here the connection to the database was successful
    
    
    
    $query="SELECT blog FROM admin WHERE blog_id = $blog_id";
    $result=mysqli_query($conn, $query)or die(mysql_error());
    $rstitle=mysqli_fetch_assoc($result);
    
    if (mysqli_num_rows($result) > 0)
    {
        echo "<table border='0' style='width:50%'>";
    
    
        while($rstitle = mysqli_fetch_assoc($result))
        {
            echo "<tr>";
            echo "<td>" . $rstitle['blog'] . "</td>";
            echo "</tr>";
        }
        echo "</table>";
    }
    
    
    
    mysqli_close($conn);
    
    ?>
    

1 个答案:

答案 0 :(得分:0)

第一部分代码有几个问题。在这里,清理,评论,以帮助向您显示更改:

// Add "blog_id" to the list of fields selected here, so available below
$query="SELECT blog_id, title FROM admin WHERE blog_id = $blog_id";
$result=mysqli_query($conn, $query) or die(mysqli_error());
$rstitle=mysqli_fetch_assoc($result);

// Use the "while () {}" construction - it's easier to read
while ( $rstitle = mysqli_fetch_assoc( $result ) ) { 
// You had a space between blogid= and the ID - this will cause problems, so removed the space 
?>
<a href="title.php?blog_id=<?php echo $rstitle['blog_id']; ?> ">
<ul>
<li id = "title"> <?php echo $rstitle['title']; ?> </li><br />
</ul>
</a>
<?php }  
// close your connection - (not necessary) - at the END of your code
mysqli_close($conn);
?>

哪个应解决该部分中的Undefined Index问题,并会创建正确的链接列表。

在您的title.php代码中(仅在下面复制了部分代码),您不是在任何地方设置 $blog_id

$servername = "localhost";
$dbusername = "root";
$dbpassword = "";
$dbname = "blog";

// create connection
$conn = mysqli_connect($servername, $dbusername, $dbpassword, $dbname);

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

// Since the blog_id is being passed in the URL, get it.
// And since you're NOT preparing your query, but passing it straight in,
// we have to be sure to SANITIZE it:
$blog_id = ( isset( $_GET['blog_id'] ) ) ? (int)$_GET['blog_id'] : 0;

// I would recommend defending against no blog_id!
if ( ! $blog_id ) {
    echo 'Invalid blog id!';
    die();
}

$query="SELECT blog FROM admin WHERE blog_id = $blog_id";
$result=mysqli_query($conn, $query)or die(mysql_error());
$rstitle=mysqli_fetch_assoc($result);