Arun3x3
Arun3x3

Reputation: 193

Data not displaying after refreshing the page

Data is displayed into a page(all.php) by retrieving the values from table.The main issue here is if I refresh the page using "f5-key" or "refresh-icon" in chrome the data stays good but once i select the url and hit "enter" the data vanishes. what could be the reason?

index.php

<form action="all.php" method="post">
    <p>Get all the Codes</p>
    <input  type="submit" name="all" >
</form>

all.php

<?php
    require 'config.php';

    if(isset($_POST['all']))
    {
        $new_obj= new config;
        $rows= $new_obj->Get_All();
        foreach($rows as $variable => $value){
        echo $value['EAN'].$value['ProductCode']."</br>";
    }
}

Config.php

<?php

class config
{
    function Get_ALL()
    {
        try {
            $conn = new PDO('mysql:host=localhost;dbname=test',$this->config['username'],$this->config['password']);
            $conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
            $stmt=$conn->prepare("select EAN,ProductCode from code");
            $stmt->execute();
            //$rows = $stmt->fetch(PDO::FETCH_ASSOC);
            $rows = $stmt->fetchall();
            return $rows;
        } 
        catch(PDOException $e)
        {
            echo  'Error:'. $e->getMessage();
        }
    }
}

$new_obj= new config;

Upvotes: 1

Views: 752

Answers (2)

Dragos
Dragos

Reputation: 1914

You are only querying the database if there's a POST request. When you load the page again, using the "click and enter" method described in your post, there is no POST request anymore, but only a GET request.

Your script also does not save in DB the data you are sending from the form.

The part of the code querying the database should be taken out of the if block testing for a POST Resquest and inside this if block you should call a (new) method to save the data in the databse.

all.php should look something like this:

    $new_obj= new config;
     if(isset($_POST['all']))
    {
      // save data in database
    }


    $rows= $new_obj->Get_All();
    foreach($rows as $variable => $value){
        echo $value['EAN'].$value['ProductCode']."</br>";
    }

Upvotes: 1

Prashant
Prashant

Reputation: 164

Data is not being display when you directly go to URL is because of IF condition that checks for $_POST variable.

What i mean is that when you click on submit button it sends the Post data (In this case it is the button), So data is displayed.

Now when you click on refresh button or hit F5 your browser resend the last request (In this case it is the same as when you click on submit button which has the Post data) so your data is displayed in this case.

But when you directly go to your URL it does not contain Post data anymore and so your if condition becomes the false and data will not be displayed anymore.

This might not be much of the help because i think it is already been answered...

Upvotes: 1

Related Questions