更新页面后获取更改后的数据 [英] Fetch altered data after page is updated

查看:62
本文介绍了更新页面后获取更改后的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当更新按钮被提交时,函数 edit_page()被调用。这个函数用输入的新数据修改数据库表的行。

这个工作正常的表被修改正确的条目是可以的。

但问题是当更新按钮被提交时
1.条目数据库插入或正确更改。

2.但是,当页面重新加载此更新页面的内容保持原来的状态时,或者在提交之前或之后更新之前。 b


我的代码:

 <?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'); //执行SQL查询并返回记录
$ row = mysql_fetch_array($ result);
$ page_title = $ row ['page_title'];
$ page_content = $ row ['page_content'];
?>
< form method =postaction =name =edit_pageclass =edit_page>
< h4>页面标题:< / h4> < input type =textname =titleclass =titleplaceholder =添加页面的标题required value =<?php echo $ page_title;?>/>< br / >
< h4>页面内容:< / h4>< br />
< textarea cols =80id =contentname =contentrows =10><?php echo $ page_content;?>< / textarea>
< input type =hiddenname =page_editsvalue =yes/>
< input type =submitname =edit_pageclass =buttonvalue =Update/>

<?php
save_edits(); }
$ b $ 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 =更新页面SET page_title ='$ page_title',page_content ='$ page_content',date_gmt ='$ date'WHERE page_id ='$ page_id';
$ result = mysql_query($ query)或死(Unable to create Page:。mysql_error());
}
}
?>
< div class =right_sidebar>
<?php edit_page();?>< / div>

最后,我的意思是我只是想要像wordpress这样的功能,当更新按钮被点击后我们看到更新的数据。

解决方案

您在这里使用PHP的程序方式。这意味着这些语句会一个接一个地执行,所以问题在于您放置语句的方式。



在您的代码中,您首先显示表单,然后才更新所以这就是为什么以前的值会被获取,尽管更新只是在稍后发生。



解决方案:功能 save_edits (),它的调用应该是第一个,然后是 edit_page()。



另一个从安全角度来看,重要的是你直接插入你从地址栏获得的值。现在就是这样,有人可以通过写一个代码。您可以使用 mysql_real_escape_string()来防止它(尽管不是完全)或更好:

请不要在新代码中使用 mysql_ * 函数 。他们不再维护并被正式弃用。请参阅 红色框 ?了解 准备语句 ,并使用 PDO MySQLi - 这篇文章将帮助你决定哪个。如果您选择PDO,这是一个很好的教程



最后,您并未关闭< form> 标记。


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.

解决方案

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.

这篇关于更新页面后获取更改后的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆