There is a form for adding data to the database

</head> <body> <center> <?php $host="localhost"; $user="lrsqkuxp_bd"; $pass="RISEZID123"; //установленный вами пароль $db_name="lrsqkuxp_bd"; $link=mysql_connect($host,$user,$pass); mysql_select_db($db_name,$link); mysql_query('SET NAMES utf8'); //Если переменная Name передана if (isset($_POST["Quiz"])) { //Вставляем данные, подставляя их в запрос $sql = mysql_query("INSERT INTO `products` (`Quiz`, `Answer`) VALUES ('".$_POST['Quiz']."','".$_POST['Answer']."')"); //Если вставка прошла успешно if ($sql) { echo "<p>Данные успешно добавлены! Спасибо, Вам, за участие.</p>"; } else { echo "<p>Произошла ошибка.</p>"; } } ?> <table> <form action="" method="post"> <tr> <td>Ваш вопрос:</td> <td><input type="text" name="Quiz"></td> </tr> <tr> <td>Ответ на вопрос:</td> <td><input type="text" name="Answer" ></td> </tr> <tr> <td colspan="2"><input type="submit" style="margin-left: 100px;" value="Добавить запись"></td> </tr> </form> </table> </body> 

They contribute to the database correctly, but then if you reload the page a couple of times, then the last one is recorded for some reason duplicated 1 to N times enter image description here

  • Well, when updating the page, the code is executed again. take measures not to plant doubles. for example, check that there are no records in the database with exactly the same data as you are going to record - Mike
  • 2
    Fight dubbing with an uncompromising method of setting restrictions in the database. You are given such a powerful instrument, and you live like in a stone age. Well, what does it cost to build a unique index? create unique index products_unique_idx on products (Quiz, Answer) Everything. Double takes no matter how hard you try. - Sergey

1 answer 1

Obviously, by reloading the page, you re-send the data to the server, as a result of which they are written to the database as many times as you have reloaded the page.

You can fight in the following ways:

1) you were asked in the comments to add a unique property to the field "unique" the second time in this column this value does not appear exactly;

2) check before adding whether there is already the same record in the database, and if so, do not write;

3) to store somewhere the information that you already received values ​​from this form (in a session or cookies, for example) ...

If you have this kind of test where some questions / answers are recorded and this field cannot be unique by default, then the simplest way is to do something from the 3rd option:

1) when the form is displayed, create $_SESSION['form_add'] = true; , and when adding (receiving data) to check whether this $ _SESSION ['form_add'], and if not, add the data and do unset($_SESSION['form_add']); ...

2) store it in cookies ...

 <?php session_start(); ?> <html> <head> </head> <body> <center> <?php $host="localhost"; $user="lrsqkuxp_bd"; $pass="RISEZID123"; //установленный вами пароль $db_name="lrsqkuxp_bd"; $link=mysql_connect($host,$user,$pass); mysql_select_db($db_name,$link); mysql_query('SET NAMES utf8'); //Если переменная Name передана if (isset($_POST['form_add']) && isset($_SESSION['form_add']) && ($_POST['form_add']==$_SESSION['form_add'])) { //Вставляем данные, подставляя их в запрос $sql = mysql_query("INSERT INTO `products` (`Quiz`, `Answer`) VALUES ('".$_POST['Quiz']."','".$_POST['Answer']."')"); //Если вставка прошла успешно if ($sql) { echo "<p>Данные успешно добавлены! Спасибо, Вам, за участие.</p>"; } else { echo "<p>Произошла ошибка.</p>"; } unset($_SESSION['form_add']); } ?> <?php $_SESSION['form_add'] = md5(time().'+'.rand(0,1000)); ?> <form action="" method="post"> <input type="hidden" name="form_add" value="<?php echo $_SESSION['form_add'];?>"> <table> <tr> <td>Ваш вопрос:</td> <td><input type="text" name="Quiz"></td> </tr> <tr> <td>Ответ на вопрос:</td> <td><input type="text" name="Answer" ></td> </tr> <tr> <td colspan="2"><input type="submit" style="margin-left: 100px;" value="Добавить запись"></td> </tr> </table> </form> </body> </html> 

Of course, the solution may be more elegant =)

  • In cookies and sessions, this can cause errors. A simple way to redirect after sending a form on the success page. - Naumov
  • @Naumov, yes that's right! I forgot about the redirect))) - Stanislav