开发者

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 "inst开发者_开发百科est" 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)
?>


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


Looks like you simply forgot to open the PHP tags in Insert.php using either <?php or <? (the former being the most compatible, and therefore preferable, version).


I think you have an error with this:

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

Try changing it to this:

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

I know you can have variables inside "" (I personally think its bad practice and slower) I think you will run into constant problems with the post var and how php interprets that.

You should turn on error_reporting as it would reflect that.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜