<?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
答案 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;
。