believeinsharing Posted June 1, 2012 Share Posted June 1, 2012 Hi all, I am trying to add data from 'form' to database using 'Insert statement' I am using 'POST' method for form. After hitting Submit button its add one record to database but as i refresh the page its adding same row again. I use 'GET' instead of 'POST' but same prob. my code is: <html> <head> <title> Contact Us </title> <link rel="stylesheet" type="text/css" href="style/myStyle.css"/> <script language="JavaScript" src="style/formValidation.js" type="text/javascript"></script> <script type="text/javascript"> </script> </head> <body> <div id="pageSize"> <div id="contactusHeading"> Contact Us </div> <div id="contContainer"> <div id="contactusImage"> </div> <div id="contactForm"> <form action="contactUs.php" name="myfrm" method="get" onsubmit="return validatefrm()"> <table id="forForm" cellpadding="2"> <tr> <td>Name:</td> <td><input type="text" name="name"></input></td> </tr> <tr> <td>Company Name:</td> <td><input type="text" name="companyName"></input></td> </tr> <tr> <td>E-mail:</td> <td><input type="text" name="email"></input></td> </tr> <tr> <td>About:</td> <td><input type="text" name="about"></input></td> </tr> <tr> <td>Message:</td> <td><input type="text" name="message"></input></td> </tr> <tr> <td colspan="2" align="center"> <input type="submit" value="Submit" name="butSubmit"></input></td> </tr> </table> </form> </div> </div> <?php $name=$_GET['name']; $compantName=$_GET['companyName']; $email=$_GET['email']; $about=$_GET['about']; $message=$_GET['message']; echo "<script type=text/javascript>"; echo "document.myfrm.reset()"; echo "</script>"; $hostname="localhost"; $db_user="root"; $db_password="admin"; $database="myproject"; $db_table="contactus"; $con=mysql_connect($localhost,$db_user,$db_password); if(!con) { die('Error in connection:'.mysql_error()); } mysql_select_db($database,$con); if($_GET['butSubmit']==true) { $sql="INSERT INTO $db_table (Name, Company_name, Email, About, Message) VALUES ('$name', '$compantName', '$email', '$about', '$message')"; if(!mysql_query($sql,$con)) { die('Error:' .mysql_error()); } echo "<div id=newRecord> New record added </div><br/>"; $result=mysql_query("SELECT * FROM $db_table ORDER BY id DESC "); unset($_GET['name']); unset($_GET['companyName']); unset($_GET['email']); unset($_GET['about']); unset($_GET['message']); } $result=mysql_query("SELECT * FROM $db_table ORDER BY id DESC "); $rowcount=mysql_num_rows($result); $i=0; echo "<br/>"; echo "<table id=contTable> <tr id=tableHeading> <th>Name</th> <th>Company Name</th> <th>Email</th> <th>About</th> <th>Message</th> </tr>"; while($row=mysql_fetch_array($result)) { if($i & 1) { echo "<tr id=oddRow>"; } else { echo "<tr id=evenRow>"; } $i++; echo "<td>" . $row['Name']. "</td>"; echo "<td>" . $row['Company_name']. "</td>"; echo "<td>" . $row['Email'] . "</td>"; echo "<td>" . $row['About'] . "</td>"; echo "<td>" .$row['Message'] . "</td>"; echo "</tr>"; } echo "</table>"; echo "<br/>"; if($result){ header("Location:contact.php"); }else { echo "Not Successful"; } mysql_close($con); ?> </div> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/263490-how-to-avoid-data-duplication-after-page-refresh/ Share on other sites More sharing options...
jcbones Posted June 1, 2012 Share Posted June 1, 2012 Couple of things you could do. 1. redirect the page back to itself after processing (without the GET params of course). 2. Hit the database prior to processing, to see if that exact row is already there, (query the database with every single bit of info you are fixing to put there, returns a row it is there). Either/or, and I'm sure there are other fine examples out there, these are just the most common that I see. Quote Link to comment https://forums.phpfreaks.com/topic/263490-how-to-avoid-data-duplication-after-page-refresh/#findComment-1350385 Share on other sites More sharing options...
RobertP Posted June 1, 2012 Share Posted June 1, 2012 offtopic: please use the tag next Quote Link to comment https://forums.phpfreaks.com/topic/263490-how-to-avoid-data-duplication-after-page-refresh/#findComment-1350395 Share on other sites More sharing options...
Psycho Posted June 1, 2012 Share Posted June 1, 2012 On the processing page that receives the form submission you simply need to do a header redirect to a confirmation page or even back to the same page. That will "wipe out" the form data. So, if a user refreshes the page they will simply see the confirmation page and no processing takes place. //Process the form data //Processing logic goes here //After processing logic completes redirect to confirmation page header("Location: confirmation_page.php"); Quote Link to comment https://forums.phpfreaks.com/topic/263490-how-to-avoid-data-duplication-after-page-refresh/#findComment-1350418 Share on other sites More sharing options...
believeinsharing Posted June 2, 2012 Author Share Posted June 2, 2012 thx all for ur help header(location:".....") solved my prob Quote Link to comment https://forums.phpfreaks.com/topic/263490-how-to-avoid-data-duplication-after-page-refresh/#findComment-1350456 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.