Jump to content

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


ashrobbins87

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

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.