1

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

让我添加一些代码。

默认的.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>
?>

数据库配置文件

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

4 回答 4

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>
于 2012-05-18T08:38:57.047 回答
1

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

由于您的代码现在可以工作,通过 F5 刷新的人将再次插入相同的值... PRG 避免了这个问题。

于 2012-05-18T08:52:25.447 回答
1

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

<script type="text/javascript">
    document.location = "URL"
</script>
于 2012-05-18T08:44:32.857 回答
0

只做

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

在你做选择之前

于 2012-05-18T08:40:41.913 回答