ashrobbins87 Posted March 18, 2009 Share Posted March 18, 2009 I have a page for adding new users whereby you can fill in the form with details and send it off to the DB. But I only want this to happen when I click the button. Instead, every time I refresh the page to test other features on the page the query is carried out and a new user is added to the database with empty fields. Has anyone come across this or know of a solution? Thank you!! Quote Link to comment https://forums.phpfreaks.com/topic/149978-solved-why-is-my-query-carried-out-every-time-page-refreshes/ Share on other sites More sharing options...
waynew Posted March 18, 2009 Share Posted March 18, 2009 Are you sure that the browser is not submitting the same post browser? Firstly you should make sure that your data isn't empty data. For example, if I wanted to make sure that the $_POST variable username wasn't empty, I'd do something like: if(strlen(trim($_POST['username'])) > 0){ //data is ok } Quote Link to comment https://forums.phpfreaks.com/topic/149978-solved-why-is-my-query-carried-out-every-time-page-refreshes/#findComment-787645 Share on other sites More sharing options...
JonnoTheDev Posted March 18, 2009 Share Posted March 18, 2009 Your code is incorrect. You need to detect the buttons value before inserting anything. i.e. if(isset($_POST['submit']) && $_POST['submit'] == 'create') { // add a new record to the database // reload page header("Location:index.php"); exit(); } Quote Link to comment https://forums.phpfreaks.com/topic/149978-solved-why-is-my-query-carried-out-every-time-page-refreshes/#findComment-787646 Share on other sites More sharing options...
ashrobbins87 Posted March 18, 2009 Author Share Posted March 18, 2009 Thanks for your help guys. I used Neil Johnson's method and it works now! That will be a handy tip for the future. Quote Link to comment https://forums.phpfreaks.com/topic/149978-solved-why-is-my-query-carried-out-every-time-page-refreshes/#findComment-787654 Share on other sites More sharing options...
ashrobbins87 Posted March 18, 2009 Author Share Posted March 18, 2009 Turns out its not solved!! The page will not run the query on refresh any more, but it wont run it when pressing the button either!! ??? Quote Link to comment https://forums.phpfreaks.com/topic/149978-solved-why-is-my-query-carried-out-every-time-page-refreshes/#findComment-787657 Share on other sites More sharing options...
waynew Posted March 18, 2009 Share Posted March 18, 2009 Show us the html for your form. Quote Link to comment https://forums.phpfreaks.com/topic/149978-solved-why-is-my-query-carried-out-every-time-page-refreshes/#findComment-787659 Share on other sites More sharing options...
ashrobbins87 Posted March 18, 2009 Author Share Posted March 18, 2009 <?php if(isset($_POST['submit']) && $_POST['submit'] == 'create') { // add a new record to the database //Adding a new user $mysqli = mysqli_connect("localhost","root","pass","opp_group"); $f = $_POST['firstname']; $s = $_POST['surname']; $u = $_POST['username']; $p = $_POST['password']; $addUser_sql = "INSERT INTO cms_users (firstname, surname, username, password) VALUES ('$f', '$s','$u',MD5('$p'))"; $addUser_res = mysqli_query($mysqli, $addUser_sql) or die(mysqli_error($mysqli)); mysqli_close($mysqli); // reload page header("Location:index.php"); exit(); } echo "<form method=\"POST\" action=\"".$_SERVER["PHP_SELF"]."\"> <p>First Name: <input type=\"text\" name=\"firstname\" size=\"20\" maxlength=\"25\"/></p> <p>Surname: <input type=\"text\" name=\"surname\" size=\"20\" maxlength=\"25\" /></p> <p>Username: <input type=\"text\" name=\"username\" size=\"15\"maxlength=\"10\"/> Password: <input type=\"password\" name=\"password\" size=\"15\" maxlength=\"10\"/></p> <p><h4>Username is first letter of firstname followed by surname, max 8 characters. e.g. jsmith. <br/>Password must be exactly 8 characters long</h4></p> <input type=\"submit\" name=\"submit\" value=\"Submit\" /> <hr class=\"short\" align=\"left\"/> </form> "; ?> Quote Link to comment https://forums.phpfreaks.com/topic/149978-solved-why-is-my-query-carried-out-every-time-page-refreshes/#findComment-787665 Share on other sites More sharing options...
waynew Posted March 18, 2009 Share Posted March 18, 2009 <?php if(isset($_POST['submit']) && $_POST['submit'] == 'Submit') { // add a new record to the database //Adding a new user $mysqli = mysqli_connect("localhost","root","pass","opp_group"); $f = $_POST['firstname']; $s = $_POST['surname']; $u = $_POST['username']; $p = $_POST['password']; $addUser_sql = "INSERT INTO cms_users (firstname, surname, username, password) VALUES ('$f', '$s','$u',MD5('$p'))"; $addUser_res = mysqli_query($mysqli, $addUser_sql) or die(mysqli_error($mysqli)); mysqli_close($mysqli); // reload page header("Location:index.php"); exit(); } echo "<form method=\"POST\" action=\"".$_SERVER["PHP_SELF"]."\"> <p>First Name: <input type=\"text\" name=\"firstname\" size=\"20\" maxlength=\"25\"/></p> <p>Surname: <input type=\"text\" name=\"surname\" size=\"20\" maxlength=\"25\" /></p> <p>Username: <input type=\"text\" name=\"username\" size=\"15\"maxlength=\"10\"/> Password: <input type=\"password\" name=\"password\" size=\"15\" maxlength=\"10\"/></p> <p><h4>Username is first letter of firstname followed by surname, max 8 characters. e.g. jsmith. <br/>Password must be exactly 8 characters long</h4></p> <input type=\"submit\" name=\"submit\" value=\"Submit\" /> <hr class=\"short\" align=\"left\"/> </form> "; ?> Quote Link to comment https://forums.phpfreaks.com/topic/149978-solved-why-is-my-query-carried-out-every-time-page-refreshes/#findComment-787668 Share on other sites More sharing options...
ashrobbins87 Posted March 18, 2009 Author Share Posted March 18, 2009 Did you really just change one word?!?! Quote Link to comment https://forums.phpfreaks.com/topic/149978-solved-why-is-my-query-carried-out-every-time-page-refreshes/#findComment-787672 Share on other sites More sharing options...
waynew Posted March 18, 2009 Share Posted March 18, 2009 Yea lol Quote Link to comment https://forums.phpfreaks.com/topic/149978-solved-why-is-my-query-carried-out-every-time-page-refreshes/#findComment-787675 Share on other sites More sharing options...
ashrobbins87 Posted March 18, 2009 Author Share Posted March 18, 2009 Thank you mate Quote Link to comment https://forums.phpfreaks.com/topic/149978-solved-why-is-my-query-carried-out-every-time-page-refreshes/#findComment-787684 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.