在同一页面上$ _POST后刷新数据

时间:2012-05-18 08:36:08

标签: php mysql redirect post insert

我有一个页面,其中谁发布了同一页面的帖子。我希望在成功的数据库INSERT之后用新数据刷新该页面。

让我添加一些代码。

如default.php

<?php
session_start();
require("dbconfig.php");
include("head.php");

//if user is not signed in, redirect to login page
if (!isset($_SESSION['sess_user']) ) {
   header ("Location: login.php");
   exit;
 }

?>

<body>

<div id="menu">
  <?php
     include("menu.php"); //include the menu
  ?>

</div>
<div id="page_content">
   <?php 
    include ($p); //Include selection from menu.php
   ?>
</div>
?>

dbconfig.php

<?php 
DEFINE ('DB_USER', 'someuser');  
DEFINE ('DB_PASSWORD', 'somepassword');  
DEFINE ('DB_HOST', 'localhost');  
DEFINE ('DB_NAME', 'somedatabase'); 

$connection = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or 
die('Connection to the specified database couldn\'t be established');  
mysql_select_db(DB_NAME)  or 
die ('Specified database couldn\'t be selected');

function db_escape ($post)
{
if (is_string($post) ) {
    if (get_magic_quotes_gpc() ) {
        $post = stripslashes($post);
    }
    return mysql_real_escape_string ($post);
}
foreach ($post as $key => $val) {
    $post [$key] = db_escape($val);
}
return $post;
}
?>

页/ cities.php

<?php 
//if not signed in correctly, redirect to login page
session_start();
if (!isset($_SESSION['sess_user']) ) {
    header ("Location: login.php");
    exit;
}
?>
<h2>Available cities</h2>
<?php
//List all available cities from database
$cities_query = "SELECT city_name FROM city_selection";
$cities_result = mysql_query($cities_query);

    while ($row = mysql_fetch_assoc($cities_result)) {
        echo $row['city_name'] . "<br />";
        }
?>
<?php
//Add new city to list
if(isset($_POST['submit'])){

    $city_name = $_POST['city_name'];
    $query="INSERT into city_selection (city_name) values ('$city_name')";
    $result = mysql_query($query);
}
 ?>
<hr>
<h2>Add new city</h2>
<form method="post" action="default.php?p=settings_cities">
Namn: <input type="text" name="city_name">
<input type="submit" name="submit" value="Add city">
</form>

这段代码可能并不漂亮,但可行。它成功地为数据库添加了一条新记录,但我希望在帖子后用我的ny记录刷新page / cities.php。这可能吗?

让我补充说这是我的第一个php页面。我只读了一些书,所以不要因为害怕一个坏程序员而抨击我:)

4 个答案:

答案 0 :(得分:6)

是的,只需将插入部件移动到选择部件上方即可。

页/ cities.php

<?php 
//if not signed in correctly, redirect to login page
session_start();
if (!isset($_SESSION['sess_user']) ) {
    header ("Location: login.php");
    exit;
}
//Add new city to list
if(isset($_POST['submit'])){

    $city_name = $_POST['city_name'];
    $query="INSERT into city_selection (city_name) values ('$city_name')";
    $result = mysql_query($query);
}
?>
<h2>Available cities</h2>
<?php
//List all available cities from database
$cities_query = "SELECT city_name FROM city_selection";
$cities_result = mysql_query($cities_query);

    while ($row = mysql_fetch_assoc($cities_result)) {
        echo $row['city_name'] . "<br />";
        }
?>

<hr>
<h2>Add new city</h2>
<form method="post" action="default.php?p=settings_cities">
Namn: <input type="text" name="city_name">
<input type="submit" name="submit" value="Add city">
</form>

答案 1 :(得分:1)

您还可以使用javascript刷新页面:

<script type="text/javascript">
    document.location = "URL"
</script>

答案 2 :(得分:1)

阅读Post-Redirect-Get模式,并在保存记录后将浏览器重定向到同一页面。重定向应该通过PHP的header函数完成。

由于您的代码现在正常工作,通过F5刷新的人将再次插入相同的值... P-R-G可以避免此问题。

答案 3 :(得分:0)

只需做

if(isset($_POST['submit'])){
  //yada yada

在执行选择之前