Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用oop在php中注册表单_Php_Sql - Fatal编程技术网

使用oop在php中注册表单

使用oop在php中注册表单,php,sql,Php,Sql,我只想做一个注册页面,但我被这个错误卡住了- 警告:从中的空值创建默认对象 第25行的C:\wamp\www\2209login\register.php* 而这个-> 致命错误:在中调用未定义的方法stdClass::create() 第30行的C:\wamp\www\2209login\register.php database.php <?php // used to get mysql database connection class Database{ // spec

我只想做一个注册页面,但我被这个错误卡住了-

警告:从中的空值创建默认对象 第25行的C:\wamp\www\2209login\register.php*

而这个->

致命错误:在中调用未定义的方法stdClass::create() 第30行的C:\wamp\www\2209login\register.php

database.php

<?php
// used to get mysql database connection
class Database{

    // specify your own database credentials
    private $host = "localhost";
    private $db_name = "ooplogin";
    private $username = "root";
    private $password = "";
    public $conn;

    // get the database connection
    public function getConnection(){

        $this->conn = null;

        try{
            $this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password);
            echo "connected";
        }catch(PDOException $exception){
            echo "Connection error: " . $exception->getMessage();
        }

        return $this->conn;
    }
}
?>
<?php
// 'user' object
class User{
  // database connection and table name
    private $conn;
    private $table_name = "users";

     // object properties
    public $id;
    public $firstname;
    public $lastname;
    public $email;

      // constructor
    public function __construct($db){
        $this->conn = $db;
        // create new user record
function create(){
 $query = "INSERT INTO
                " . $this->table_name . "
            SET
                firstname = :firstname,
                lastname = :lastname,
                email = :email";

                   // prepare the query
    $stmt = $this->conn->prepare($query);

     // sanitize
    $this->firstname=htmlspecialchars(strip_tags($this->firstname));
    $this->lastname=htmlspecialchars(strip_tags($this->lastname));
    $this->email=htmlspecialchars(strip_tags($this->email));

    // bind the values
    $stmt->bindParam(':firstname', $this->firstname);
    $stmt->bindParam(':lastname', $this->lastname);
    $stmt->bindParam(':email', $this->email);

    // execute the query, also check if query was successful
    if($stmt->execute()){
        return true;
    }else{
        $this->showError($stmt);
        return false;
    }
}
}
}
<?php
// core configuration
//include_once "config/core.php";

// set page title
$page_title = "Register";

// include login checker
//include_once "login_checker.php";

// include classes
include_once 'config/database.php';
include_once 'objects/user.php';
//include_once "libs/php/utils.php";

// include page header HTML
//include_once "layout_head.php";

if($_POST){

    // get database connection
    $database = new Database();
    $db = $database->getConnection();

    $user->firstname=$_POST['firstname'];
    $user->lastname=$_POST['lastname'];
    $user->email=$_POST['email'];

    // create the user
if($user->create()){

    echo "<div class='alert alert-info'>";
        echo "Successfully registered. <a href='{$home_url}login'>Please login</a>.";
    echo "</div>";
    }else{
    echo "<div class='alert alert-danger' role='alert'>Unable to register. Please try again.</div>";
}
}
?>
<form action='register.php' method='post' id='register'>

    <table class='table table-responsive'>

        <tr>
            <td class='width-30-percent'>Firstname</td>
            <td><input type='text' name='firstname' class='form-control' required value="<?php echo isset($_POST['firstname']) ? htmlspecialchars($_POST['firstname'], ENT_QUOTES) : "";  ?>" /></td>
        </tr>

        <tr>
            <td>Lastname</td>
            <td><input type='text' name='lastname' class='form-control' required value="<?php echo isset($_POST['lastname']) ? htmlspecialchars($_POST['lastname'], ENT_QUOTES) : "";  ?>" /></td>
        </tr>
 <tr>
            <td>Email</td>
            <td><input type='email' name='email' class='form-control' required value="<?php echo isset($_POST['email']) ? htmlspecialchars($_POST['email'], ENT_QUOTES) : "";  ?>" /></td>
        </tr>
        <td></td>
            <td>
                <button type="submit" class="btn btn-primary">
                    <span class="glyphicon glyphicon-plus"></span> Register
                </button>
            </td>
        </tr>

    </table>
</form>

user.php

<?php
// used to get mysql database connection
class Database{

    // specify your own database credentials
    private $host = "localhost";
    private $db_name = "ooplogin";
    private $username = "root";
    private $password = "";
    public $conn;

    // get the database connection
    public function getConnection(){

        $this->conn = null;

        try{
            $this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password);
            echo "connected";
        }catch(PDOException $exception){
            echo "Connection error: " . $exception->getMessage();
        }

        return $this->conn;
    }
}
?>
<?php
// 'user' object
class User{
  // database connection and table name
    private $conn;
    private $table_name = "users";

     // object properties
    public $id;
    public $firstname;
    public $lastname;
    public $email;

      // constructor
    public function __construct($db){
        $this->conn = $db;
        // create new user record
function create(){
 $query = "INSERT INTO
                " . $this->table_name . "
            SET
                firstname = :firstname,
                lastname = :lastname,
                email = :email";

                   // prepare the query
    $stmt = $this->conn->prepare($query);

     // sanitize
    $this->firstname=htmlspecialchars(strip_tags($this->firstname));
    $this->lastname=htmlspecialchars(strip_tags($this->lastname));
    $this->email=htmlspecialchars(strip_tags($this->email));

    // bind the values
    $stmt->bindParam(':firstname', $this->firstname);
    $stmt->bindParam(':lastname', $this->lastname);
    $stmt->bindParam(':email', $this->email);

    // execute the query, also check if query was successful
    if($stmt->execute()){
        return true;
    }else{
        $this->showError($stmt);
        return false;
    }
}
}
}
<?php
// core configuration
//include_once "config/core.php";

// set page title
$page_title = "Register";

// include login checker
//include_once "login_checker.php";

// include classes
include_once 'config/database.php';
include_once 'objects/user.php';
//include_once "libs/php/utils.php";

// include page header HTML
//include_once "layout_head.php";

if($_POST){

    // get database connection
    $database = new Database();
    $db = $database->getConnection();

    $user->firstname=$_POST['firstname'];
    $user->lastname=$_POST['lastname'];
    $user->email=$_POST['email'];

    // create the user
if($user->create()){

    echo "<div class='alert alert-info'>";
        echo "Successfully registered. <a href='{$home_url}login'>Please login</a>.";
    echo "</div>";
    }else{
    echo "<div class='alert alert-danger' role='alert'>Unable to register. Please try again.</div>";
}
}
?>
<form action='register.php' method='post' id='register'>

    <table class='table table-responsive'>

        <tr>
            <td class='width-30-percent'>Firstname</td>
            <td><input type='text' name='firstname' class='form-control' required value="<?php echo isset($_POST['firstname']) ? htmlspecialchars($_POST['firstname'], ENT_QUOTES) : "";  ?>" /></td>
        </tr>

        <tr>
            <td>Lastname</td>
            <td><input type='text' name='lastname' class='form-control' required value="<?php echo isset($_POST['lastname']) ? htmlspecialchars($_POST['lastname'], ENT_QUOTES) : "";  ?>" /></td>
        </tr>
 <tr>
            <td>Email</td>
            <td><input type='email' name='email' class='form-control' required value="<?php echo isset($_POST['email']) ? htmlspecialchars($_POST['email'], ENT_QUOTES) : "";  ?>" /></td>
        </tr>
        <td></td>
            <td>
                <button type="submit" class="btn btn-primary">
                    <span class="glyphicon glyphicon-plus"></span> Register
                </button>
            </td>
        </tr>

    </table>
</form>

您需要分离
\u构造
创建
方法。在当前代码中,创建方法位于构造函数内部,请将用户类更改为:

// 'user' object
class User{
  // database connection and table name
    private $conn;
    private $table_name = "users";

     // object properties
    public $id;
    public $firstname;
    public $lastname;
    public $email;

      // constructor
    public function __construct($db){
        $this->conn = $db;

    }
        // create new user record
     public function create(){
         $query = "INSERT INTO
                        " . $this->table_name . "
                    SET
                        firstname = :firstname,
                        lastname = :lastname,
                        email = :email";

                           // prepare the query
            $stmt = $this->conn->prepare($query);

             // sanitize
            $this->firstname=htmlspecialchars(strip_tags($this->firstname));
            $this->lastname=htmlspecialchars(strip_tags($this->lastname));
            $this->email=htmlspecialchars(strip_tags($this->email));

            // bind the values
            $stmt->bindParam(':firstname', $this->firstname);
            $stmt->bindParam(':lastname', $this->lastname);
            $stmt->bindParam(':email', $this->email);

            // execute the query, also check if query was successful
            if($stmt->execute()){
                return true;
            }else{
                $this->showError($stmt);
                return false;
            }
        }

}

$user=new user()
在您的注册表中丢失。phpthanks Masivuye先生第一个错误已解决,但第二个错误->致命错误:调用未定义的方法user::create()是否使用任何框架?不,我只使用notepade++,如果任何一个好的,请建议,解释您的代码为何工作以及如何工作,以便答案和问题对经验较少的开发人员有价值