Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/243.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用php向数据库提交HTML表单时,只显示php脚本_Php_Html_Database - Fatal编程技术网

使用php向数据库提交HTML表单时,只显示php脚本

使用php向数据库提交HTML表单时,只显示php脚本,php,html,database,Php,Html,Database,我有一个html页面,它有一个表单,可以向数据库提交一些数据。 我已经将它链接到php文件。每当我按下submit键时,它都会进入php文件,但不会向数据库提交任何内容。相反,它显示了php代码 这是我的HTML <html> <body> <form action="insert.php" method="post"> <fieldset> <legend>Name</legen

我有一个html页面,它有一个表单,可以向数据库提交一些数据。 我已经将它链接到php文件。每当我按下submit键时,它都会进入php文件,但不会向数据库提交任何内容。相反,它显示了php代码

这是我的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>

名称
名字:


姓氏:

下面是PHP代码

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

您需要向提交按钮添加名称

<input type="submit" name="submit" />


您在apache中安装了php吗?您的php文件的具体文件类型是什么?那是你自己的服务器还是托管服务器?嗨!这是一个免费托管服务器。我刚刚开始学习PHP。文件名为“insert.php”。我不太清楚什么是apache。谢谢如果你不知道你的主机提供商是否支持PHP,你可以简单地下载诸如XAMPP(Windows)、LAMPP(Linux)或MAMP(Mac)之类的东西,这将在你的计算机上为你提供一个支持Apache、MySQL和PHP的本地web服务器,而不是使用免费的主机服务(这对于开发来说并不理想)。一旦安装了本地服务器,您就可以通过或访问。是的,我将尝试了解更多关于XAMPP的信息。目前,我需要与免费服务器提供商核实他们是否支持PHP。但是代码正确吗?谢谢代码看起来很好。尽管我建议您不要使用mysql。要么跳转到Mysqli(是的,最后只是一个“i”),要么PDO,因为mysql已被弃用,将来将被删除