kleb Posted January 30, 2012 Share Posted January 30, 2012 please help me with this.since i started posting this form to its self it has been inserting values twice into the database when the user clicks submit button and i also noticed that i have to refresh the page before inserted comments can be showed.this is my code: <?php include"header.php"; $sql="SELECT post_content,post_by FROM post WHERE topicsID='$tpid'"; $result=mysql_query($sql)or die(mysql_error()); while($row=mysql_fetch_array($result)) { echo"<strong>{$row['post_by']}</strong>: {$row['post_content']}"."</br>"; } ?></td> </tr> </table> <?php include"header.php"; if(isset($_POST['submit'])) { $comment=mysql_real_escape_string(trim($_POST['comment'])); $name=mysql_real_escape_string(trim($_POST['name'])); $hidden=$_POST['id']; if($comment!=='' && $name!=='') { $topicid=$_GET['id']; $ins="INSERT INTO post(post_content,post_by,post_id)VALUES('$comment','$name','$topicid')"; mysql_query($ins) or die(mysql_error()); } else { echo"you cannot post an empty field"; } } ?> <h3>Post your comments here</h3> <form action=''method='post'> <textarea name="comment" id="content" style="width:400px;height:50px;background-color:#D0F18F;color:#000000;font:15px/20px cursive;scrollbar-base-color:#638E0D;"></textarea> <br /> Name:<input type="text"name="name"/> <input class="button" type="submit"name="submit"value="submit" /> </p> </form> Quote Link to comment https://forums.phpfreaks.com/topic/256033-double-insert/ Share on other sites More sharing options...
kickstart Posted January 30, 2012 Share Posted January 30, 2012 Hi You are displaying data before you attempt the insert, hence needing to do the refresh to display it. And a refresh probably will resubmit the form data so inserting another row All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/256033-double-insert/#findComment-1312526 Share on other sites More sharing options...
ocpaul20 Posted January 30, 2012 Share Posted January 30, 2012 when submitting a form to the same program, you normally place the test if POST[submit] at the top and then if not, display the html page. Thats what I do anyway. You can always var_dump the POST variables at the top to see what they are. Quote Link to comment https://forums.phpfreaks.com/topic/256033-double-insert/#findComment-1312530 Share on other sites More sharing options...
kleb Posted January 30, 2012 Author Share Posted January 30, 2012 thanks guys i think i will just that now Quote Link to comment https://forums.phpfreaks.com/topic/256033-double-insert/#findComment-1312539 Share on other sites More sharing options...
kleb Posted January 30, 2012 Author Share Posted January 30, 2012 i just changed it now.i put the insert before the select but i noticed that if i donot insert any data the ones already in the database table will not be showed Quote Link to comment https://forums.phpfreaks.com/topic/256033-double-insert/#findComment-1312582 Share on other sites More sharing options...
kleb Posted January 30, 2012 Author Share Posted January 30, 2012 wow!! dont border i have fixed it.thanks everyone for your concern Quote Link to comment https://forums.phpfreaks.com/topic/256033-double-insert/#findComment-1312594 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.