How to select data from each row of database using php

  blogs, database, html, php, sql

I am working to create a Blog page, I want to retrieve data from database using php. I have created 2 pages:

  1. Blogs Page
  2. Preview Page

Blog page is used to retrieve all data from the database and display only Title and Concept of the blog. After the concept there is a button to view whole blog. I am uploading example of UI, like how should it look.

Blog Example

After clicking on button, it should Navigate to preview page where whole content of blog should be dynamically changed as according to Title selected in Blog Page.

Now, I’ve created 2 blogs on backend but it is taking all the values from database of the latest one that I created recently, even when I am clicking on the button of the blog which I created 2 days ago.

Code for Blog Page that I written is:

<?php
    session_start();
    $dbHost = "localhost";
    $dbUsername = 'root';
    $dbPassword = '';
    $dbName     = 'database';
    $db = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);
    if($db->connect_error){
        die("Connection failed: " . $db->connect_error);
    }
    $sql = $db->prepare('Select * from blogs;');
    $sql->execute();
    $result = $sql->get_result();
?>
<!DOCTYPE html>
<html>
    <head>
        <title>Ayurvedajya</title>
        <?php include '_header.html'; ?>
        <link rel="stylesheet" href="css/about.css">
        <link rel="stylesheet" href="css/blog.css">
    </head>
    <body>
        <?php include '_navbar.html'; ?>
        <div class="bg2">
            <h1>Our Opinion</h1>      
        </div>
        <div class="col-sm-12">
            <div class="container">
                <div class="col-sm-12">
                    <div class="row">
                        <div class="col-sm-12">
                            <?php 
                                if($result)
                                {
                                    while($row = $result->fetch_assoc())
                                    {
                                        echo 
                                        "<form method = 'post'>
                                            <h3>".$row['Title']."</h3>
                                            <p>".$row['concept']."</p>
                                            <button class='btn btn-outline-success' name = 'data'>Know More</button><br><br><hr>
                                        </form>";
                                        if(isset($_POST['data']))
                                    {
                                        $_SESSION['Title'] = $row['Title'];
                                        header("Location:preview.php");
                                    }      
                                    }                       
                                }
                            ?>
                        </div><hr>
                    </div>
                </div>
            </div>
        </div>
        <div style="padding:50px"></div>
        <?php include '_footer1.html'; ?>
        <?php include '_footer.html'; ?>
    </body>
</html>

And code for Preview Page is:

<?php
    session_start();
    $title = $_SESSION['Title'];
    $dbHost = "localhost";
    $dbUsername = 'root';
    $dbPassword = '';
    $dbName     = 'database';
    $db = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);
    if($db->connect_error){
        die("Connection failed: " . $db->connect_error);
    }
    $sql = $db->prepare("SELECT * FROM blogs where Title = ?");
    $sql ->bind_param("s",$title);
    $sql->execute();
    $result = $sql ->get_result();
?>
<!DOCTYPE html>
<html>
    <head>
        <title>Blog</title>
        <?php include '_header.html'; ?>
        <link rel="stylesheet" href="css/treatment.css">
    </head>
    <body>
        <?php include '_navbar.html'; ?>
        <div class="col-sm-12">
            <div class="container-fluid">
                <div style="text-align:center">
                    <img src = "img/Ayurvedjya 02.png" alt = "Ayurvedajya Logo" width = "15%">
                </div>
                <?php
                    if($result)
                    {
                        while($row = $result->fetch_assoc())
                        {
                        echo "<h3>".$row['Title']."</h3><hr>";
                ?>
                <div class="col-sm-12">
                    <div class="row">
                        <div class="col-sm-9">
                            <?php echo "<p><b>".$row['Content']."</b></p>";?>    
                        </div>
                        <div class="col-sm-3">
                            <?php 
                                    echo '<img src="img/'.$row['image'].'" width= 100%/>';   
                            ?>
                        </div>
                    </div>
                </div>
                <?php }}else {echo "No Data";}?>
            </div>
        </div>
        <div style="padding:50px"></div>
        <?php include '_footer1.html'; ?>
        <?php include '_footer.html'; ?>
    </body>
</html>

Can anyone please confirm where I am going wrong and How can I fix this?

Source: Ask PHP

LEAVE A COMMENT