I am writing my code procedurally and I am experiencing an odd result.
After I add a record to my website, if I click refresh, or CRTL and refresh, then another instance of the same record will be added.
I have sourced this out to a variable which I am setting in a form, but I’m not sure why it is not clearing on a refresh. As part of my debugging, I tried to clear the variable at multiple stages in the code, but nothing is working – when I refresh the page, the variable is being reset to “y”.
Here are the elements of my code.
In my variable declaration section at the top of my page I have:
$switch_new = $_POST['switch_new'];
Then after the variables section, I have a section for checking if any conditions are met and if so, then a query is run:
if ($switch_new == "y") {
$sql = "INSERT INTO tableMinis (mini_name, mini_sets_id) values (:miniName, :miniSetsId);";
$stmt = $pdo->prepare($sql);
$stmt->execute(['miniName' => addslashes($add_newName), 'miniSetsId' => $setSelection]);
$stmt = null;
$_POST["switch_new"] = null;
$switch_new = null;
}
And here is the form that starts it all off (note: I have omitted the input entries on the form for this example):
echo '<form enctype="multipart/form-data" name="add_newData" id="add_newData" action="main-minis.php" method="post">';
echo '<input type="hidden" name="switch_new" value="y">';
echo '<input type="submit" value="Add Mini">';
echo '</form>';
Can someone please offer me some suggestions on how I can prevent this from happening. Thank you.