使用HTML表单将空字符串发送到mysql数据库

时间:2019-03-04 06:28:06

标签: php html mysql

if(isset($_POST['submit']) && !empty($_POST['submit']))始终是 计算为假,当我摆脱if语句时, 程序将空字符串发送到数据库的属性。我是 PHP的新功能,所以我不确定将php代码段放在哪里 HTML代码以及位置是否重要。任何帮助或建议 将不胜感激。

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta name= "viewport" content="width=device-width">
        <meta name= "descriptions" content="User friendly online animal shelter">
        <meta name= "keywords" content="animal, shelter, animal shelter, dogs, cats, adoption, donation, pets">
        <meta name= "author" content="">
        <title>Animal Shelter | About</title>
        <link rel="stylesheet" href= "./style.css">
    </head>
    <body>

        <header>
            <div class= "container">
                <div id= "branding">
                    <h1><span class= "highlight">DJ's</span> Animal Shelter</h1>
                </div>
                <nav>
                    <ul>
                        <li><a href= "index.php">Home</a></li>
                        <li><a href= "about.php">About</a></li>
                        <li><a href= "login.php">Sign In</a></li>
                    </ul>
                </nav>
            </div>
        </header>

        <section id= "Create">
            <div class="container">
                <div class="dark">
                    <form>

                        <h1>Create Account</h1>

                        <div class="form-input">
                            <input type="text" name = "fname" placeholder="First Name"/>    
                        </div>

                        <div class="form-input">
                            <input type="text" name = "lname" placeholder="Last Name"/> 
                        </div>

                        <div class="form-input">
                            <input type="text" name = "username" placeholder="Username"/>   
                        </div>

                        <div class="form-input">
                            <input type="email" name = "email" placeholder="Email"/>    
                        </div>

                        <div class="form-input">
                            <input type="text" name = "phone_number" placeholder="Phone Number"/>   
                        </div>

                        <div class="form-input">
                            <input type="password" name = "pwd" placeholder="Password"/>
                        </div>

                        <input type="submit" name="submit" value = "Create Account"/>

                        <nav id= "create">
                            <button type = "submit" class= "button_2"><a href= "login.php" style="color:white">Already Have An Account? Sign In!</a></button>
                        </nav>

                    </form>
                </div>
            </div>  
        </section>

        <footer>
            <p>DJ's Animal Shelter, Copyright &copy; 2019</p>
        </footer>

<!--Sending new profile data to database-->
<?php
    // TODO: Add some sort of input validation if we have the time
    if(isset($_POST['submit']) && !empty($_POST['submit']))
    {
    // Variables
        $user = 'root';
        $password = 'root';
        $db = 'animal_shelter';
        $host = 'localhost';
        $first_name = $_POST['fname'];
        $last_name = $_POST['lname'];
        $phone_number = $_POST['phone_number'];
        $email = $_POST['email'];
        $username = $_POST['username'];
        $pwd = $_POST['pwd'];

    // Connect to database
        $con = mysqli_connect($host, $user, $password, $db);

    // Insert new profile in to database
        $sql = "INSERT INTO profile (First_Name, Last_Name, Mobile_Number, Email, Username, Pwd, Join_Date) 
                VALUES  ('$first_name', '$last_name', '$phone_number', '$email', '$username', '$pwd', NOW())";

        if(!mysqli_query($con, $sql))
            header('Location: error.php');
    }
?>

    </body>
</html>

1 个答案:

答案 0 :(得分:2)

您需要定义使用的方法。喜欢发布或获取。如果未定义,则默认情况下将使用get。这样使用

<form method="post" action="">
</form>