radian Posted February 19, 2009 Share Posted February 19, 2009 Ok, i have a 3 php scripts. index.php, compose_news.php, list_news.php. index.php shows the news articles list_news.php lists all the news articles available in the table compose_news.php allows you to compose new news articles This is the problem; i compose a news article, it inserts the data into the table perfectly well, and it shows up in the index and list_news pages fine. But when i refresh the list_news page after using compose_news, it replicates the article i just submitted. This happens everytime i refresh. So if I refresh 10 times, the article will replicate 10 times. index.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" > <head> <title>Welcome to my site</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <style type="text/css"> h1, h3 { text-align:center; } h3 { background-color:black; color:white; font-size:0.9em; margin-bottom:0px; } .news p { background-color:#CCCCCC; margin-top:0px; } .news { width:70%; margin:auto; } </style> </head> <body> <h1>Welcome to my site!</h1> <p>Here are the latest items of news:</p> <?php mysql_connect("localhost", "root", ""); mysql_select_db("news"); // We retrieve the 5 latest items of news $return = mysql_query('SELECT * FROM news ORDER BY id DESC LIMIT 0, 10'); while ($data = mysql_fetch_array($return)) { ?> <div class="news"> <h3> <?php echo $data['title']; ?> <em> - <?php echo date('m/d/Y H\hi', $data['timestamp']); ?></em> <?php echo 'written by ' . $data['author']; ?> </h3> <p> <?php // We delete the potential backslashes THEN we create the HTML enter key pressed (<br />) $content = nl2br(stripslashes($data['content'])); echo $content; ?> </p> </div> <?php } // End of news loop ?> <p><a href="list_news.php" />New News</a></p> </body> </html> list_news <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" > <head> <title>News list</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <style type="text/css"> h2, th, td { text-align:center; } table { border-collapse:collapse; border:2px solid black; margin:auto; } th, td { border:1px solid black; } </style> </head> <body> <h2><a href="compose_news.php">Add an item of news</a></h2> <?php mysql_connect("localhost", "root", ""); mysql_select_db("news"); //----------------------------------------------------- // Check 1 : do we want to post an item of news? //----------------------------------------------------- if (isset($_POST['title']) AND isset($_POST['content'])) { $title = addslashes($_POST['title']); $content = addslashes($_POST['content']); $author = $_POST['author']; // We check if it is a news modification or not if ($_POST['id_news'] == 0) { // It is not a modification, we create a new entry in the table mysql_query("INSERT INTO news VALUES('', '" . $title . "', '" . $content . "', '" . time() . "', '" . $author . "')"); } else { // We protect the variable "id_news" to avoid an SQL fault $_POST['id_news'] = addslashes($_POST['id_news']); // This is a modification, we only update the title and content mysql_query("UPDATE news SET title='" . $title . "', content='" . $content . "' WHERE id='" . $_POST['id_news'] . "'"); } } //-------------------------------------------------------- // Check 2 : do we want to delete an item of news ? //-------------------------------------------------------- if (isset($_GET['delete_news'])) // If we ask to delete an item of news { // Then we delete the corresponding item of news // We protect the variable "id_news" to avoid an SQL fault $_GET['delete_news'] = addslashes($_GET['delete_news']); mysql_query('DELETE FROM news WHERE id=\'' . $_GET['delete_news'] . '\''); } ?> <table><tr> <th>Edit</th> <th>Delete</th> <th>Title</th> <th>Date</th> <th>Author</th> </tr> <?php $return = mysql_query('SELECT * FROM news ORDER BY id DESC'); while ($data = mysql_fetch_array($return)) // We make a loop to list the items of news { ?> <tr> <td><?php echo '<a href="compose_news.php?edit_news=' . $data['id'] . '">'; ?>Edit</a></td> <td><?php echo '<a href="list_news.php?delete_news=' . $data['id'] . '">'; ?>Delete</a></td> <td><?php echo stripslashes($data['title']); ?></td> <td><?php echo date('m/d/Y', $data['timestamp']); ?></td> <td><?php echo $data['author'] ?></td> </tr> <?php } // End of loop listing news ?> </table> <center><a href="index.php">Home</a></center> </body> </html> compose_news <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" > <head> <title>Compose an item of news</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <style type="text/css"> h3, form { text-align:center; } </style> </head> <body> <h3><a href="list_news.php">Return to the news list</a></h3> <?php mysql_connect("localhost", "root", ""); mysql_select_db("news"); if (isset($_GET['edit_news'])) // If you ask to edit a piece of news { // We protect the variable "edit_news" to avoid an SQL fault $_GET['edit_news'] = mysql_real_escape_string(htmlspecialchars($_GET['edit_news'])); // We retrieve the pieces of information of the corresponding item of news $return = mysql_query('SELECT * FROM news WHERE id=\'' . $_GET['edit_news'] . '\''); $data = mysql_fetch_array($return); // We place the title and the content in simple variables $title = stripslashes($data['title']); $content = stripslashes($data['content']); $id_news = $data['id']; // This variable is used to remember that this is a modification } else // We are composing a fresh item of news { // The variables $title and $content are empty, since it is a fresh item of news $title = ''; $content = ''; $id_news = 0; // The value of the variable is 0, so we will remember that it is not a modification } ?> <form action="list_news.php" method="post"> <p>Title : <input type="text" size="30" name="title" value="<?php echo $title; ?>" /></p> <p> Content :<br /> <textarea name="content" cols="50" rows="10"> <?php echo $content; ?> </textarea><br /> <input type="hidden" name="id_news" value="<?php echo $id_news; ?>" /> <input type="hidden" name="author" value="<?php echo $_SESSION["logged"]; ?>" /> <input type="submit" value="Ok" /> </p> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/145998-data-replication-on-page-refresh/ Share on other sites More sharing options...
Cal Posted February 19, 2009 Share Posted February 19, 2009 It's probably because your resending the post data when you refresh, try going on google then going on list_news to see if this is the case. Quote Link to comment https://forums.phpfreaks.com/topic/145998-data-replication-on-page-refresh/#findComment-766485 Share on other sites More sharing options...
radian Posted February 19, 2009 Author Share Posted February 19, 2009 yeah, i submitted a new article and went on google like you said, went back to the list_news and now when i refresh it the article doesn't replicate. yay! so any way to stop resending of $_POST data? Quote Link to comment https://forums.phpfreaks.com/topic/145998-data-replication-on-page-refresh/#findComment-766494 Share on other sites More sharing options...
sasa Posted February 19, 2009 Share Posted February 19, 2009 reorganize your sript that 1st do all database staff (before any output) 2nd after update database use header() to reload page Quote Link to comment https://forums.phpfreaks.com/topic/145998-data-replication-on-page-refresh/#findComment-766501 Share on other sites More sharing options...
radian Posted February 19, 2009 Author Share Posted February 19, 2009 thanks...i'll get working on that UPDATE: right...it works now, all I did was put the insert SQL query in an insert.php script, before redirecting to list_news.php. Thanks Guys. Quote Link to comment https://forums.phpfreaks.com/topic/145998-data-replication-on-page-refresh/#findComment-766502 Share on other sites More sharing options...
Cal Posted February 19, 2009 Share Posted February 19, 2009 yeah, i submitted a new article and went on google like you said, went back to the list_news and now when i refresh it the article doesn't replicate. yay! so any way to stop resending of $_POST data? I'm not sure about stopping resending it, but i think most browsers warn the user before they attempt to refresh a page that will resend data. Quote Link to comment https://forums.phpfreaks.com/topic/145998-data-replication-on-page-refresh/#findComment-766509 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.