Search code examples
phphtmlsqldatabaseblogs

How to select data from each row of database using php


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>Blogs</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 "<h3><a href = 'preview.php' style = 'color:black'>".$row['Title']."</a></h3>";
                                        $_SESSION['Title'] = $row['Title'];
                                        echo "<p>".$row['concept']."</p><hr>";
                                    }                        
                                }
                            ?>
                        </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>Preview</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?


Solution

  • you're putting the data of each row into the session, overwriting that value each time it loops, and then redirecting afterwards (your header values get overwritten on each loop too, only the last one is issued to the browser). The title you're putting into the session is unrelated to what was actually clicked on. Your form needs to send the ID of the clicked blog and then use that, not the data fetched from the database.

    Also for this scenario there's really no reason to post the form to the same script and then redirect (as per your original code). A simple hyperlink with a query parameter will do the job very nicely:

    Blog page:

    echo "<h3><a href = 'preview.php?id=".$row["ID"]."' style = 'color:black'>".$row['Title']."</a></h3>";
    

    Preview page:

    <?php
    session_start();
    $id = $_GET["id"]; //get the ID from the URL
    //... etc, then
    $sql = $db->prepare("SELECT * FROM blogs where ID = ?");
    $sql ->bind_param("i",$id);
    

    Note that I'm assuming here that your blogs have an integer ID column as the primary key in the database - this would be normal practice, and it's more robust to use that as the query parameter than the title (because titles can change, or not be unique, or have characters in them which don't work well in a URL, etc. so I strongly suggest you use the ID to identify the blog instead of the title).