准备声明致命错误

时间:2016-01-20 12:15:10

标签: php prepared-statement

<?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

2 个答案:

答案 0 :(得分:1)

嗯,您在null

上设置了$dbh
$dbh = null;

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

答案 1 :(得分:1)

您有两个单引号'。取代

$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;