$mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname); if ($mysqli->connect_errno) { echo "Не удалось подключиться к MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error; } $stmt = $mysqli->prepare("INSERT INTO users ( user_id, user_status, user_email, user_pass, user_name, user_surname, user_avatar) VALUES (NULL, '2', ?, ?, ?, ?, '/front/img/users/user.png');"); $stmt->bind_param($this->email, $this->pass, $this->name, $this->surname); /* выполнение подготовленного выражения */ if (!$stmt->execute()) {echo "ошибочка!";}; /* Закрытие соединения и выражения*/ $stmt->close(); 

the code above gives an error ..

PS All variables checked for the presence of values, they are not empty. Edited the code, now it does not even reach the error output

 $stmt = $mysqli->prepare("INSERT INTO users ( user_id, user_status, user_email, user_pass, user_name, user_surname, user_avatar) VALUES (?, ?, ?, ?, ?, ?, ?);"); $stmt->bind_param('NULL', '2', $this->email, $this->pass, $this->name, $this->surname, '/front/img/users/user.png'); 
  • What kind of error gives the code? - Ilya Indigo
  • Two more comments on the code above. 1. The string 'NULL' has nothing to do with an empty NULL value. 2. Values ​​cannot be entered directly into bind_param, but can only be passed as variables - Ipatyev

1 answer 1

mysqli_stmt::bind_param first parameter accepts a string that describes the types of parameters, followed by the parameters themselves. There are 4 different identifiers in total

  • i - integer (integer)
  • d - double (floating point number)
  • s - string (string)
  • b - BLOB

For example, for four string parameters, the call should be

 $stmt->bind_param('ssss', $this->email, $this->pass, $this->name, $this->surname); 

Well, then, instead

if (!$stmt->execute()) {echo "ошибочка!";};

Write so

 if (!$stmt->execute()) echo $stmt->error; 

There will be more food for thought

  • error "No data supplied for parameters in prepared statement" - Nikolay
  • @Nikolay Did you specify the first parameter bind_param necessary types of parameters? - Anton Shchyrov
  • there must be lines .. obtained in this way: $name = $_POST["name"]; - Nikolay
  • @Nikolay edited the answer - Anton Shchyrov
  • one
    @Nikolay in the response the first parameter is the value of ssss which means four string values. Edited the answer again - Anton Shchyrov