Hello. Data from the form is not sent to the users table. I use local Apache server and MySQL database (XAMPP). The table structure: user_id (AUTO_INCREMENT); login; password; name; email;

File register_html.php :

<!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/html"> <head> <meta charset="UTF-8"> <title>Страница регистрации</title> </head> <body> Заполните форму: <form method="post" action=""> Логин: <input type="text" name="login" required><br> E-Mail:<input type="email" name="email" required><br> Пароль:<input type="password" name="password" required><br> Имя:<input type="text" name="name" required><br> <input type="submit" name="enter" value="Регистрация"><br> <input type="reset" value="Очистить"><br> </form> <?php ini_set('display_errors',1); error_reporting(E_ALL); mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); include_once "../connect.php"; if (isset($_POST['enter'])) { $login = $_POST['login']; $email = $_POST['email']; $password = $_POST['password']; $name = $_POST['name']; $result = mysqli_query($link,"INSERT INTO `users` (`login`, `email`, `password`, `name`) VALUES ('".$login."', '".$email."', '".$password."', '".$name."')"); if($result){ echo 'Регистрация прошла успешно'; } else{ echo 'Ошибка'; // выводите ошибку } } echo mysqli_error($link); ?> 

File connect.php

 <?php ini_set('display_errors',1); error_reporting(E_ALL); $link = mysqli_connect("localhost", "root", "", "mydb") or die("Unable to select database"); $db = mysqli_select_db($link,"mydb"); mysqli_query($link," SET NAMES 'utf8' "); if ($link->connect_errno) { echo "Не удалось подключиться к MySQL: (" . $link->connect_errno . ") " . $link->connect_error; } echo $link->host_info . "\n"; if (!$link || !$db){ exit(mysqli_error()); } ?> 

When entering data into a form and clicking the registration button, the form is simply updated to empty, no errors appear, just like the 'Registration was successful' registered in the script. If you simply execute the text request to the database without a form, everything is written into the table. Such a feeling that the problem is in the script of the form handler. Tell me please what can be the reason? Thanks in advance.

  • INSERT INTO `users` (` login`, `email`,` password`, `name`) VALUES ('login22', 'mail@mail.ru', '123qwe', 'myname'); - try to execute this request in phpMyAdmin, will it give an error? - sanix Sep.
  • No, everything is fine, everything is added to the table in this way. By way of a php file, via mysqli_query ($ link, "INSERT INTO users ( login , email , password , name ) VALUES ('losdfdsf', 'msdfsdl@mail.ru', '123sdfqwe', 'mynasdfme')") with connection To the database, too, all data is recorded. - Dust0 2:51 pm

2 answers 2

In the action attribute, specify the name of the script that is to process the form data.

 <form method="post" action="register_html.php"> 

And replace the condition

 if (isset($_POST['enter'])) { 

on

 if (isset($_POST['login'])) { 

because the enter parameter from the form is not passed

  • He indicated, but unfortunately the problem remained. - Dust0
  • @ Dust0 replied to the answer - Anton Shchyrov
  • changed to if (isset ($ _ POST ['login'])) {, but it still does not help: (It seems that the post method is disabled altogether. Maybe something is wrong in php.ini? - Dust0

You have mistakes, and not one. So:

HTML

 <form method="post" action=""> Логин: <input type="text" name="login" required><br> E-Mail:<input type="email" name="email" required><br> Пароль:<input type="password" name="password" required><br> Имя:<input type="text" name="name" required><br> <input type="submit" name="enter" value="Регистрация"><br> <input type="reset" value="Очистить"><br> </form> 

file where you connect connect.php

 if ($_SERVER['REQUEST_METHOD']=='POST' && isset($_POST['enter'])): $login = $_POST['login'];//Надо чистить/проверять данные $email = $_POST['email'];//Надо чистить/проверять данные $password = $_POST['password'];//Надо чистить/проверять данные $name = $_POST['name'];//Надо чистить/проверять данные $link = mysqli_connect("localhost", "root", "", "mydb") or die(mysqli_error($link)); $query = "INSERT INTO `users` (`login`, `email`, `password`, `name`) VALUES ('$login', '$email', '$password', '$name') " $result = mysqli_query($link, $query); if(mysqli_affected_rows($result)>0) echo "Регистрация прошла успешно!"; esle echo "Что-то случилось, а вы не зарегистрировались"; endif; 

Errors:

  • $db = mysqli_select_db($link,"mydb"); You have already specified the name of the database in the $link connection.
  • In your case, the use of procedural and object-oriented style together is unacceptable.
  • You don't need the connect.php file at all, because there is only one connection line. And to transfer this connection simply using include_once will fail.
  • All the conclusions of errors and the termination of the script, such as: echo mysqli_error($link); , if (!$link || !$db){ exit(mysqli_error());} is also not necessary. In any case, you will give an error, and the sweeping will stop.
  • Sending bare data straight to the database is a very bad start! Always need to process and clear data.
  • This is the construction of '".$login."' When creating a request is redundant. You can simply specify single quotes ( see my example ).

This is basic. Can continue for a very long time.

UPDATE 1

Best of all, make one input file index.php , and through it to direct all actions. The controller.php file accepts these actions, it must parse what data to transfer to the last file that works with the database, this is model.php . It’s very undesirable to combine HTML and PHP code in one file (except for outputting information).

It seems to me that you learn from the video tutorials of Evgeny Popov :) If not, then it’s for beginners the most. He knows how to explain, but the quality of writing code leaves much to be desired.

UPDATE 2 #

  1. Create on the server a new folder for the test project, name it, say, test-project .
  2. After you have created the project folder, restart the server.
  3. In this folder, create two files index.php and action.php , below I will write their contents.
  4. After creating the files, open this project in the browser (I don't know how the path to the local site is written in xampp)

index.php file

 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Index File for test</title> </head> <body> <form action="" method="post"> <input type="text" name="firstInput" placeholder="first" value=""> <input type="text" name="secondInput" placeholder="second" value=""> <input type="text" name="thirdInput" placeholder="third" value=""> <input type="submit" name="actionButton" value="RUN ACTION"> </form> </body> </html> <?php if(include_once 'action.php') echo 'yes'; ?> 

action.php file

 <?php if ($_SERVER['REQUEST_METHOD']=='POST' && isset($_POST['actionButton'])): echo "<pre>"; print_r($_POST); echo "</pre>"; endif; 

All these parameters in the php.ini that you asked for, do not need to change / delete and everything else. The server is already configured to work. Is that you have special wishes and comments, but this is not your case :)

Everything, try. Unsubscribe.

  • Hello, thank you very much for your reply and advice, as a newbie, this is very useful for me. I did everything as you wrote, but it still did not help :( I created the test.html and test.php files now, this form in test.html: form method = "post" action = "test.php"> Name: <input type = "text" name = "name"> <br> <input type = "submit" name = "enter" value = "Forward" / > <br> <input type = "reset" value = "Clear"> <br> And in test.php: print_r ($ _ POST); var_dump ($ _ POST); When you submit a form, Array () array (0) { }. It turns out that the post method I have not processed at all? - Dust0
  • @ Dust0 Try removing action='test.php' . And rename the test.html file to index.php or any other name, put the main extension .php . And in this file above try to include 'test.php' , I will do your example a little later and show you if nothing happens. And yet, I hope the form has a closing tag :)) - Rosnowsky
  • I tried, but it still doesn’t work. The form has yes, the tag is closed, I just copied it crookedly. Is there something else to see or is it not in this file? In general, as set xampp, did not change anything in any files. - Dust0
  • No, nothing needs to be changed in php.ini . There everything is set up as it should. I use the Open Server , I personally liked it most of all, it is a pleasure to work :) Well, okay. I will now add a few lines in my answer, what needs to be done. - Rosnowsky
  • I did everything and finally found the problem!)) It was all about PhpStorm, when I opened a project through my browser, for example, localhost / test-project / index.php , then I added something like this to localhost. localhost: 62233 and after index.php there were lines. In the browser as a whole, I’ve got something like this: localhost: 62233 / test-project / ... as it turned out, there was a problem ...... When I independently entered localhost / test in the browser -project / index.php without extraneous characters - it all worked)) - Dust0