I make you a semplified example of my problem and then try to explain it:
<?php
$connect = mysqli_connect($link, $user, $pw, $db);
$informations = mysqli_fetch_array(mysqli_query($connect, "SELECT * FROM table WHERE id = '$id' "));
if($_POST['submit']){
if($informations['show'] == 'true'){
if($informations['changes'] <= '100'){
$value = mysqli_real_escape_string($connect, $_POST['value']);
mysqli_query($connect, "UPDATE table SET changes=changes+1, value='$value' WHERE id = '$id'");
}else{
echo "You made too much changes.";
}
}else{
echo "You cannot change this.";
}
}
if($informations['show'] == 'true'){
echo "<form action='' method='post'>";
echo "<input type='text' name='value' /><input type='submit' name='submit' value='Update' />";
echo "</form>";
}
echo "<br/>You currently have made ".$informations['changes']." changes";
?>
So, looking at this code I can see this:
I enter my page and have a text input near a submit button
Under this i have written "You currently have made 0 changes."
If I press the submit button my page refresh and I can see:
A text input near a submit button
Under this i have written "You currently have made 0 changes."
If i refresh the page this time I can see:
A text input near a submit button
Under this i have writte "You currently have made 1 changes."
So what i wanna ask you is:
Is there any way to perform the update and directly see the changes on the page after the normal form refresh?
I cannot put the "update query" above the "select query" because before make the update i need to check the values of the user....
I know I may use AJAX but it's seems weird that php cannot do that without AJAX, and that takes more time...
Any help will be appreciated, thank you!
The reason is not linked to using Ajax or not. You are increasing the changes value in the database but not in your PHP variable.
So just before this line:
mysqli_query($connect, "UPDATE table SET changes=changes+1, value='$value' WHERE id = '$id'");
add the following:
$informations['changes']++;
Remarks
Although you have escaped quotes in $value
to avoid SQL injection, it is better to prepare your statements instead of concatenating values into it.
The field changes is numerical so this comparison is wrong:
if($informations['changes'] <= '100'){
It should be without quotes:
if($informations['changes'] <= 100){
Change the message "too much changes" to "too many changes", because changes is a plural.