Question:
Data repeatedly inserted on php page?
Kim
2007-10-10 01:15:17 UTC
I have a php page that inserts data into mysql database.
Data is successfully inserted and the page works well.
But if I refresh the page after data is inserted, the same data as previously submitted is repeatedly inserted into database.
How can I fix this?
Four answers:
Dashes
2007-10-10 01:22:00 UTC
check if the data exists in the db already.



$result = mysql_query("SELECT * WHERE whatever = something");

$numrows = mysql_num_rows($result);

if ($numrows < 1) {

enter data into database

}
David D
2007-10-10 01:19:49 UTC
Use the POST-Redirect-GET pattern.



Have your data sent (using the form's POST method) to a PHP script which inserts it into the database and then using the header function sends a location header to redirect the user to another page (this might include something like the row id of the newly inserted data if that data needs to be displayed to the user).



This second page will be requested, automatically, but the user's browser using the GET method. This page can, if desired, get the data back out of the database if it needs to be displayed back to the user.



Refreshing this page will just GET the data again and won't rePOST it, so it won't get repeatedly added.



http://en.wikipedia.org/wiki/Post/Redirect/Get
Coosa
2007-10-10 01:49:23 UTC
The database design is questioned here; it seems you did not make primary keys and possibly also foreign keys for relations. Should it be an incremental ID which makes the PK, then make sure that you also have a unique attribute there such as the e-mail address or IC number, ...etc. and validate for redundancy before proceeding. Make sure you design well a normalized DB model. You can also use the InnoDb Engine instead of the MyISAM Engine since InnoDb supports referential integrity while MyISAM doesn't.
?
2016-10-22 01:28:58 UTC
$mysqli_db = new mysqli('localhost', 'root', 'root', 'club'); you may desire to have all those values perfect! you are going to be lacking values. do no longer use this even even though it may desire to be perfect $mysqli_db = new mysqli('hostname', 'port', 'root_table', 'username', 'bypass'); Does Hypertext Preprocessor require you to connection.OPEN? Your MySQL close seems to be in a incorrect place. upload some output for all your variables as a debug. (See the place your application gets to or breaks at.) if ($requery->execute()) { header('region:living house.Hypertext Preprocessor?identity=' . $requery->insert_username); }


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...