准备语句致命错误


prepare statement fatal error

<?php
//Defining page title
define('WEBSITE_TITLE', 'Register');
//Content location
$content = 'content/register.php';
//Database connection
try {
    $dbh = new PDO('mysql:host=localhost;dbname=saldev', 'admin', '420blazeit');
    $dbh = null;
} catch (PDOException $e) {
    die();
}
//Including website template
include_once 'template.php';

if(isset($_POST['submitRegister'])) {
    $username = $_POST['username'];
    $password = $_POST['password'];
    $password2 = $_POST['password2'];
    $email = $_POST['email'];
    $rank = 'user';

    $ins = $dbh->prepare("INSERT INTO 'users'('username', 'password', 'email', 'rank') VALUES (''username','password', 'email','user')");
    $ins->execute();
    echo 'Success! you have been register';
    header("Location: index.php");
    }
?>

我收到以下错误:"致命错误:在第 23 行的 C:''workspace''register.php null 上调用成员函数 prepare() "

我一直在试图找出问题所在,但我不知道如何解决这个问题。有人请帮忙!:c

您有两个单引号'。取代

$ins = $dbh->prepare("INSERT INTO 'users'('username', 'password', 'email', 'rank') VALUES (''username','password', 'email','user')");

$ins = $dbh->prepare("INSERT INTO users (username, password, email, rank) VALUES ('username','password', 'email','user')");

同时删除$dbh = null;

好吧,你设置null $dbh

$dbh = null;

此外,您不应该在 mysql 查询中使用表或列名称的'