创建 URL 和未定义的索引 ID

Creating URL and Undefined Index ID

我正在创建一个链接到我的 Facebook 帐户的博客网站,我希望允许用户查看较早的博客。因此,我正在创建一个循环,它根据标题输出 urls,然后根据 blog_id 动态生成一个新页面。但是我有两个问题。

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

================================

$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);

?>

代码的第一部分有几个问题。在这里,已清理,并附有评论以帮助您了解更改:

// 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);