Mysql UPDATE需要再刷新一次才能显示值

时间:2017-02-17 18:12:29

标签: php mysql database

我正在尝试使用表单更新数据库。这是php代码

require 'connect.php';
include_once('header.php');
if (isset($_GET['id']) && is_numeric($_GET['id'])){
$id = $_GET['id'];
$result = mysql_query("SELECT * FROM table WHERE id=$id");
$get = mysql_fetch_assoc($result);

if(isset($_REQUEST['value1'])){
    $value1 = $_REQUEST['value1'];
    $value2 = $_REQUEST['value2'];
    $value3 = $_REQUEST['value3'];


    $update = mysql_query("UPDATE `table` SET `value1` = $value1, `value2` = $value2 WHERE `id` = $id");

    if($update){
            $msg = "<div class=\"alert alert-success\">Server UPDATED Successfully.</div>";
        }else{
            $msg ="<div class=\"alert alert-danger\">Failed to Update server!</div>";
        }

它完美地工作并更新数据库。但一旦显示“服务器已成功更新”。下面,它仍然以表格形式显示旧值。

如何在成功更新后重新加载页面或值?     }

2 个答案:

答案 0 :(得分:1)

操作顺序应为

  • 更新
  • 选择
  • 显示

所以你把更新部分放在第一位(注意防止sql注入):

require 'connect.php';
include_once('header.php');

$id = (integer) @$_GET['id'];
if (! empty($id)) {

  if (isset($_REQUEST['value1'])) {
    $value1 = mysql_real_escape_string($_REQUEST['value1']);
    $value2 = mysql_real_escape_string($_REQUEST['value2']);
    $value3 = mysql_real_escape_string($_REQUEST['value3']);

    $update = mysql_query("UPDATE `table` SET `value1` = '$value1', `value2` = '$value2' WHERE `id` = $id");

    if ($update) {
      $msg = "<div class=\"alert alert-success\">Server UPDATED Successfully.</div>";
    } else {
      $msg ="<div class=\"alert alert-danger\">Failed to Update server!</div>";
    }
  }

现在你可以选择:

  $result = mysql_query("SELECT * FROM table WHERE id=$id");
  $get = mysql_fetch_assoc($result);
}

现在您可以显示您的页面:

答案 1 :(得分:-2)

检查以下代码

    require 'connect.php';
    include_once('header.php');
    if (isset($_GET['id']) && is_numeric($_GET['id'])){
    $id = $_GET['id'];
    $result = mysql_query("SELECT * FROM table WHERE id=$id");
    $get = mysql_fetch_assoc($result);

    if(isset($_REQUEST['value1'])){
        $value1 = $_REQUEST['value1'];
        $value2 = $_REQUEST['value2'];
        $value3 = $_REQUEST['value3'];


        $update = mysql_query("UPDATE `table` SET `value1` = $value1, `value2` = $value2 WHERE `id` = $id");

        if($update){

          $result = mysql_query("SELECT * FROM table WHERE id=$id");
          $get = mysql_fetch_assoc($result);
                $msg = "<div class=\"alert alert-success\">Server UPDATED Successfully.</div>";
            }else{
                $msg ="<div class=\"alert alert-danger\">Failed to Update server!</div>";
            }