首页 > 解决方案 > 在 null 和未定义变量上调用成员函数 prepare():数据库

问题描述

当我按注册时

我知道你可能想把它作为重复的帖子发布,但我做了我的研究并尝试了一切,但仍然无法让它工作,我正在用 html、php 和 css 做一个题词表格,我有两个问题,未定义的变量:数据库并在 null 上调用成员函数 prepare()。代码中突出显示的部分是我正在处理的内容以及问题出在哪里。我有两页代码(3 页带有 css,但我不会包含它),我正在使用 php 5.6.35,如果还有问题,请随时提问,但我最近开始编程,所以我可能不知道你在做什么谈论。谢谢(我试图让用户两次没有相同的电子邮件,如果可能的话,我想有人帮助我确保用户是 13 岁以上,并且我的代码表中已经包含了“日期”输入,好的谢谢再次!)

index2.php:

<?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>PHP User Registration Form</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" name="userName" value="<?php if(isset($_POST['userName'])) echo $_POST['userName']; ?>"></td>
</tr>
<tr>
<td>First Name</td>
<td><input type="text" class="demoInputBox" name="firstName" value="<?php if(isset($_POST['firstName'])) echo $_POST['firstName']; ?>"></td>
</tr>
<tr>
<td>Last Name</td>
<td><input type="text" class="demoInputBox" name="lastName" value="<?php if(isset($_POST['lastName'])) echo $_POST['lastName']; ?>"></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" class="demoInputBox" name="password" value=""></td>
</tr>
<tr>
<td>Confirm Password</td>
<td><input type="password" class="demoInputBox" name="confirm_password" value=""></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" class="demoInputBox" 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>
</body></html>

dbcontroller.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 */                  $reqemail = $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][1]**
?> 

标签: phphtmlcss

解决方案


推荐阅读