将数据从HTML表单插入MySQL

时间:2015-11-19 22:23:33

标签: php mysql forms

我在弄清楚如何使这段代码工作时遇到了问题。我想将HTML表单中的数据插入到我设置的本地MySQL数据库中。

HTML:

<!DOCTYPE html>
<html>

<head>
    <title>Database Project</title>
    <link rel="stylesheet" type="text/css" href="stylesheet.css"/>
</head>

<body>
    <div id="Container">
        <div id="Nav">
            <h2>Movie Database</h2>
        </div>
        <div id="Body">
            <h3>Register a movie database:</h3>
            <p>Are you a new director who just made a stunning movie? Or are you an actor with several movies on your resume. No matter what you are people need a way to find your movies. Well now you can! Upload your movie, director and or actor information down below:</p>
        </div>
        <div id="Body_Form">
            <form id="User_Form" method="post" action="process.php">
                <fieldset>
                    <legend>Account information:</legend>
                    First name:
                    <br>
                    <input type="text" name="firstname" placeholder="Type first name here" required size="30">
                    <br>
                    <br>
                    Last name:
                    <br>
                    <input type="text" name="lastname" placeholder="Type last name here" required size="30">
                    <br>
                    <br>
                    Sex:
                    <br>
                    <br>
                    <input type="radio" name="sex" value="male" required>Male
                    <br>
                    <input type="radio" name="sex" value="female">Female
                    <br>
                    <br>
                    Password:
                    <br>
                    <input type="password" name="password" placeholder="Type password here" required size="30"/>
                    <br>
                    <br>
                    Confirm password:
                    <br>
                    <input type="password" name="confirm_password" placeholder="Type password again here" required size="30"/>
                    <br>
                    <br>
                    Email-address:
                    <br>
                    <input type="text" name="email_address" placeholder="Type email address here" required size="30"/>
                    <br>
                    <br>
                    Confirm email-address:
                    <br>
                    <input type="text" name="confirm_email_address" placeholder="Type email address here again" required size="30"/>
                    <br>
                    <br>
                    <select name="birthmonth" required>
                        <option> - Month - </option>
                        <option value="January">January</option>
                        <option value="Febuary">Febuary</option>
                        <option value="March">March</option>
                        <option value="April">April</option>
                        <option value="May">May</option>
                        <option value="June">June</option>
                        <option value="July">July</option>
                        <option value="August">August</option>
                        <option value="September">September</option>
                        <option value="October">October</option>
                        <option value="November">November</option>
                        <option value="December">December</option>
                    </select>
                    <select name="birthday" required>
                        <option> - Day - </option>
                        <option value="01">01</option>
                        <option value="02">02</option>
                        <option value="03">03</option>
                        <option value="04">04</option>
                        <option value="05">05</option>
                        <option value="06">06</option>
                        <option value="07">07</option>
                        <option value="08">08</option>
                        <option value="09">09</option>
                        <option value="10">10</option>
                        <option value="11">11</option>
                        <option value="12">12</option>
                        <option value="13">13</option>
                        <option value="14">14</option>
                        <option value="15">15</option>
                        <option value="16">16</option>
                        <option value="17">17</option>
                        <option value="18">18</option>
                        <option value="19">19</option>
                        <option value="20">20</option>
                        <option value="21">21</option>
                        <option value="22">22</option>
                        <option value="23">23</option>
                        <option value="24">24</option>
                        <option value="25">25</option>
                        <option value="26">26</option>
                        <option value="27">27</option>
                        <option value="28">28</option>
                        <option value="29">29</option>
                        <option value="30">30</option>
                        <option value="31">31</option>
                    </select>
                    <select name="birthyear" required>
                        <option value="-"> - Year - </option>
                        <option value="1997">1997</option>
                        <option value="1996">1996</option>
                        <option value="1995">1995</option>
                        <option value="1994">1994</option>
                        <option value="1993">1993</option>
                        <option value="1992">1992</option>
                        <option value="1991">1991</option>
                        <option value="1990">1990</option>
                        <option value="1989">1989</option>
                        <option value="1988">1988</option>
                        <option value="1987">1987</option>
                        <option value="1986">1986</option>
                        <option value="1985">1985</option>
                        <option value="1984">1984</option>
                        <option value="1983">1983</option>
                        <option value="1982">1982</option>
                        <option value="1981">1981</option>
                        <option value="1980">1980</option>
                        <option value="1979">1979</option>
                        <option value="1978">1978</option>
                        <option value="1977">1977</option>
                        <option value="1976">1976</option>
                        <option value="1975">1975</option>
                        <option value="1974">1974</option>
                        <option value="1973">1973</option>
                        <option value="1972">1972</option>
                        <option value="1971">1971</option>
                        <option value="1970">1970</option>
                        <option value="1969">1969</option>
                        <option value="1968">1968</option>
                        <option value="1967">1967</option>
                        <option value="1966">1966</option>
                        <option value="1965">1965</option>
                        <option value="1964">1964</option>
                        <option value="1963">1963</option>
                        <option value="1962">1962</option>
                        <option value="1961">1961</option>
                        <option value="1960">1960</option>
                        <option value="1959">1959</option>
                    </select>
                    <br>
                    <br>
                    <input type="submit" value="submit">
                </fieldset> 
            </form>
            <form id="Movie_Form">
            </form>
        </div>
    </div>
</body>

</html>

PHP:

<?php
        if ($_POST){
        $servername= 'localhost';
        $username = 'root';
        $password = 'passwords';

        $connection = mysql_connect($servername, $username, $password);

        if (!$connection) {
            die("Connection failed: " . mysql_connect_error());
            echo "Oops!"
        }

        mysql_select_db("database_project", $connection);

        $firstname = $_POST['firstname'];
        $lastname = $_POST['lastname'];
        $sex = $_POST['sex'];
        $password = $_POST['password'];
        $confirm_password = $_POST['confirm_password'];
        $email_address = $_POST['email_address'];
        $confirm_email_address = $_POST['confirm_email_address'];

        $firstname = mysql_real_escape_string($firstname);
        $lastname = mysql_real_escape_string($lastname);
        $sex = mysql_real_escape_string($sex);
        $password = mysql_real_escape_string($password);
        $email_address = mysql_real_escape_string($email_address);

        $sql = "INSERT INTO 'database_project' . 'users' ('userid', 'firstname', 'lastname', 'sex', 'password', 'email_address') VALUES (NULL, '$firstname', '$lastname', '$sex', '$password', '$email_address');"; 

        mysql_query($query);

        echo '<script language="Javascript">alert("User was successfully created.");</script>';
        mysqli_close($connection);
        }
?>

问题是,只要我填写表单并提交,我就会在浏览器中获取PHP代码。没有执行,只有代码。如果有人能发现问题,那将非常感激!

0 个答案:

没有答案