简体   繁体   English

HTML表单打开PHP脚本而不是运行它

[英]HTML form opens PHP script instead of running it

I am using WAMP with MySQL and PHP to run a local server. 我将WAMP与MySQL和PHP结合使用来运行本地服务器。 The WAMP server is online and I use a simple HTML script to run PHP file that connects to a database and inserts data in a table that I have created on WAMP server. WAMP服务器处于联机状态,我使用一个简单的HTML脚本来运行PHP文件,该文件连接到数据库并将数据插入在WAMP服务器上创建的表中。

The problem is that instead of running (executing) my script, the browser opens it in text mode. 问题在于,浏览器没有以运行方式(执行)而是以文本模式打开它。 As in I can see the script. 如我所见,脚本。

Now there have been similar questions on this forum and others and people have solved the problem. 现在这个论坛上有类似的问题,其他人和人们已经解决了这个问题。 However what makes my problem unique is that I am able to run a test.php . 但是,使我的问题独特的是我能够运行test.php I am able to display text with it as well as open info.php so PHP is running on my server, however when I use the HTML form it refuses to run and only opens the script. 我可以显示文本以及打开info.php因此PHP在我的服务器上运行,但是当我使用HTML表单时,它拒绝运行,仅打开脚本。

I am using Chrome browser and I have also checked it in Firefox with the same result. 我正在使用Chrome浏览器,并且在Firefox中也进行了检查,结果相同。

I have one HTML file that is a form linked to my PHP files which handles connection and insertion of values in the table on the WAMP server. 我有一个HTML文件,该文件是链接到我的PHP文件的表单,该文件处理WAMP服务器上表中的值的连接和插入。 The name of the database is test. 该数据库的名称为test。

form.html form.html

<form action= "create_product.php" method= "get">
    <center>
       <table>
            <td><label><b><font size="5"> Name</label></td>
            <td><font size="5"><input type="text" name="name" /></td>
         </tr>
         <tr>
            <td><label><b><font size="5"> Description</label></td>
            <td><font size="5"><input type="text" Description="description" /></td>
        </tr>
        <tr>
            <center><td></td></center>
            <center><td><input type="submit" value="Submit"/> <input type="reset" value="reset"/>
            </tr>

       </table>
    </center>

insert_product.php insert_product.php

<?php
$con= mysql_connect("localhost", "admin","");
$db = mysql_select_db("test");

$name = $_GET["name"];
$description = $_GET["description"];

$query = "Insert into products('name','description')values('".$name."','".$description."')";
$result = mysql_query($query);
?>

I had the same problem and was surfing all known technical forums. 我遇到了同样的问题,正在浏览所有已知的技术论坛。 I finally figured out what was the problem - nothing to do with PHP or Apache setup. 我终于弄清楚了问题所在-与PHP或Apache安装无关。 Nothing wrong with the code (at least on mine). 代码没有问题(至少在我看来是这样)。 Here's what was NOT working: I double-clicked to open the file from Finder (or explorer if you are a Windows user), and the URL in browser is 这是行不通的:我双击从Finder(如果您是Windows用户,则为Explorer)打开文件,浏览器中的URL为

"/file/xxxxx/foo.html" “/file/xxxxx/foo.html”

. This would open 这将打开

/file/xxxx/bar.php" /file/xxxx/bar.php”

, which doesn't work! ,这不起作用!

However, if I typed into the URL in browser 但是,如果我在浏览器中输入网址

"localhost/~xxx/foo.html" “本地主机/〜XXX / foo.html”

, it runs nicely when ,当

"bar.php" “bar.php”

is called. 叫做。

Perhaps this is what you can try. 也许这是您可以尝试的。

"file:///XXXX/foo.php" “文件:///XXXX/foo.php”

doesn't work, but 不起作用,但是

"localhost/XXX/foo.php" “本地主机/ XXX / foo.php”

does. 确实。

Make sure that you open it localhost/yourfile.php not like C:\\wamp\\yourfile.php 确保您不像C:\\ wamp \\ yourfile.php那样打开它localhost / yourfile.php

Start all services 启动所有服务

or try xampp download 或尝试xampp 下载

tried wamp and xampp , in my opinion xampp is better 尝试过wamp和xampp,我认为xampp更好

This is edit- If u have tested that PHP is working on your local server,then the most probable cause for text display could be missing php end tag(?>) 这是编辑的-如果您已经测试过PHP在本地服务器上运行,那么最可能导致文本显示的原因可能是缺少php end tag(?>)

Also please note that if u have saved your file as anything other than .php,it will not be parsed. 还请注意,如果您将文件另存为.php之外的其他文件,则不会进行解析。 Files containing PHP code but saved as HTML or doc file renders the php codes within it useless. 包含PHP代码但另存为HTML或doc文件的文件使其中的php代码无用。

<<--EDIT ENDS-->> <<-编辑结束->>

u gave action= "create_product.php" in form although u specified the php file as insert_product.php 尽管您将php文件指定为insert_product.phpaction= "create_product.php"形式提供了action= "create_product.php"

Also as a good practice,always provide database connection variable in mysql_select_db 同样好的做法,总是在mysql_select_db中提供数据库连接变量

use $db = mysql_select_db("test",$con); 使用$db = mysql_select_db("test",$con);

instead of $db = mysql_select_db("test"); 而不是$db = mysql_select_db("test");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM