3

I have a html page that has a form which should submit some data to a database. I have linked it to php file. Whenever I press submit, it goes to the php file but it does not submit anything to the database. Instead, it shows the php code.

Here's my HTML

<html>

<body>
    <form action="insert.php" method="post">
        <fieldset>
            <legend>Name</legend>
            Firstname:
            <input type="text" name="firstname" />
            <br>
            <br> Lastname:
            <input type="text" name="lastname" />
            <br>
            <br>
        </fieldset>
        <input type="submit" />
    </form>
</body>

</html>

Here's the PHP Code

<?php

$user_name = "sql683849";
$password = "*******";
$database = "sql683849";
$server = "sql6.freemysqlhosting.net";

mysql_connect("$server", "$user_name", "$password");

mysql_select_db("$database");

if (isset($_POST['submit'])) {
    $firstname = $_POST['firstname'];
    $lastaddress = $_POST['lastname'];

    $order = mysql_query("INSERT INTO nametable (firstname, lastname) VALUES ('$firstname', '$lastname)");

    if ($order) {
        echo '<br>Input data is successful';
    } else {
        echo '<br>Input data is not valid';
    }
}
?>

I'm not quite what's wrong with this code. Please help! Thank you

Envil
  • 2,687
  • 1
  • 30
  • 42
  • Have you installed php in your apache? What filetype is your php file exactly? Is that your own server or a hosted one? – Marvin Fischer Jul 16 '15 at 07:27
  • Hi! This is a free hosted server. I have just started learning PHP. The file name is 'insert.php'. I'm not quite sure what apache is. Thanks! – user5122378 Jul 16 '15 at 07:32
  • If you don't know if your hosting provider supports PHP, you can simply download something like XAMPP (Windows), LAMPP (Linux) or MAMP (Mac) this will give you a local web server on your computer with support of Apache, MySQL and PHP rather than using a free hosting service (which isn't ideal for development). Once you have a local server installed you can access via http://localhost or http://127.0.0.1 – Danny Broadbent Jul 16 '15 at 07:42
  • Yeah, I'll try to learn more about XAMPP. For the time being, I need to check with the free server provider whether they support PHP or not. But is the code correct? Thanks – user5122378 Jul 16 '15 at 07:45
  • 2
    The code looks perfectly fine. Although I recommend you not to use mysql. Either jump to Mysqli (yes just an "i" more at the end) or PDO cause mysql is deprecated and will be removed in the future – Marvin Fischer Jul 16 '15 at 09:58
  • Agree. Its clear that your code does not get interpreted by php. Either your provider does not support it with your subscription or you need another file ending ('.php5'). Most likely the first one. – hogan Jul 16 '15 at 22:56
  • **Danger**: You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). You are **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that a modern API would make it easier to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Jul 18 '15 at 23:15

1 Answers1

-1

You need to add a name to the submit button.

<input type="submit" name="submit" />
pjau
  • 915
  • 1
  • 8
  • 14