且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在mysqli中插入数据不起作用

更新时间:2022-10-17 23:27:37

在mySQL中,标识符引号字符是反引号,所以用反引号替换列名周围的单引号:

$ $ p code $ sql =INSERT INTO accounts(`username`,``密码,电子邮件)
VALUES('{$ username}','{$ hashedpass}','{$ email}');


I am wondering why my insert statement does not work. There are no records shown in the database and it does not show any errors. Here is the code

<?php 
    if(isset($_POST['submit'])){
        $username = trim($_POST['username']);
        $password = trim($_POST['password']);
        $conf_pass = trim($_POST['conf_password']);
        $email = trim($_POST['email']);
        require_once('./includes/Db/CheckPassword.php');
        $check_pwd = new Db_CheckPassword($password);
        $passwordOK =$check_pwd->check();
        $password_match = Db_CheckPassword::confirmPassword($password,$conf_pass);
        require_once('./includes/Db/CheckUsername.php');
        $check_user = new Db_CheckUsername($username,$conn);
        $userOK = $check_user->check();

        if($userOK && $passwordOK && $password_match){
            $hashedpass = crypt($password);

            $sql = "INSERT INTO accounts ('username','password','email') VALUES('{$username}','{$hashedpass}','{$email}')";
            $conn->query($sql);
        }else{
            $pass_error =  $check_pwd -> getErrors();
            $user_error  = $check_user->getErrors();
        }
    }
?>

This is where I insert records into the database

if($userOK && $passwordOK && $password_match){
                $hashedpass = crypt($password);

                $sql = "INSERT INTO accounts ('username','password','email') VALUES('{$username}','{$hashedpass}','{$email}')";
                $conn->query($sql);

In mySQL the identifier quote character is the backtick , so replace the single-quotes surrounding the column-names by backticks:

$sql = "INSERT INTO accounts (`username`,`password`,`email`)    
                     VALUES('{$username}','{$hashedpass}','{$email}')";