PHP,插入时刷新页面

时间:2014-09-04 10:17:44

标签: php mysql

这是更新功能:

public function update($id) {
        $id = $_GET['id'];
        $stmt9 = $this->conn->prepare("UPDATE users SET `name`= :name, `email`= :email WHERE `id` = :id");


        $stmt9->bindParam(':name', $this->name);
        $stmt9->bindParam(':email', $this->email);
        $stmt9->bindParam(':id' , $id, PDO::PARAM_INT);
        $stmt9->execute();
        if ($stmt9) {
           $message = "User updated Sussesfully!";
            header('location:');
        }else {
            header("location:");
        }
        }
    }

现在在这里更新我想要刷新页面以便我可以看到更新的数据,但是现在如果它更新它将使用户保持编辑页面,如果我在数据库中看到,将显示输入的数据数据已更新,如果我用f5刷新页面,它将显示编辑页面已更新,当我提交表单时,它将获得更新,但在表单上它将显示主要数据,

所以我可以在提交后让页面刷新。重定向,如果重定向到列表页面,它将显示它已被更新,但在这里我想平均时间留在编辑页面和reaload页面,所以我可以看到更新的数据。

问候

5 个答案:

答案 0 :(得分:4)

简单:

header('Refresh: 0'); // 0 = seconds

即使您可以指定新位置

header("Refresh:2; url=new_page.php");

但是在使用header函数时,在调用之前不应该有任何回应, 但是如果你已经回应了任何东西,那么你可以使用html或javascript:

<强> HTML

<meta http-equiv="refresh" content="0">
<!--here you can also specify new url location-->
<meta http-equiv="refresh" content="0; url=http://url.com/">

<强> JS

window.location.reload();

更新,因为您无法使用header执行此操作:

if ($stmt9) 
{
    $message = "User updated Sussesfully!";
    echo '<meta http-equiv="refresh" content="0">';
 }
else 
{
    echo '<meta http-equiv="refresh" content="0">';
}

答案 1 :(得分:1)

您应该重定向到更新网址而不是重新加载 例如

  header("location:updateurl?id=1");

答案 2 :(得分:1)

您可以使用jquery来完成。

location.reload();

答案 3 :(得分:1)

如果您希望重定向到完全相同的页面,可以使用变量$_SERVER['REQUEST_URI']

header('location:' . $_SERVER['REQUEST_URI'] );

此代码熟悉重写规则(如果有)。

  

警告:无法修改标头信息 - 已由(ou

)发送的标头

您需要在PHP中打开输出缓冲。如果启用此选项,则需要检查代码是否在之前的某处不刷新输出缓冲区。

答案 4 :(得分:-1)

简单,改变这段代码:

if ($stmt9) {
    $message = "User updated Sussesfully!";
    header('location:');
}else {
    header("location:");
}
像这样

if ($stmt9) {
    $message = "User updated Sussesfully!";
    header('location: ?message='.$message);
}else {
    header("location: ?message=error");
}
相关问题