致命错误:对字符串调用成员函数prepare()

pkmbmrz7  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(323)

等等,请不要把它作为一个副本发布,因为我做了研究,尝试了一切,但无法让它工作,我不断得到这个错误“致命错误:调用一个成员函数prepare(),在c:\wamp64\www\etego\dbcontroller.php的字符串在第63行”我试图让人们在我的题词形式不要使用同一封电子邮件两次,提前谢谢!代码如下:
数据库控制器.php

<?php
class DBController {
    public $host = "localhost";
    public $user = "root";
    public $password = "";
    public $database = "members";
    public $conn;

    function __construct() {
        $this->conn = $this->connectDB();
    }

    function connectDB() {
        $conn = mysqli_connect($this->host,$this->user,$this->password,$this->database);
        return $conn;
    }

    function runQuery($query) {
        $result = mysqli_query($this->conn,$query);
        while($row=mysqli_fetch_assoc($result)) {
            $resultset[] = $row;
        }        
        if(!empty($resultset))
            return $resultset;
    }

    function numRows($query) {
        $result  = mysqli_query($this->conn,$query);
        $rowcount = mysqli_num_rows($result);
        return $rowcount;    
    }

    function updateQuery($query) {
        $result = mysqli_query($this->conn,$query);
        if (!$result) {
            die('Invalid query1: ' . mysqli_error($this->conn));
        } else {
            return $result;
        }
    }

    function insertQuery($query) {
        $result = mysqli_query($this->conn,$query);
        if (!$result) {
            die('Invalid query2: ' . mysqli_error($this->conn));
        } else {
            return $result;
        }
    }

    function deleteQuery($query) {
        $result = mysqli_query($this->conn,$query);
        if (!$result) {
            die('Invalid query3: ' . mysqli_error($this->conn));
        } else {
            return $result;
        }
    }
}

/* Email already exists */
/*line 63*/       
$db = new DBController; 
$db->database->prepare("SELECT * FROM members WHERE email = ?");
$reqemail->execute(array($email));
$emailexist = $reqemail->rowCount();
if($emailexist == 0) {
} else {
    $error_message = "Email already exists";
}
//end of email existance
?>

索引2.php

<!-- how to make members when login "keep me signed in"  and ho to make users 13+ with the date input -->

<?php
if(!empty($_POST["register-user"])) {
    /* Form Required Field Validation */
    foreach($_POST as $key=>$value) {
        if(empty($_POST[$key])) {
        $error_message = "All Fields are required";
        break;
        }
    }
    /* Password Matching Validation */
    if($_POST['password'] != $_POST['confirm_password']){ 
    $error_message = 'Passwords should be same<br>'; 
    }

    /* Email Validation */
    if(!isset($error_message)) {
        if (!filter_var($_POST["userEmail"], FILTER_VALIDATE_EMAIL)) {
        $error_message = "Invalid Email Address";
        }
    }

    /* Validation to check if gender is selected */
    if(!isset($error_message)) {
    if(!isset($_POST["gender"])) {
    $error_message = " All Fields are required";
    }
    }

    /* Validation to check if Terms and Conditions are accepted */
    if(!isset($error_message)) {
        if(!isset($_POST["terms"])) {
        $error_message = "Accept Terms and Conditions to Register";
        }
    }

    if(!isset($error_message)) {
        require_once("dbcontroller.php");
        $db_handle = new DBController();
        $query = "INSERT INTO members (username, firstname, lastname, password, email, gender, dob) VALUES
        ('" . $_POST["userName"] . "', '" . $_POST["firstName"] . "', '" . $_POST["lastName"] . "', '" . md5($_POST["password"]) . "', '" . $_POST["userEmail"] . "', '" . $_POST["gender"] . "' , '" . $_POST["dob"] . "' )";
        $result = $db_handle->insertQuery($query);
        if(!empty($result)) {
            $error_message = "";
            $success_message = "You have registered successfully!";    
            unset($_POST);
        } else {
            $error_message = "Problem in registration. Try Again!";    
        }
    }
}
?>
<html>

<?php 

    include 'C:\wamp64\www\Etego\stylesignup.css';

    ?>

<head>
<title>https://Etego/signup.com</title>
</head>
<body>
<form name="frmRegistration" method="post" action="">
<table border="0" width="500" align="center" class="demo-table">
<?php if(!empty($success_message)) { ?>    
<div class="success-message"><?php if(isset($success_message)) echo $success_message; ?></div>
<?php } ?>
<?php if(!empty($error_message)) { ?>    
<div class="error-message"><?php if(isset($error_message)) echo $error_message; ?></div>
<?php } ?>
<tr>
<td>User Name</td>
<td><input type="text" class="demoInputBox allinsc" name="userName" value="<?php if(isset($_POST['userName'])) echo $_POST['userName']; ?>"></td>
</tr>
<tr>
<td>First Name</td>
<td><input type="text" class="demoInputBox allinsc" name="firstName" value="<?php if(isset($_POST['firstName'])) echo $_POST['firstName']; ?>"></td>
</tr>
<tr>
<td>Last Name</td>
<td><input type="text" class="demoInputBox allinsc" name="lastName" value="<?php if(isset($_POST['lastName'])) echo $_POST['lastName']; ?>"></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" class="demoInputBox allinsc" name="password" value=""></td>
</tr>
<tr>
<td>Confirm Password</td>
<td><input type="password" class="demoInputBox allinsc" name="confirm_password" value=""></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" class="demoInputBox allinsc" name="userEmail" value="<?php if(isset($_POST['userEmail'])) echo $_POST['userEmail']; ?>"></td>
</tr>
        <tr>
<td>Date Of birth</td>
<td><input type="date" value="<?php print(date("YYYY-MM-DD"))?>" class="demoInputBox" name="dob" value="<?php if(isset($_POST['dob'])) echo $_POST['dob']; ?>"></td>
</tr>
<tr>
<td>Gender</td>
<td><input type="radio" name="gender" value="Male" <?php if(isset($_POST['gender']) && $_POST['gender']=="Male") { ?>checked<?php  } ?>> Male
<input type="radio" name="gender" value="Female" <?php if(isset($_POST['gender']) && $_POST['gender']=="Female") { ?>checked<?php  } ?>> Female
    <input type="radio" name="gender" value="not specified" <?php if(isset($_POST['gender']) && $_POST['gender']=="not specified") { ?>checked<?php  } ?>> not specified
</td>
</tr>
<tr>
<td colspan=2>
<input type="checkbox" name="terms"> I accept <a href="terms.html">Terms and Conditions</a> <input type="submit" name="register-user" value="Register" class="btnRegister"></td>
</tr>

</table>
</form>

<div class="header1"></div>
    <div class="hdetail1"></div>

    <h class="etegotxt1">Etego</h>

    <img src="Etego_Logo.png" alt="Etego logo" width="50" height="50" class="logo1">

</body></html>
tv6aics1

tv6aics11#

这里有很多问题:
你正在准备一个你正在使用的语句 $db->database->prepare() 如果你看看你的班级 database 它是一个包含字符串的字符串 members 即。 public $database = "members"; 这就解释了所报告的错误
你似乎也有 mysqli_ api和 PDO api混乱,正在使用一些PDOAPI函数,这将永远不会工作-他们是完全不同的野兽。
所以也要改变这个

/* Email already exists */
/*line 63*/       
$db = new DBController; 
$db->database->prepare("SELECT * FROM members WHERE email = ?");
$reqemail->execute(array($email));
$emailexist = $reqemail->rowCount();
if($emailexist == 0) {
} else {
    $error_message = "Email already exists";
}

/* Email already exists */
/*line 63*/       
$db = new DBController; 
$stmt = $db->conn->prepare("SELECT * FROM members WHERE email = ?");
$stmt->bind_param("s", $email);
$stmt->execute();

$result = $stmt->get_result();

if($result->num_rows > 0) {
    $error_message = "Email already exists";
}

您将使用connection对象来准备查询和所有 mysqli_ api函数、方法和属性。
更新:仍在创建dup帐户
在我看来,您的dup帐户支票放错了位置,应该移到index2.php中。
或者在这行之后添加一个测试 $error_message 因为您忘记了测试dup电子邮件检查是否产生错误。

if(!isset($error_message)) {
    require_once("dbcontroller.php");
    if ( !isset($error_message) ) {

我的强烈建议是在index2中执行dup email check并将其从 dbconnect.php 因为它不属于 dbconnect.php 因为每当你想用任何脚本连接到一个数据库的时候,这都是不必要的!

wj8zmpe1

wj8zmpe12#

这东西是你的 $database 变量是没有 prepare() 功能。相反,您可能需要使用 $conn 保存有效数据库连接的变量。
要做到这一点,就要改变

$db->database->prepare("SELECT * FROM members WHERE email = ?");

$stmt = $db->conn->prepare("SELECT * FROM members WHERE email = ?");
$stmt->bind_param("s", $email);
$stmt->execute();

这是php官方文档。

相关问题