duanjunjie0196 2014-05-15 04:31
浏览 116
已采纳

页面更新后获取更改的数据

When update button is submitted a function edit_page() is called. This function alter the database table row with new data entered.
This works fine table altered correctly entries are ok.
But problem is that when update button is submitted
1. Entries is database inserted or altered correctly.
2. But when page reloads content of this updated page remains as it is as previous or like before updation on front end or just after submission.

My code:

<?php

function edit_page()
{
add_cat();
global $page_id; 
?>
<?php
if (isset($_GET['page_action']) && ($_GET['page_action'] == 'edit'))
    {
    $page_id = $_GET['post'];
    } 
?>
<?php
$page_id = $_GET['post'];
$result = mysql_query("SELECT * FROM pages WHERE page_id = '$page_id'"); //execute the SQL query and return records
$row = mysql_fetch_array($result);
$page_title = $row['page_title'];
$page_content = $row['page_content']; 
?>
<form  method="post" action="" name="edit_page" class="edit_page">
<h4>Page Title:</h4> <input type="text" name="title" class="title" placeholder="Add title of the Page" required value="<?php echo  $page_title;?>"/><br/>
<h4>Page Content:</h4><br/> 
<textarea cols="80" id="content" name="content" rows="10"><?php echo $page_content;?></textarea>
<input type="hidden" name="page_edits" value="yes" />   
<input type="submit" name="edit_page"  class="button" value="Update"/>

<?php
save_edits(); }

function save_edits()
    {
if (isset($_POST['edit_page']) && $_POST['page_edits'])
    {
    $page_id = $_GET['post'];
    $page_id = $_GET['post'];
    $page_title = $_POST['title'];
    $page_content = $_POST['content'];
    $date = date('Y-m-d h:i');
    $query = "UPDATE pages SET page_title='$page_title', page_content='$page_content', date_gmt='$date' WHERE page_id = '$page_id'";
    $result = mysql_query($query) or die("Unable to create Page: " . mysql_error());
    }
}
?>
<div class="right_sidebar">
<?php edit_page();?></div>

Finally, my mean is that i just want functionality like wordpress in which when update button is clicked just after that we see updated data.

  • 写回答

1条回答 默认 最新

  • douliu7929 2014-05-15 05:06
    关注

    You're doing PHP the procedural way here. That means the statements are executed one after another so the problem lies in the way you place your statements.

    In your code, you are displaying the form first and only then updating it, so that's why the previous values get fetched although update is happening only later.

    Solution: The function save_edits() and its call should come first followed by edit_page().

    Another important thing in terms of security, you are directly inserting the value you get from the address bar. Right now the way it is, someone can drop your whole table by writing in a piece of code. You could use mysql_real_escape_string() to prevent it (although not totally) or better yet:

    Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

    Lastly, you are not closing your <form> tag.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 深度学习残差模块模型
  • ¥50 怎么判断同步时序逻辑电路和异步时序逻辑电路
  • ¥15 差动电流二次谐波的含量Matlab计算
  • ¥15 Can/caned 总线错误问题,错误显示控制器要发1,结果总线检测到0
  • ¥15 C#如何调用串口数据
  • ¥15 MATLAB与单片机串口通信
  • ¥15 L76k模块的GPS的使用
  • ¥15 请帮我看一看数电项目如何设计
  • ¥23 (标签-bug|关键词-密码错误加密)