I created the database "anastasiya" with the table "users".

When a user registers on the site, nothing is recorded in the "users" table. Check for count. characters "login" and "password" is successful. It does not give any errors.

Tell me, what could be wrong here?

Here is the code:

<?php if (isset($_POST['reg'])){ $login = $_POST['login']; $password = $_POST['password']; $bad=false; session_start(); unset ($_SESSION['error_login']); unset ($_SESSION['error_password']); unset ($_SESSION['reg_success']); if ((strlen($login) < 3) || (strlen($login) > 32)){ $_SESSION['error_login'] = 1; $bad=true; } if ((strlen($password) < 6) || (strlen($password) > 32)){ $_SESSION['error_password'] = 1; $bad=true; } if (!$bad){ $mysqli = new mysqli("localhost","root","","anastasiya"); $password = md5($password); $login = $mysqli->real_escape_string($login); $password = $mysqli->real_escape_string($password); $mysqli -> query("INSERT INTO users ('login','password')VALUES('$login','$password')"); $mysqli -> close(); $SESSION['reg_success'] = 1; header("Location: index.php"); } } ?> 
  • If you omit the fact that it is impossible to write such code at all, I will assume that autocommit is not enabled and you need to enable it or send a commit manually - andreymal
  • And by the way, if you write like this, then in the database, the idea is to save exactly the lines '$login' and '$password' instead of the ones entered by the user. Therefore, either use string concatenation, or see how to pass parameters in mysqli (unfortunately, I am not familiar with mysqli). - Aries
  • @Anastasiya I think it would be useful for you to familiarize yourself with it.onat.edu.ua/docs/WEB_PHP_the_right_way.pdf - zenith

2 answers 2

Before $mysqli = new mysqli write

 mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); 

from the error message it can be seen that the names of the fields in the request are specified as strings, in quotes. Field names must be written without quotes.

For this request, backquotes are not needed. But you can do it by pressing the shift key and the ~ key at the same time (E in the Russian layout)

  • And tell me more. I added the line you told me. And I got the error Uncaught exception 'mysqli_sql_exception' with message 'You have an error in your SQL syntax; If you’re on the right side, you’ll find out if you’re on the right side of the server. \ reg.php: 25 Stack trace: # 0 Z: \ home \ Anastasiya \ www \ reg.php (25): mysqli-> query ('INSERT INTO use ...') # 1 {main} thrown in - Anastasiya
  • @Anastasiya do not shine with passwords :) + You wrote in pure English "you need to write a valid query to the database. Turn around with the manual" - zenith
  • @Anastasiya ktasty problem in quotes. In the enumeration of the fields of the table it is necessary to use the inverse single "and not the usual one" - zenith
  • This is a joke? How to make the return single quote, but not normal? - Anastasiya
  • the symbol 'and the symbol `this is not the same - AntonioK

programming practice shows the following:

 $sql = "INSERT INTO users ('login','password')VALUES('$login','$password')"; echo $sql,PHP_EOL; $mysqli -> query($sql); 

echo $ sql - this line can always be commented out or deleted, but we can always understand which query goes to the database

you can also execute the query in the output in the mysql or phpMyAdmin console and see errors

  • Alexandra, how are you always in the subject) - Ipatyev
  • Nice to see familiar faces:; Let the person get acquainted with the basics of debugging. - akalend