-1

整天都在与这段代码作斗争,大部分错误都被敲定了,但现在我收到了这个错误消息

致命错误:在第 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>
4

1 回答 1

1

Inside register(), $pdo is not defined. Use the following instead:

$result_set = $db->prepare("INSERT INTO `users` (`username`, `password`) 
                        VALUES (:username, :password)");

Concerning the $sername and $password variables not getting initialized in the function, change your function definition to:

function register($db,$username,$password)

and call it like this:

register($db,$_POST['username'],$_POST['password']);
于 2013-10-16T20:39:45.580 回答