Saturday 31 December 2016

html - Stop PHP from making repeat querys on refresh

I'm making a web application, and I'm having trouble figuring out how to stop the PHP from making a query to my database every time the page is reloaded.


Basically when the form is submitted I want the PHP to make a query to the database adding the variables, however, because it has saved those values in the $_POST, it's making a query every time I refresh with those same values.


I want it to make the query, then perhaps unset the $_POST values or something so that it doesn't satisfy the if condition, and in turn stops it from querying the database with repeat values when the submit button hasn't updated them with new values.


Sorry if that's convoluted I'm trying to explain my problem the best I can.


Here is the PHP


//Require login gile -- Fatal error if not found
require_once('login.php');
require_once('app.html');
//Connect w/ MySQL database [login variables]
$con = new mysqli($hn, $un, $pw, $db);
//If connection error -> kill application and display error
if ($con->connect_error) die('Connect Error (' . $con->connect_errno . ') '. $con->connect_error);
//If both goal and difficulty are set
if (!empty($_POST['goal']) && !empty($_POST['difficulty'])) {
$goal = get_post($con, 'goal');
$difficulty = get_post($con, 'difficulty');
$query = "INSERT INTO items VALUES" . "('$goal', 1, '$difficulty', NULL)";
$result = $con->query($query);
if(!$result) echo "INSERT failed: $query
" . $con->error . "

";
}
unset($_POST['goal']);
unset($_POST['difficulty']);
//close connection
$con->close();
function get_post($conn, $var) {
return $conn->real_escape_string($_POST[$var]);
}
?>

html





Testing






















No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...