准备语句致命错误
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");
}
?>
我收到以下错误:"Fatal error: Call to a member function prepare() on null in C:\workspace\register.php on line 23"
我一直在努力找出问题所在,但不知道如何解决这个问题。有人请帮忙! :c
嗯,你在 $dbh
上设置 null
$dbh = null;
此外,您不应在 mysql 查询表或列名称的过程中使用 '
。
你有两个单引号 '
。替换
$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;
.
<?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");
}
?>
我收到以下错误:"Fatal error: Call to a member function prepare() on null in C:\workspace\register.php on line 23"
我一直在努力找出问题所在,但不知道如何解决这个问题。有人请帮忙! :c
嗯,你在 $dbh
null
$dbh = null;
此外,您不应在 mysql 查询表或列名称的过程中使用 '
。
你有两个单引号 '
。替换
$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;
.