PHP:使用PDO格式将值从表单插入Mysql

时间:2019-06-14 17:18:53

标签: php mysql pdo

我创建了一个简单的注册表单,该表单将从数据库保存到tblsignup中,并且我尝试从该表单中插入一个值,当我尝试单击按钮并打开tblsignup的数据库表时,该表为空/未存储。对于帮助以及推荐的教程网站或文章,对于初学者如何学习pdo和oop,我将不胜感激。

这是我的连接文件

<?php 
// database_connection.php

    class Dbh{
        private $servername;
        private $username;
        private $password;
        private $dbname;
        private $charset;

        public function connect(){
            $this->servername = "localhost";
            $this->username = "root";
            $this->password = "";
            $this->dbname = "emailer";
            $this->charset = "utf8";

            try{
                $dsn = "mysql:host=".$this->servername.";dbname=".$this->dbname.";charset=".$this->charset;
                $pdo = new PDO($dsn, $this->username, $this->password);
                $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                return $pdo;
            }catch(PDOException $e){
                echo "Connection Failed: ".$e->getMessage();
            } 
        }
    }
?>

这是我的html。文件名:sign_up.php

<div class="bg">
<form action="" method="POST">
    <section id="SignUp">
        <div class="container">
            <h3><span class="fu">Yin</span></h3>
            <h2><span class="Acc">Create Accounnt</span></h2>
            <div class="form">
                <input type="email" name="txtEmail" class="txtEmail" placeholder="Email"><br>
                <input type="text" name="txtFName" class="txtFName" placeholder="First Name"><br>
                <input type="text" name="txtLName" class="txtLName" placeholder="Last Name"><br>
                <input type="text" name="txtMobile" class="txtMobile" placeholder="Mobile Number"><br>
                <input type="password" name="txtPassword" class="txtPassword" placeholder="New Password"><br>
                <input type="password" name="txtRe-Password" class="txtRe-Password" placeholder="Re-Password"><br>

                <h3><span class="Bday">Birthday</span></h3>
                <select name="slctMonth">
                    <option disabled selected>Month</option>
                    <option>January</option>
                    <option>February</option>
                    <option>March</option>
                    <option>April</option>
                    <option>May</option>
                    <option>June</option>
                    <option>July</option>
                    <option>August</option>
                    <option>September</option>
                    <option>October</option>
                    <option>November</option>
                    <option>December</option>
                </select>

                <select name="slctDay">
                    <option disabled selected>Day</option>
                    <?php
                        for($Day = 1; $Day <= 31; $Day++){
                    ?>
                    <option><?php echo $Day; ?></option>;
                    <?php
                        }
                    ?>
                </select>

                <select name="slctYear">
                    <option disabled selected>Year</option>
                    <?php
                        $Year = date("Y-m-d");
                        $Prev = 1950;

                        foreach(range($Year,$Prev) as $Yr){
                            echo '<option> '.$Yr;' </option>';
                        }
                    ?>
                </select>
                <input type="text" name="txtStatus" class="txtStatus" value="Not Verified" hidden>

                <h3><span class="Gender">Gender</span></h3>
                <input type="radio" name="Gender" class="rdMale" value="Male"> Male &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <input type="radio" name="Gender" class="rdFemale" value="Female"> Female
            </div>
            <input type="submit" name="btnSignUp" value="Sign Up" class="btnSignUp">
            <p> Already have an account? <a href="sign_in.php">Log in</a></p>
        </div>
    </section>
</form>
</div>

位于我sign_up.php及以下的提交查询是我的代码。

<?php
    if(isset($_POST['btnSignUp'])){

        $sql = "INSERT INTO tblsignup (Email, First_Name, Last_Name, Mobile_No, Password, Gender, Birth_Month, Birth_Day, Birth_Year, Status) 
        VALUES (:Email, :FirstName, :LastName, :Mobile, :Passowrd, :Gender, :BMonth, :BDay, :BYear, :Status)";

        $stmt = $pdo->prepare($sql);

        $stmt->bindValue(':Email', $_POST['txtEmail']);
        $stmt->bindValue(':FirstName', $_POST['txtFName']);
        $stmt->bindValue(':LastName', $_POST['txtLName']);
        $stmt->bindValue(':Mobile', $_POST['txtMobile']);
        $stmt->bindValue(':Passowrd', $_POST['txtPassword']);
        $stmt->bindValue(':Gender', $_POST['Gender']);
        $stmt->bindValue(':BMonth', $_POST['slctMonth']);
        $stmt->bindValue(':BDay', $_POST['slctDay']);
        $stmt->bindValue(':BYear', $_POST['slctYear']);
        $stmt->bindValue(':Status', $_POST['txtStatus']);

        $result = $stmt->execute();
    }

?>

先谢谢您!

0 个答案:

没有答案