how can I stop my php form resubmitting input after page refresh

123
August 14, 2018, at 7:50 PM
 <?php
$connection = mysqli_connect("host", "username", "password")
or die("Couldn't connect to the server"); //no issue 
$db = mysqli_select_db($connection, "dbname")
or die("Couldn't select database"); //no issue
$sql = "INSERT INTO vendor-tbl(company_name, industry, details, website, data_type, start_date) VALUES ('".$_POST['company_name']."','".$_POST['industry']."','".$_POST['details']."','".$_POST['website']."','".$_POST['data_type']."','".$_POST['start_date']."')";
// mysqli_query(($connection,$sql);
 if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
} 
$conn->close(); //closes the connection       
?>      
<form autocomplete="on" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post" role="form">
<label id="company_name">Company Name:</label><br/>
<input type="text" name="company_name" id="company_name" autocomplete='organization'><br/>
<label id="industry">Industry:</label><br/>
<input type="text" name="industry" id="industry" autocomplete='industry'><br/>
<label id="details">Details:</label><br/>
<input type="text" name="details" id="details" autocomplete='details'><br/>
<label id="website">Website:</label><br/>
<input type="text" name="website" id="website" autocomplete='website'><br/>
<label id="data_type">Data Type:</label><br/>
<input type="text" name="data_type" id="data_type" autocomplete='datatype'><br/>
<label id="start_date">Start Date:</label><br/>
<input type="text" name="start_date" id="start_date" autocomplete='startdate'><br/>
<button id="submitButton" value="submit" name="submit">Submit</button><br/>

</form>

When I refresh my form after just submitting data, it seems to add that data into phpmyadmin twice, and if I reload the form another time(blank form) it adds the data a third time etc. Is there any way of preventing this without using session variables? I'm very inexperienced with php but I have tried using session variables but I didn't seem to get it working.

Answer 1

What you want to do is a "post/redirect/get", for which you use an 303. This way you do a post, and then are 'forced' to do a get. When you f5, you reload that get and so not your post..

see:

  • https://en.wikipedia.org/wiki/Post/Redirect/Get
  • https://en.wikipedia.org/wiki/HTTP_303
Answer 2

Use isset() to check if the button was entered

if (isset($_POST['submit'])) {
     $connection = mysqli_connect("host", "username", "password")
or die("Couldn't connect to the server"); //no issue 
$db = mysqli_select_db($connection, "dbname")
or die("Couldn't select database"); //no issue
$sql = "INSERT INTO vendor-tbl(company_name, industry, details, website, data_type, 
start_date) VALUES ('".$_POST['company_name']."','".$_POST['industry']."','".$_POST['details']."','".$_POST['website']."','".$_POST['data_type']."','".$_POST['start_date']."')";
// mysqli_query(($connection,$sql);
 if ($conn->query($sql) === TRUE) {
     echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
  } 
}
Answer 3

Solution:

<?php
 if($_POST){
$connection = mysqli_connect("host", "username", "password")
or die("Couldn't connect to the server"); //no issue 
$db = mysqli_select_db($connection, "dbname")
or die("Couldn't select database"); //no issue
$sql = "INSERT INTO vendor-tbl(company_name, industry, details, website, data_type, start_date) VALUES ('".$_POST['company_name']."','".$_POST['industry']."','".$_POST['details']."','".$_POST['website']."','".$_POST['data_type']."','".$_POST['start_date']."')";
// mysqli_query(($connection,$sql);
 if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
} 
$conn->close(); //closes the connection       
Header("Location: " . $_SERVER['REQUEST_URI']);
 }
?>      
READ ALSO
How to do give expire time to specific session in laravel 5.6 ?

How to do give expire time to specific session in laravel 5.6 ?

I want to create session and it should be remove at 3 days later

106
WooCommerce AJAX Pagination no plugin

WooCommerce AJAX Pagination no plugin

I am trying to look into how I could create my own WooCommerce AJAX Pagination functionAll the tutorials I've found use a plugin and I would prefer not to

170
Prevent array_merge from merging an array if one of the specified argument is not an array

Prevent array_merge from merging an array if one of the specified argument is not an array

I want array_merge to merge only when the argument specified is an array else skip that argument

161