Php 致命错误:对字符串调用成员函数prepare()
请稍等,不要将其作为副本发布,因为我做了研究并尝试了所有方法,但都无法使其正常工作,我一直遇到此错误“致命错误:在C:\wamp64\www\Etego\dbcontroller.php的字符串上调用成员函数prepare(),第63行”我试图让我的题词表单上的人不要使用同一封电子邮件两次,提前谢谢!代码如下: dbcontroller.phpPhp 致命错误:对字符串调用成员函数prepare(),php,html,mysql,phpmyadmin,Php,Html,Mysql,Phpmyadmin,请稍等,不要将其作为副本发布,因为我做了研究并尝试了所有方法,但都无法使其正常工作,我一直遇到此错误“致命错误:在C:\wamp64\www\Etego\dbcontroller.php的字符串上调用成员函数prepare(),第63行”我试图让我的题词表单上的人不要使用同一封电子邮件两次,提前谢谢!代码如下: dbcontroller.php <?php class DBController { public $host = "localhost"; public $us
<?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
?>
index2.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>
https://Etego/signup.com
用户名
问题是,$database
变量是一个没有prepare()
函数的字符串。相反,您可能希望使用保存有效数据库连接的$conn
变量
要做到这一点,就要改变
$db->database->prepare("SELECT * FROM members WHERE email = ?");
到
这是PHP。这里有一些问题:
当您试图准备一条语句时,您使用的是$db->database->prepare()
,如果您查看您的类,则propertydatabase
是一个包含字符串成员的字符串,即public$database=“members”代码>这解释了正在报告的错误
您似乎还混淆了mysqli\ucode>API和PDO
API,并且正在使用一些PDO-API函数,这些函数永远不会工作它们是完全不同的
所以也要改变这一点
/* 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";
}
到
您将使用connection对象来准备查询和所有mysqli\uu
API函数、方法和属性
更新:仍在创建dup帐户
在我看来,您的dup帐户检查位置错误,应该移动到index2.php中
或者在这一行之后添加针对$error\u message
的测试,因为您忘记了测试Dup电子邮件检查是否产生了错误
if(!isset($error_message)) {
require_once("dbcontroller.php");
if ( !isset($error_message) ) {
我的强烈建议是在index2中执行Dup电子邮件检查,并将其从dbconnect.php
中删除,因为它并不真正属于dbconnect.php
,因为每次您想要在任何脚本中连接到数据库时,都会不必要地运行该检查 所以,$db->database
是一个字符串!(设置为成员)改为尝试$db->prepare(…)
。@PaulT。致命错误:调用未定义的方法DBController::prepare(),您的方法与我预期的有点不同,然后尝试:$db->conn->prepare(…)
看起来@sazzadur已经回答了。你知道我怎样才能把它传输到我的index2.php而不是dbcontroller上,因为我的所有其他错误消息都在index2上吗?你不想这样包含包括'C:\wamp64\www\Etego\stylesignup.css'代码>当您专门将站点移动到一个可以正常工作的unix服务器时。使用相关路径确实可以记录不同的动物。这让我大吃一惊,吓了我一跳。一切似乎都很好,但我仍然可以为不同的帐户使用同一封电子邮件,邮件上说我会注册。这取决于你在这段代码之后做了什么,以及你没有向我们展示的代码。所以我建议你再问一个关于that@RiggsFolly我已经向你展示了我所有的代码,而且我还要等2天才能问另一个问题,所以我希望你能现在就做?当然,只要你愿意就行。好了,费克斯建议
/* 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";
}
if(!isset($error_message)) {
require_once("dbcontroller.php");
if ( !isset($error_message) ) {