Search code examples
phphtmlinsertexecute

Insert using PHP shows up as a download instead of executing


Sorry if this already exists, but I couldn't find this anywhere.

I am trying to insert information from a form into a table called "instest" inside a database called "test". The columns inside the "instest" table are "firstName", "lastName", and "age".

The problem is that every time click on the submit button it either shows up as a download (IE) or the code shows up (Opera), instead of the code actually executing.

Here is the code:

/* TestInsert.php */

<!DOCTYPE HTML>
<html>
<body>
    <form action="Insert.php" method="post">
        Firstname: <input type="text" name="firstname" />
        Lastname: <input type="text" name="lastname" />
        Age: <input type="text" name="age" />
        <input type="submit" />
    </form>
</body>
</html>
/* Insert.php */

<?php

    $con = mysql_connect("localhost","root@localhost"); //No password
    if (!$con)
    {
        die('Could not connect: ' . mysql_error());
    }

    mysql_select_db("test", $con);

    $sql="INSERT INTO instest (firstName, lastName, age)
    VALUES
    ('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";

    if (!mysql_query($sql,$con))
    {
        die('Error: ' . mysql_error());
    }
    echo "1 record added";

    mysql_close($con)
?>

Solution

  • Try adding a value="submit"browsers prefer proper html