Why when I refresh the page the comments themselves are added?

<div id="add_comment">
 <div class="add_comment">
 <form class="form" method="POST" action="/shablon.php?id=<?php echo $vidos['id']; ?> #add_comment">
 <?php 
 if(isset($_POST['do_post']) )
{
 $errors = array();

 if($_POST['name'] == ")
{
 $errors[] = 'enter a name!';
}
 if($_POST['text'] == ")
{
 $errors[] = 'enter text!';
}
 if( empty($errors))
{


 mysqli_query($connection,"INSERT INTO `comment` (`name`,`date`,`text_comment`,`the primary objective of BCC`) VALUES('".$_POST['name']."','".$_POST['text']."', NOW(), '".$vidos['id']."')");

 echo '<span style="color: green; font-weight:bold;margin-bottom:10px;display:block;"-->Commentry successfully added';


 } else
{
 echo $errors['0']; 
}
}
?>

 <div class="row">
 <input type="text" name="name" class="form__control" placeholder="Your name">
</div>
 <div class="text">
 <textarea class="textarea" cols="40" rows="3" name="text" placeholder></textarea>
</div>
 <div class="add_submit">
 <input type="submit" name="do_post" value="Add commentary" class="form__control">
</div>
</form>
</div>
 </div>
June 27th 19 at 15:22
4 answers
June 27th 19 at 15:24
Solution
After entry into the database put header ('Location: Адрес_сообщения_об_удачной_записи_в_базу');
The script will open another page and the update will not duplicate the entry.
mysqli_query($connection,"INSERT INTO `comment` (`name`,`date`,`text_comment`,`the primary objective of BCC`) VALUES('".$_POST['name']."','".$_POST['text']."', NOW(), '".$vidos['id']."')");
 header( 'Location: /shablon.php?id=<?php echo $vidos['id'];?> ');

So here? - humberto.Wuckert18 commented on June 27th 19 at 15:27
: header( 'Location: /shablon.php?id='.$vidos['id']); - hipolito_Trantow20 commented on June 27th 19 at 15:30
: Writes Cannot modify header information - headers already sent by Why? - humberto.Wuckert18 commented on June 27th 19 at 15:33
Because the output echo before the header. You need logic to alter. - hipolito_Trantow20 commented on June 27th 19 at 15:36
In your code it would be something like:

<?php 
 if(isset($_POST['do_post']) )
{
 $errors = array();

 if($_POST['name'] == ")
{
 $errors[] = 'enter a name!';
}
 if($_POST['text'] == ")
{
 $errors[] = 'enter text!';
}
 if( empty($errors))
{


 mysqli_query($connection,"INSERT INTO `comment` (`name`,`date`,`text_comment`,`the primary objective of BCC`) VALUES('".$_POST['name']."','".$_POST['text']."', NOW(), '".$vidos['id']."')");

header( 'Location: /shablon.php?id='.$vidos['id']); 


 } else
{
 echo $errors['0']; 
}
}
?>

<div id="add_comment">
 <div class="add_comment">
 <form class="form" method="POST" action="/shablon.php?id=<?php echo $vidos['id']; ?> #add_comment">

 <div class="row">
 <input type="text" name="name" class="form__control" placeholder="Your name">
</div>
 <div class="text">
 <textarea class="textarea" cols="40" rows="3" name="text" placeholder></textarea>
</div>
 <div class="add_submit">
 <input type="submit" name="do_post" value="Add commentary" class="form__control">
</div>
</form>
</div>
 </div>
- hipolito_Trantow20 commented on June 27th 19 at 15:39
Actually, correct would be not to make blotches in the code, and at first handle everything, and then make a conclusion.
<?php
$var = 'the result';

echo 
'<html > -->
Conclusion '.$var.' variable';


That will allow you to go to work with template engines, replacement of keys in an array or other options work. I.e. separate the logic and the conclusion. Otherwise, this style will be a problem for large volumes. - hipolito_Trantow20 commented on June 27th 19 at 15:42
error is gone, but still when updating added comments - humberto.Wuckert18 commented on June 27th 19 at 15:45
Try to fix header( 'Location: /shablon.php?id='.$vidos['id']);
on
header( 'Location: /shablon.php?id='.$vidos['id'].'&location=yes');

After adding a comment, changing the address in the bar
dot.com/shablon.php?id=ID&location=yes
? - hipolito_Trantow20 commented on June 27th 19 at 15:48
Copy my code again, perhaps you took the first option, I forgot about the Location in it - hipolito_Trantow20 commented on June 27th 19 at 15:51
all the problem is solved, thank you - humberto.Wuckert18 commented on June 27th 19 at 15:54
Not at all! Good luck!
But, think about the separation of logic from layout is the right way to use templates and MVC. - hipolito_Trantow20 commented on June 27th 19 at 15:57
June 27th 19 at 15:26
Solution
Usually smart people, after writing to the database rendered the page and do a redirect to another or same get but that such incidents did not occur.
And how to make in my case a redirect? - humberto.Wuckert18 commented on June 27th 19 at 15:29
: https://yandex.ru/search/?text=php%20redirect&clid... - hipolito_Trantow20 commented on June 27th 19 at 15:32
June 27th 19 at 15:28
Because your $_POST request does not disappear, and when you refresh the browser again it sends. A good idea is to separate the display from the save (well, in General terms, the reality is more complicated). In your case action in the form can be save.php?id=.. and after saving the save file to redirect the user back to shablon.php
Turns out you need another file save.php? or what? I understood everything you wrote except the last two lines - humberto.Wuckert18 commented on June 27th 19 at 15:31
: Yes, I meant it. All the work with the database stand in a separate file. The form is (for example)


And in the file save.php after saving do a redirect back to the first page
header( 'Location: /shablon.php?id=' . $_GET['id']);
- hipolito_Trantow20 commented on June 27th 19 at 15:34
: There's definitely $_GET ? - humberto.Wuckert18 commented on June 27th 19 at 15:37
: Don't want to go comments - hipolito_Trantow20 commented on June 27th 19 at 15:40
June 27th 19 at 15:30

Find more questions by tags PHPMySQL