Refreshing page after sql update

Issue

After submitting information to my database, I want to refresh the page to show those changes, as when the form has been processed. The page “reloads” after submission but does not reflect the changes, so I assumed I would need to add a refresh command in when submit is pressed, but it seems to be too quick?

So I added a refresh time, but even cranking it up to 50 I got the same result.

If I press the button twice it refreshes with the correct information. Is there a better way to do this?

<?php
ini_set('display_errors',1);
error_reporting(E_ALL);

include_once '../includes/conn.php';

if(!$user->is_loggedin()){
    $user->redirect('../users/login.php');
}

$id = $_SESSION['session'];
$stmt = $conn->prepare("SELECT * FROM users WHERE id=:id");
$stmt->execute(array(":id"=>$id));

$userRow=$stmt->fetch(PDO::FETCH_ASSOC);
$location = isset($_POST['location']) ? $_POST['location'] : '';
$about = isset($_POST['about']) ? $_POST['about'] : '';
$title = isset($_POST['title']) ? $_POST['title'] : '';

if($title!=''){
    $sql = "UPDATE users SET title=:title WHERE id=:id";    
    $stmt = $conn->prepare($sql); 
    if($stmt == false){ 
        $error = "User Title update failed. Please try again.";
    } 

    $result = $stmt->execute(array(":title"=>$title, ":id"=>$id));

    if($result == false) { 
        $error = "User Title update failed. Please try again.";
    } 
    $count = $stmt->rowCount();
} 

if($location!=''){
    $sql = "UPDATE users SET location=:location WHERE id=:id";  
    $stmt = $conn->prepare($sql); 
    if($stmt == false){ 
        $error = "User Location update failed. Please try again.";
    } 

    $result = $stmt->execute(array(":location"=>$location, ":id"=>$id));

    if($result == false) { 
        $error = "User location update failed. Please try again.";
    } 
    $count = $stmt->rowCount();
} 

if($about!=''){
    $sql = "UPDATE users SET about=:about WHERE id=:id";    
    $stmt = $conn->prepare($sql); 
    if($stmt == false){ 
        $error = "about Me update failed. Please try again.";
    } 

    $result = $stmt->execute(array(":about"=>$about, ":id"=>$id));

    if($result == false) { 
        $error = "about Me location update failed. Please try again.";
    } 
    $count = $stmt->rowCount();
} 
?>
<!DOCTYPE html>
<html lang="en">    
<head>
    <title>EpicOwl UK | CMS Users Edit Profile</title>
    <meta charset="utf-8">
    <link rel="shortcut icon" href="../images/favicon.ico" type="image/x-icon" />
    <link rel="stylesheet" type="text/css" href="../css/main.css">
</head>
<body>
<div id="header">
    <a href="index.php"><img id="logo" src="../images/logo.png" /></a>
    <div id="navigation">
        <ul>
            <a href="../index.php"><li>Home</li></a>
            <a href="./profile.php"><li>My Profile</li></a>
            <a href="../admin/index.php"><li>Admin Panel</li></a>
        </ul>
    </div>
</div>
<div id="content">
<form method="post"><br />
    <h2>Edit Profile</h2>
    <label><strong>User Title:</strong></label><br />
    <input type="text" name="title" maxlength="50" placeholder="<?php echo ($userRow['title']); ?>" /><br /><br />
    <label><strong>My Location:</strong></label><br />
    <input type="text" name="location" maxlength="50" placeholder="<?php echo ($userRow['location']); ?>" /><br /><br />
    <label><strong>About Me:</strong><label><br />
    <textarea name="about" rows="13" cols="60" maxlength="255" placeholder="<?php echo ($userRow['about']); ?>"></textarea><br /><br />
    <button type="submit" name="update">Update</button><br /><br /><br />
    <?php
    if(isset($_POST['submit'])){
        header('refresh:20; Location: '.$_SERVER['REQUEST_URI']);
    }
    ?>
</form>
</div>
<div id="footer">
    <p class="copyright">&copy; EpicOwl UK. All Rights Reserved.</p>
</div>
</body>
</html>

Solution

I managed to get the desired result by adding header('Location: ./editprofile.php'); after the database was updated. See bellow:

if($title!=''){
    $sql = "UPDATE users SET title=:title WHERE id=:id";    
    $stmt = $conn->prepare($sql); 
    if($stmt == false){ 
        $error = "User Title update failed. Please try again.";
    } 

    $result = $stmt->execute(array(":title"=>$title, ":id"=>$id));

    if($result == false) { 
        $error = "User Title update failed. Please try again.";
    } 
    $count = $stmt->rowCount();
} 

After:

if($title!=''){
    $sql = "UPDATE users SET title=:title WHERE id=:id";    
    $stmt = $conn->prepare($sql); 
    if($stmt == false){ 
        $error = "User Title update failed. Please try again.";
    } 

    $result = $stmt->execute(array(":title"=>$title, ":id"=>$id));

    if($result == false) { 
        $error = "User Title update failed. Please try again.";
    } 
    $count = $stmt->rowCount();
    header('Location: ./editprofile.php');  
} 

Answered By – Sauced Apples

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published