1

I'm having a problem figuring out how to make this code work. I want to insert data from the HTML form into my local MySQL database that I setup.

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);
        }
?>

The problem is that as soon as I fill in the form and submit it, I just get the PHP code in my browser. No execution, just the code. If anyone can spot the problem it would be of great appreciation!

  • is the page with the php code .php or .html – Memor-X Nov 19 '15 at 22:25
  • It's called process.php so yeah PHP. – Bo Kamphues Nov 19 '15 at 22:28
  • 1
    What is the url of your broswer you access the file? Is it localhost/my_file.php or file:///path/to/your/file ? – Kostas Mitsarakis Nov 19 '15 at 22:30
  • @Fred-ii- could you explain how the incorrect use of quotes in the query is causing the entire file to output the PHP code to the browser? *"The problem is that as soon as I fill in the form and submit it, I just get the PHP code in my browser. No execution, just the code."* – Memor-X Nov 19 '15 at 22:33
  • 1
    It's a filepath like file:///path etc. – Bo Kamphues Nov 19 '15 at 22:38
  • So the SQL code should be like this? : – Bo Kamphues Nov 19 '15 at 22:40
  • @BoKamphues your Query does need fixing however i do not believe it is causing the problem with all your PHP code being outputted to the browser since as you said it's not even executing – Memor-X Nov 19 '15 at 22:41
  • "INSERT INTO `users` (`userid`, `firstname`, `lastname`, `sex`, `password`, `email_address`) VALUES (NULL, '$firstname', '$lastname', '$sex', '$password', '$email_address');" – Bo Kamphues Nov 19 '15 at 22:44
  • @Memor-X Yes I changed the query line now, thanks for that as well, but it indeed still outputs the entire PHP code to the browser. It almost seems like the problem lies with the action attribute in the form tag. – Bo Kamphues Nov 19 '15 at 22:48
  • doing a test with another php file i have which i am able to run i have noticed that `file://` is causing your problem. as @KostasMitsarakis as pointed out it should be `localhost/my_file.php` or whatever your webserver's url is (whether that is `localhost` of `www.mysite.com`). – Memor-X Nov 19 '15 at 22:49
  • Ah I that was indeed the problem, it's executing now. Thank you! – Bo Kamphues Nov 19 '15 at 22:51

0 Answers0