更新后的值仅在刷新页面后显示

时间:2016-10-24 09:17:52

标签: php forms session browser

我正在尝试更新表单中的姓名,电子邮件,图片信息。

名称,电子邮件正在更新,但图片未保存在文件夹中,因此我删除了以下行中的;

if ($user_home->update($uname,$email, $phone, $uid));

现在,一旦我们点击“保存”按钮,图像就会保存在文件夹中,

但名字和&电子邮件显示旧值,&刷新页面后显示更新的值。但是,一旦我们点击“保存”按钮,我想显示更新的值。

形式

<form action="profile.php" method="POST" enctype="multipart/form-data">
Name : 
<?php echo $row['userName'] ?> <br/>
Email :
<?php echo $row['userEmail'] ?> <br>

<h3>photo</h3>

<input type="file" name="photo" id="fileSelect"><br> 
<input type="submit" name="submit" value="Save" />
</form> 

代码,电子邮件

<?php
include 'home.php';

// session_start();
require_once 'class.user.php';
$user_home = new USER();

if(!$user_home->is_logged_in())
{
    header("Location: index.php");
die();
}

$stmt = $user_home->runQuery("SELECT * FROM tbl_users WHERE userID=:uid");
$stmt->execute(array(":uid"=>$_SESSION['userSession']));
$row = $stmt->fetch(PDO::FETCH_ASSOC);

?>



    <?php

    $FORM['uname'] = "";
    $FORM['txtuname'] = "";
    if (isset($_POST['submit'])) {
    // new data
    $uname = $_POST['txtuname'];
    $email = $_POST['txtemail'];
    $phone = $_POST['phone'];
    $uid = (isset($_GET['userID']) ? intval($_GET['userID']) : -1);

    // query
    if ($user_home->update($uname,$email, $phone, $uid)); // This is the line
    {
        header("Location: profile.php");
        die(); 
    }
    }

    ?>

图片代码

<?php 
if(isset($_FILES["photo"]["error"])){ 
if($_FILES["photo"]["error"] > 0){ 
echo "Error: " . $_FILES["photo"]["error"] . "<br>"; 

} else{ 
$allowed = array("jpg" => "image/jpg", "jpeg" => "image/jpeg", "gif" => "image/gif", "png" => "image/png"); 
$filename = $_FILES["photo"]["name"]; 
$filetype = $_FILES["photo"]["type"]; 
$filesize = $_FILES["photo"]["size"]; 

// Verify file extension 
$ext = pathinfo($filename, PATHINFO_EXTENSION); 
if(!array_key_exists($ext, $allowed)) die("Error: Please select a valid file format."); 

// Verify file size - 5MB maximum 
$maxsize = 5 * 1024 * 1024; 
if($filesize > $maxsize) die("Error: File size is larger than the allowed limit."); 

// Verify MYME type of the file 
if(in_array($filetype, $allowed)){ 
// Check whether file exists before uploading it 
if(file_exists("upload/" . $_FILES["photo"]["name"])){ 
echo $_FILES["photo"]["name"] . " is already exists."; 

} else{ 
move_uploaded_file($_FILES["photo"]["tmp_name"], "upload/" . $_FILES["photo"]["name"]); 

echo "Your file was uploaded successfully."; 
} 
} else{ 

echo "Error: There was a problem uploading your file - please try again."; 
} 

} 

} else{ 
echo "Error: Invalid parameters - please contact your server administrator."; 
} 

?>

1 个答案:

答案 0 :(得分:0)

您需要在更新查询后执行select查询,否则您将获取旧信息,然后更新数据库中的记录。

<?php
include 'home.php';

// session_start();
require_once 'class.user.php';
$user_home = new USER();

if(!$user_home->is_logged_in())
{
    header("Location: index.php");
    die();
}

$FORM['uname'] = "";
$FORM['txtuname'] = "";
if (isset($_POST['submit'])) {
// new data
$uname = $_POST['txtuname'];
$email = $_POST['txtemail'];
$phone = $_POST['phone'];
$uid = (isset($_SESSION['userSession']) ? intval($_SESSION['userSession']) : 0);

// query
if ($uid > 0 && $user_home->update($uname,$email, $phone, $uid)) // This is the line
{
    header("Location: profile.php");
    die(); 
}
}

$stmt = $user_home->runQuery("SELECT * FROM tbl_users WHERE userID=:uid");
$stmt->execute(array(":uid"=>$_SESSION['userSession']));
$row = $stmt->fetch(PDO::FETCH_ASSOC);

?>

或者您可以使用此自定义更新:

// query
if ($uid > 0)
{
    $stmt = $user_home->runQuery("UPDATE tbl_users SET userName=:uname, 
                              userEmail=:email, phone=:phone WHERE userID=:uid");
    $stmt->execute(array(
                 ":uid"=>$_SESSION['userSession'],
                 ":email" => $email,
                 ":phone" => $phone,
                 ":uname" => $uname
    ));
    header("Location: profile.php");
    die(); 
}