-4

I'm creating a blog website linked to my Facebook account and I'm want to allow users to see older blogs. Therefore I'm creating a loop which outputs urls based the title, which then dynamically generates a new page based on the blog_id. However I have two problems.

  1. I only outputting one hyperlink and therefore doesn't loop through correctly
  2. Nothing is generating on the title.php page, and I get undefined index blog_id when submitting data through the url

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

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

Which links to the title.php page

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

?>
Zlini
  • 23
  • 2

1 Answers1

0

The first section of code has a couple of issues. Here it is, cleaned up, with comments to help show you the changes:

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

Which should solve your Undefined Index issue in that section, and will create a proper list of links.

In your title.php code, (only part of it is copied below), you aren't setting $blog_id anywhere.

$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);
random_user_name
  • 25,694
  • 7
  • 76
  • 115
  • Thanks for the fixes, the problem on the first page was caused by the wrong SELECT statement and I never would have been able to figure out without your help that I needed to sanitize the blog_id once I passed the ID through. – Zlini May 03 '16 at 18:20