shan Posted January 6, 2015 Share Posted January 6, 2015 (edited) im trying to insert a value in to mysql using an experiment everything works fine after hitting the insert button but the problem arises wen i refresh the page as a empty value gets inserted in to the mysql table wen i refresh it.(pardon my english) this is the index page where i fetch an result from mysql table: <div id="container"> <div class="Container-left"> </div> <div class="container-right"> <form action="1.2.php" method="post"> <textarea height="190px" width="190px" name="text" placeholder="share wats on your mind"></textarea> <br> <input type="submit" name="button" value="Type Here" /> </form> <hr> <?php include '1.2.php'; while ($fetch = mysqli_fetch_array($query1)) { echo 'comment:'.$fetch['post'].'<br/>'; echo '<small>date:'.$fetch['date'].'</small><br/><hr>'; } ?> </div> </div> this is the page where my second code rests: <?php include 'config.php'; error_reporting(E_ALL ^ E_NOTICE); $h= htmlspecialchars($_POST['text']); $p= mysqli_real_escape_string($conn,$h); $sql = "INSERT INTO article (post) VALUES ('$h') "; $sql1="SELECT * FROM article"; $query=mysqli_query($conn, $sql); $query1= mysqli_query($conn, $sql1); if ($_POST['button']){ echo 'inserted'; } else { echo 'not inserted'. mysqli_error($conn); } what should i do to stop value getting inserted in to my table while refreshing the page. pl help and thanks in advance. Edited January 6, 2015 by shan Quote Link to comment https://forums.phpfreaks.com/topic/293706-value-gets-inserted-after-refeshing-the-page-need-help/ Share on other sites More sharing options...
hansford Posted January 6, 2015 Share Posted January 6, 2015 (edited) Every time you refresh the page, it resubmits the form. Have the php page go back to your html page. Edited January 6, 2015 by hansford Quote Link to comment https://forums.phpfreaks.com/topic/293706-value-gets-inserted-after-refeshing-the-page-need-help/#findComment-1501906 Share on other sites More sharing options...
shan Posted January 6, 2015 Author Share Posted January 6, 2015 i did use the ('header: index.php') instead of "echo 'inserted';" in the page. and that is when i noticed that my mysql table is getting populated when it refreshes.so, changed it to inserted instead sir Quote Link to comment https://forums.phpfreaks.com/topic/293706-value-gets-inserted-after-refeshing-the-page-need-help/#findComment-1501907 Share on other sites More sharing options...
hansford Posted January 6, 2015 Share Posted January 6, 2015 (edited) comment out the actual code that inserts anything into your db - echo the query instead so you can see what's happening. You must be hitting the db somewhere. Get rid of the location header until you can find the problem. Use an include for that page instead. Edited January 6, 2015 by hansford Quote Link to comment https://forums.phpfreaks.com/topic/293706-value-gets-inserted-after-refeshing-the-page-need-help/#findComment-1501908 Share on other sites More sharing options...
hansford Posted January 6, 2015 Share Posted January 6, 2015 <form action="1.2.php" method="post"> This form goes to the page set in the action. 1.2.php needs to do its work and then redirect back to this form - no refresh on a form page as it just resubmits the form and thus the blank db insert. Quote Link to comment https://forums.phpfreaks.com/topic/293706-value-gets-inserted-after-refeshing-the-page-need-help/#findComment-1501909 Share on other sites More sharing options...
shan Posted January 6, 2015 Author Share Posted January 6, 2015 thanks bro! its working well now 1 Quote Link to comment https://forums.phpfreaks.com/topic/293706-value-gets-inserted-after-refeshing-the-page-need-help/#findComment-1501912 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.