SQL更新后刷新页面 [英] Refreshing page after sql update

查看:85
本文介绍了SQL更新后刷新页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将信息提交给我的数据库后,我想刷新页面以显示这些更改,就像处理表单一样。提交后页面重新加载,但没有反映更改,所以我认为在按下提交按钮时我需要添加一个刷新命令,但它似乎太快了?


$ b $所以我增加了一个刷新时间,但是甚至将其启动到50我得到了相同的结果。



如果我按下按钮两次,它会刷新正确信息。有没有更好的方法来做到这一点?

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

include_once'../includes/conn.php'; $!
$ b 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 =用户标题更新失败,请重试。;


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

if($ result == false){
$ error =用户标题更新失败,请重试。
}
$ count = $ stmt-> rowCount();

$ b $ if($ location!=''){
$ sql =UPDATE users SET location =:location WHERE id =:id;
$ stmt = $ conn-> prepare($ sql);
if($ stmt == false){
$ error =用户位置更新失败,请重试。;


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

if($ result == false){
$ error =用户位置更新失败,请重试。;
}
$ count = $ stmt-> rowCount();
}

if($ about!=''){
$ sql =UPDATE users SET about =:about WHERE id =:id;
$ stmt = $ conn-> prepare($ sql);
if($ stmt == false){
$ error =关于我更新失败,请重试。;


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

if($ result == false){
$ error =关于我的位置更新失败,请重试。
}
$ count = $ stmt-> rowCount();
}
?>
<!DOCTYPE html>
< html lang =en>
< head>
< title> EpicOwl UK | CMS用户编辑个人资料< / title>
< meta charset =utf-8>
< link rel =快捷图标href =../ images / favicon.icotype =image / x-icon/>
< link rel =stylesheettype =text / csshref =../ css / main.css>
< / head>
< body>
< div id =header>
< a href =index.php>< img id =logosrc =../ images / logo.png/>< / a>
< div id =navigation>
< ul>
< a href =../ index.php>< li>主页< / li>< / a>
< a href =./ profile.php>< li>我的个人资料< / li>< / a>
< a href =../ admin / index.php>< li>管理员面板< / li>< / a>
< / ul>
< / div>
< / div>
< div id =content>
< form method =post>< br />
< h2>编辑个人资料< / h2>
< label>< strong>使用者标题:< / strong>< / label>< br />
< input type =textname =titlemaxlength =50placeholder =<?php echo($ userRow ['title']);?> />< br />< br />
< label>< strong>我的位置:< / strong>< / label>< br />
< input type =textname =locationmaxlength =50placeholder =<?php echo($ userRow ['location']);?> />< br />< br />
< label>< strong>关于我:< / strong>< label>< br />
< textarea name =aboutrows =13cols =60maxlength =255placeholder =<?php echo($ userRow ['about']);?>> ;< / textarea>< br />< br />
< button type =submitname =update>更新< /按钮>< 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。保留所有权利。< / p>
< / div>
< / body>
< / html>


解决方案

您做错了,您必须处理在显示HTML之前提交表单提交。 PHP正在逐行执行,因此在您首先显示数据并且您正在检查表单是否已提交的情况下执行。只需将此代码移动到您的其他PHP代码所在的位置(您甚至可以删除refresh stuff命令):

  if (isset($ _ POST ['submit'])){
header('Location:'。$ _ SERVER ['REQUEST_URI']);

code $


编辑:

人们发明了MVC,因为当你混合HTML和PHP时,就像你的情况一样,不知道为什么事情不起作用。将您的PHP代码保留在文件顶部,尽量不要在HTML中的任何位置编写PHP代码,这样可以节省很多麻烦。并且,在调用之后,再使用 exit 停止代码执行。这里是你的代码的更新版本,简化和更算法(我希望你看到并理解代码流如何):

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

include_once'../includes/conn.php'; $!
$ b 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);

if(isset($ _ POST ['submit'])){
$ location = isset($ _ POST ['location'])? $ _POST ['location']:null;
$ about = isset($ _ POST ['about'])? $ _POST ['about']:null;
$ title = isset($ _ POST ['title'])? $ _POST ['title']:null;

$ sql_part = array();
$ prepare = array();
if($ location){
$ sql_part [] ='location =:location';
$ prepare [':location'] = $ location;

if($ about){
$ sql_part [] ='about =:about';
$ prepare [':about'] = $ about;

if($ title){
$ sql_part [] ='title =:title';
$ prepare [':title'] = $ title;
}
$ prepare [':id'] = $ id;

if(count($ sql_part)){
$ sql ='UPDATE users SET';
$ sql。= implode(',',$ sql_part);
$ sql。='WHERE id =:id';

$ stmt = $ dbh-> prepare($ sql);

if($ stmt){
//查找另一种方式,通过刷新
// $ result = $ stmt-> execute($ prepare);
// $ count = $ stmt-> rowCount();
header('Location:'。$ _SERVER ['REQUEST_URI']);
出口;
}
}
}
?>
<!DOCTYPE html>
< html lang =en>
< head>
< title> EpicOwl UK | CMS用户编辑个人资料< / title>
< meta charset =utf-8>
< link rel =快捷图标href =../ images / favicon.icotype =image / x-icon/>
< link rel =stylesheettype =text / csshref =../ css / main.css>
< / head>
< body>
< div id =header>
< a href =index.php>< img id =logosrc =../ images / logo.png/>< / a>
< div id =navigation>
< ul>
< a href =../ index.php>< li>主页< / li>< / a>
< a href =./ profile.php>< li>我的个人资料< / li>< / a>
< a href =../ admin / index.php>< li>管理员面板< / li>< / a>
< / ul>
< / div>
< / div>
< div id =content>
< form method =post>< br />
< h2>编辑个人资料< / h2>
< label>< strong>使用者标题:< / strong>< / label>< br />
< input type =textname =titlemaxlength =50placeholder =<?php echo($ userRow ['title']);?> />< br />< br />
< label>< strong>我的位置:< / strong>< / label>< br />
< input type =textname =locationmaxlength =50placeholder =<?php echo($ userRow ['location']);?> />< br />< br />
< label>< strong>关于我:< / strong>< label>< br />
< textarea name =aboutrows =13cols =60maxlength =255placeholder =<?php echo($ userRow ['about']);?>> ;< / textarea>< br />< br />
< button type =submitname =update>更新< /按钮>< br />< br />
< / form>
< / div>
< div id =footer>
< p class =copyright>& copy; EpicOwl UK。保留所有权利。< / p>
< / div>
< / body>
< / html>


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>

解决方案

You are doing it wrong, you have to process the form submission BEFORE showing the HTML. PHP is being executed line-by-line so in your case you are firstly showing the data and then you are checking if the form is submitted. Simply move this code up where the rest of your PHP code is located (you can even remove the refresh stuff command):

if(isset($_POST['submit'])){
    header('Location: '.$_SERVER['REQUEST_URI']);
}


Edit:

People invented MVC because of cases like yours when you are mixing HTML and PHP and wonder why things don't work. Keep your PHP code at the top of the files, try not to write PHP code anywhere inside HTML, you will save yourself a lot of trouble. And also, use exit after calling header to stop code execution any further. Here is an updated version of your code, simplified and more "algorithmic" (I hope you do see and understand how the code flow goes):

<?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);

if (isset($_POST['submit'])) {
    $location = isset($_POST['location']) ? $_POST['location'] : null;
    $about = isset($_POST['about']) ? $_POST['about'] : null;
    $title = isset($_POST['title']) ? $_POST['title'] : null;

    $sql_part = array();
    $prepare = array();
    if ($location) {
        $sql_part[] = 'location = :location';
        $prepare[':location'] = $location;
    }
    if ($about) {
        $sql_part[] = 'about = :about';
        $prepare[':about'] = $about;
    }
    if ($title) {
        $sql_part[] = 'title = :title';
        $prepare[':title'] = $title;
    }
    $prepare[':id'] = $id;

    if (count($sql_part)) {
        $sql = 'UPDATE users SET ';
        $sql .= implode(', ', $sql_part);
        $sql .= ' WHERE id = :id';

        $stmt = $dbh->prepare($sql);

        if ($stmt) {
            // Find another way too pass these through the refresh
            // $result = $stmt->execute($prepare);
            // $count = $stmt->rowCount();
            header('Location: '. $_SERVER['REQUEST_URI']);
            exit;
        }
    }
}
?>
<!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 />
</form>
</div>
<div id="footer">
    <p class="copyright">&copy; EpicOwl UK. All Rights Reserved.</p>
</div>
</body>
</html>

这篇关于SQL更新后刷新页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆