簡體   English   中英

PDO准備好的將NULL值插入mysql數據庫的語句

[英]PDO prepared statements inserting NULL value into mysql database

在這里,我有一個基本的注冊表格。

<form name="signUp" id="signUp" method="POST" action="insertUser.php">
            <table>
                <tr>
                   <td>Name</td>
                   <td><input type="text" name="signUpName" placeholder="Name"/></td>
                </tr> 
                <tr>
                   <td>Email</td>
                   <td><input type="text" name="signUpEmail" placeholder="Email"/></td>
                </tr> 
                <tr>
                   <td>Password</td>
                   <td><input type="password" name="signUpPassword" placeholder="password"/></td>
                </tr> 
                <tr>
                   <td><input type="submit" name="submit" value="Go"/></td>
                </tr> 
            </table>
        </form> 

insertUser.php字段數據發送到文件insertUser.php

  <?php

 include 'User.php';

 if(isset($_POST['submit'])){

    $user = new User();

    $user->setUserName($_POST['signUpName']);
    $user->setUserEmail($_POST['signUpEmail']);
    $user->setUserPassword($_POST['signUpPassword']);
    $user->userInsert();


 }

?>

該文件創建一個名為User的類的對象,並將從表單接收的數據傳遞給該類的變量。 最后, User類的userInsert函數將數據插入數據庫中。

<?php

     class User{

        public $userName, $userEmail, $userPassword;

        public function getUserName(){
            return $this->userName;
        }

        public function setUserName($userName){
            $this->userName = $userName;
        }

        public function getUserEmail(){
            return $this->userEmail;
        }

        public function setUserEmail($userEmail){
            $this->userEmail = $userEmail;
        }

        public function getUserPassword(){
            return $this->userPassword;
        }

        public function setUserPassword($userPassword){
            $this->userPassword = $userPassword;
        }


     public function userInsert(){

        global $userName, $userEmail, $userPassword;

        include 'db_connection.php';

         $r = $db->prepare("INSERT INTO user(Username, Useremail, Userpassword) VALUES(:userName, :userEmail, :userPassword);");
         $r->bindParam(':userName',$userName);
         $r->bindParam(':userEmail',$userEmail);
         $r->bindParam(':userPassword',$userPassword);
         $r->execute();

         header('Location: index.php?successful=1');

        }

}

數據庫連接

<?php

        try{
            $db = new PDO("mysql:host=localhost;dbname=basicchatapp","root","");
            $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        }
        catch(Exception $e){
            die("Error: ");
        }

?>

現在的問題是,每次插入NULL輸入時。

搜索了很多其他答案,但是沒有一個解決方案對我有用。 得到提示將有很大幫助。 謝謝。

可能是您的userInsert()函數未找到變量值,因此在函數中傳遞那些參數,則它應類似於以下代碼

 class User{

    public $userName, $userEmail, $userPassword;

    public function getUserName(){
        return $this->userName;
    }

    public function setUserName($userName){
        $this->userName = $userName;
    }

    public function getUserEmail(){
        return $this->userEmail;
    }

    public function setUserEmail($userEmail){
        $this->userEmail = $userEmail;
    }

    public function getUserPassword(){
        return $this->userPassword;
    }

    public function setUserPassword($userPassword){
        $this->userPassword = $userPassword;
    }


    public function userInsert($userName=null,$userEmail=null,$userPassword=null){
        include 'db_connection.php';

        $sql = "INSERT INTO user(Username, Useremail, Userpassword) VALUES($userName,$userEmail,$userPassword);";

        if(mysqli_query($db,$sql) == TRUE){
            header('Location: index.php?successful=1');
        }
        else{
            header('Location: index.php?successful=0');
        } 
    }

}

盡管您應該考慮@tadman在PHP中使用綁定參數和預處理語句的注釋,但是您可能會發現以下行應在變量之前設置$ this->來設置作用域。

$sql = "INSERT INTO user(Username, Useremail, Userpassword) VALUES('$userName','$userEmail','$userPassword');";


$sql = "INSERT INTO user(Username, Useremail, Userpassword) VALUES('$this->userName','$this->userEmail','$this->userPassword');";

請以此替換您的查詢

$ sql =“ INSERT INTO user(Username,Useremail,Userpassword)VALUES('$ this-> userName','$ this-> userEmail','$ this-> userPassword');”;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM