duanjunjie0196 2014-05-14 20: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-14 21: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 IBMP550小型机使用串口登录操作系统
  • ¥15 关于#python#的问题:现已知七自由度机器人的DH参数,利用DH参数求解机器人的逆运动学解目前使用的PSO算法
  • ¥15 发那科机器人与设备通讯配置
  • ¥15 Linux环境下openssl报错
  • ¥15 我在使用VS编译并执行之后,但是exe程序会报“无法定位程序输入点_kmpc_end_masked于动态链接库exe上“,请问这个问题有什么解决办法吗
  • ¥15 el-select光标位置问题
  • ¥15 单片机 TC277 PWM
  • ¥15 在更新角色衣服索引后,Sprite 并未正确显示更新的效果该如何去解决orz(标签-c#)
  • ¥15 VAE代码如何画混淆矩阵
  • ¥15 求遗传算法GAMS代码
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部