Jump to content

[SOLVED] Why is my query carried out every time page refreshes???


Recommended Posts

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!!

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
}

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();
}

<?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>
";

?>

<?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>
";

?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.