chelvan Posted June 16, 2007 Share Posted June 16, 2007 My problem is .. everytime when i click refresh on my broswer it adds another item to my databasewhat can i do to make it only adds the data to database by only clicking submiti think the problem i have is .. how do i check using an php If statement to see if an submit buttom is clicked also refresh will not count as a submit .. i know the post data will keep posting if refresh is clicked .. but how can I avoid that?? <html><body><form action="welcome.php" method="post"><table border='1'> <tr> <td>EnglishInfo: </td> <td><input type="text" name="EnglishInfo" /></td> </tr> <tr> <td>ChineseInfo: </td> <td><input type="text" name="ChineseInfo" /></td> </tr> <tr> <td>Links: </td> <td><input type="text" name="Links" /> </td> </tr></table><input type="submit" name="submit" value="clicked" /></form><?php$con = mysql_connect("localhost","******","********");if (!$con) { die('Could not connect: ' . mysql_error() ); }else{ mysql_select_db("chelvan_videos", $con);if(isset($_POST[submit])){ $result2 = mysql_query("SELECT MAX(FieldNumber) FROM videoscontrol"); $row = mysql_fetch_array($result2); $field = $row['MAX(FieldNumber)'] + 1; $sql = "INSERT INTO videoscontrol(FieldNumber, EnglishInfo, ChineseInfo, Links) VALUES('$field', '$_POST[EnglishInfo]','$_POST[ChineseInfo]','$_POST[Links]')"; mysql_query($sql);}$result = mysql_query("SELECT * FROM videoscontrol ORDER BY FieldNumber");echo "<table border='1'><tr><th>FieldNumber</th><th>EnglishInfo</th><th>ChineseInfo</th><th>Links</th></tr>";while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['FieldNumber'] . "</td>"; echo "<td>" . $row['EnglishInfo'] . "</td>"; echo "<td>" . $row['ChineseInfo'] . "</td>"; echo "<td>" . $row['Links'] . "</td>"; echo "</tr>"; }echo "</table>";mysql_close($con);}?></body></html> Link to comment Share on other sites More sharing options...
vijay Posted June 16, 2007 Share Posted June 16, 2007 Hi..add this line @ the end of code after closing connection..use redirect funda whenever you face this type of probs..so page will be redirected after our db insertion process.. so it won't add if you refresh the page due to redirection...Regards,Vijay------------------------------------------------------------------------------------- ............ echo "<td>" . $row['EnglishInfo'] . "</td>"; echo "<td>" . $row['ChineseInfo'] . "</td>"; echo "<td>" . $row['Links'] . "</td>"; echo "</tr>"; }echo "</table>";mysql_close($con);header("location:welcome.php"); // this 2 lineexit; // this line also..} Link to comment Share on other sites More sharing options...
chelvan Posted June 16, 2007 Author Share Posted June 16, 2007 Hi..add this line @ the end of code after closing connection..use redirect funda whenever you face this type of probs..so page will be redirected after our db insertion process.. so it won't add if you refresh the page due to redirection...Regards,Vijay------------------------------------------------------------------------------------- ............ echo "<td>" . $row['EnglishInfo'] . "</td>"; echo "<td>" . $row['ChineseInfo'] . "</td>"; echo "<td>" . $row['Links'] . "</td>"; echo "</tr>"; }echo "</table>";mysql_close($con);header("location:welcome.php"); // this 2 lineexit; // this line also..} i added the 2 lines it still adds another line of data in the database when i click refresh Link to comment Share on other sites More sharing options...
Lulzim Posted June 16, 2007 Share Posted June 16, 2007 echo "<td>" . $row['EnglishInfo'] . "</td>"; echo "<td>" . $row['ChineseInfo'] . "</td>"; echo "<td>" . $row['Links'] . "</td>"; echo "</tr>"; }echo "</table>";mysql_close($con);header("location:welcome.php"); // this 2 lineexit; // this line also..}that wouldn't work, you cannot modify headers after all those echo-s Link to comment Share on other sites More sharing options...
chelvan Posted June 16, 2007 Author Share Posted June 16, 2007 that wouldn't work, you cannot modify headers after all those echo-s so what can i do? Link to comment Share on other sites More sharing options...
Anders Moen Posted June 16, 2007 Share Posted June 16, 2007 Eh..I think he can. If he puts ob_start(); on top of the document (under session_start(); if you have that) Link to comment Share on other sites More sharing options...
chelvan Posted June 16, 2007 Author Share Posted June 16, 2007 Eh..I think he can. If he puts ob_start(); on top of the document (under session_start(); if you have that)OK I added <?php session_start(); ob_start(); ?> <html> <body> ....... ....... // under my insert if() i added the header("location: welcome.php"); and it worked .. thanks Link to comment Share on other sites More sharing options...
Anders Moen Posted June 17, 2007 Share Posted June 17, 2007 You put the header() in the document, and not over the doctype, right? Just want to know how you did it...sometimes I need that myself you see Link to comment Share on other sites More sharing options...
Mr_CHISOL Posted June 17, 2007 Share Posted June 17, 2007 with output buffering you can put header() "anywhere" (almost, not after you flushed...) you want..Without OB, you must put it before any output Link to comment Share on other sites More sharing options...
Anders Moen Posted June 17, 2007 Share Posted June 17, 2007 Okay. Sounds good Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.