一直在打这个代码得到了大部分错误,但现在我得到了这个错误消息
致命错误:在第26行的/home/jsnow/public_html/registration.php中调用非对象的成员函数prepare()
这是我的代码:
<?php
if(isset($_POST['username']) && isset($_POST['password']))
{
$db =connect('********','*********');
if($db!=false)
{
register($db);
echo "User registered";
}
}
function connect($dbuser,$dbpassword)
{
try{
$db = new PDO('mysql:host=localhost;dbname=jsnow_login', '*******' , '*********');
return $db;
}catch(PDOException $e){
echo $e;
return false;}
}
function register($db)
{
$result_set = $pdo->prepare("INSERT INTO `users` (`username`, `password`)
VALUES (:username, :password)");
$result_set->execute(array(
':username' => $username, ':password' => $password
));
}
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form action="http://vps8383.***********.com/~jsnow/registration.php" method="post">
username:<input type="text" name="username"/>
password:<input type="password" name="password"/>
email<input type="text" name="e_mail"/>
<input type="submit"/>
</form>
</body>
</html>
答案 0 :(得分:1)
在register()
内,未定义$pdo
。请改用以下内容:
$result_set = $db->prepare("INSERT INTO `users` (`username`, `password`)
VALUES (:username, :password)");
关于$sername
和$password
变量未在函数中初始化,请将函数定义更改为:
function register($db,$username,$password)
并将其称为:
register($db,$_POST['username'],$_POST['password']);