Php 使用HTML表单向mysql数据库发送空字符串

Php 使用HTML表单向mysql数据库发送空字符串,php,html,mysql,Php,Html,Mysql,if(设置($\u POST['submit'])和&!empty($\u POST['submit']))始终为空 计算结果为false,当我去掉if语句时 程序向数据库的属性发送空字符串。我是 PHP新手,因此我不确定将PHP代码段放在何处 HTML代码以及位置是否重要。有什么帮助或建议吗 非常感谢 <!DOCTYPE html> <html> <head> <meta charset="utf-8">

if(设置($\u POST['submit'])和&!empty($\u POST['submit']))
始终为空 计算结果为false,当我去掉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>

动物收容所
DJ动物收容所
创建帐户 DJ的动物收容所,版权和副本;2019年


您需要定义您使用的方法。喜欢发帖还是收。如果未定义,则默认情况下将使用get。像这样使用

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

您应该在
表单中添加
method=“post”
标记。警告:您对参数化查询非常开放,应该真正使用参数化查询,而不是像这样手动生成查询。特别是因为你根本没有逃避用户的输入!永远不要以明文形式存储密码!只存储密码哈希!使用PHP的和。如果您运行的PHP版本低于5.5(我真的希望您不是),那么可以使用来获得相同的功能。