0

所以我正在尝试制作一个注册表单。我已经制作了一个 php MySQL 类,它使用构造函数与 new MySQLi() 建立连接以用于 verifyLogin 函数,并与 mysql_connect() 用于我的 addElement 函数。

错误在于我的 addElement 函数,SQL 语句正确写出,但似乎没有连接到数据库。我检查了所有名称是否正确。有任何想法吗?

<?php

class MySQL {

private $connection;
private $conn;
private $databaseName;

function __construct($dbServer, $dbUser, $dbPassword, $dbName) {
    $this->connection = new MySQLi($dbServer, $dbUser, $dbPassword, $dbName)
        or die('PROBLEM CONNECTING TO DATABASE');

    $this->conn = mysql_connect($dbServer, $dbUser, $dbPassword);
        echo $this->conn;

    $databaseName = $dbName;
}

function verifyLogin($table, $email, $password) {

    $query = "SELECT *
            FROM ?
            WHERE email = ?
            AND password = ?
            LIMIT 1";

    if($statement = $this->connection->prepare($query)) {
        $statement->bind_param('sss', $table, $email, $password);
        $statement->execute();

        if($statement->fetch()) {
            $statement->close();
            return true;    
        }
        else {
            return false;
        }
    }

}

function addElement($table, $firstName, $lastName, $email, $mobile, $password,
                        $faculty, $campus) {


    $query = "INSERT INTO $table (first_name, last_name, email, mobile, password, faculty, campus_location) 
            VALUES('$firstName', '$lastName','$email','$mobile',
            '$password','$faculty','$campus');";
    echo $query;

    mysql_select_db($this->databaseName, $this->conn);

    if(!mysql_query($query)) {
        die('Error: ' . mysql_error()); 
    }

    mysql_close($this->connection);
}


}

?>
4

2 回答 2

2

要在 MySQLi 中选择数据库,您需要像这样选择它:

$db = new mysqli("localhost", "my_user", "my_password", "database_name");

或者

$db = new mysqli("localhost", "my_user", "my_password");
$db->select_db('database_name');

与 MySQL 不同:

$db = mysql_connect('localhost', 'my_user', 'my_password');
$db_selected = mysql_select_db('database_name', $db);

MySQLi:http ://www.php.net/manual/en/mysqli.select-db.php

MySQL:http ://www.php.net/manual/en/function.mysql-select-db.php

于 2012-11-14T19:44:35.510 回答
0

您正在混合 mysql 和 mysqli php 驱动程序。

由于不推荐使用 mysql 驱动程序,我建议使用 mysqli。

PHP 文档: http ://www.php.net/manual/en/book.mysqli.php

让您入门的良好概述: http ://www.dreamincode.net/forums/topic/54239-introduction-to-mysqli-and-prepared-statements/

于 2012-11-14T19:42:30.123 回答