$smt->get_result() gives empty result after MySQL (only) INSERT

  mysqli, php

I’m doing a user registration in PHP and I’m trying to check, if user account after INSERT was actually created. What am I doing wrong? $dataR variable returns nothing, so after every registration, account is created, but the script still returns "Sorry, your registration failed. Please go back and try again."

Thanks for response, feel free to ask!

Register.php:

                $username = $this->db_connection->real_escape_string(strip_tags($_POST['username'], ENT_QUOTES));
                $useremail = $this->db_connection->real_escape_string(strip_tags($_POST['email'], ENT_QUOTES));
                $password = $_POST['password_new'];
                $options = [
                    'cost' => 10,
                ];
                $salt = $this->random_str(64);
                $salted_password = $password . $salt;
                $password_hash = password_hash($salted_password, PASSWORD_BCRYPT, $options);

                $query = $this->db_connection->prepare("SELECT name, mail FROM users WHERE name = ? OR mail = ?");
                $query->bind_param('ss', $username, $useremail);
                $query->execute();
                $results = $query->get_result(); // this works fine

                if($results->num_rows == 1) {
                    $row = $results->fetch_object();
                    
                    if($username == $row->name) {
                        $this->errors[] = "This username is already taken!";
                    } elseif($useremail == $row->mail) {
                        $this->errors[] = "This email address is already taken!";
                    } else {
                        $this->errors[] = "This username / email address is already taken.";
                    }
                } else {
                    $SIS = new SnowflakeIdService;
                    $snowflakeID = $SIS->CreateSnowflakeID();
                    $sql = $this->db_connection->prepare("INSERT INTO users (snowflake, name, salt, hash, mail) VALUES (?, ?, ?, ?, ?)");
                    $sql->bind_param("issss", $snowflakeID, $username, $salt, $password_hash, $useremail);
                    $sql->execute();
                    $dataR = $sql->get_result(); // this not

                    if($dataR) {
                        $this->messages[] = "Your account has been created successfully. You can now log in.";
                    } else {
                        $this->errors[] = "Sorry, your registration failed. Please go back and try again.";
                    }
                }

Source: Ask PHP

LEAVE A COMMENT