PHP文件和文件html文件之间的链接问题



当我点击一个按钮通过PHP代码的实现将数据插入数据库时,问题是它打开了一个新的选项卡,并显示了PHP代码

html代码

<html>
<body>
<form action="InsertStudent.php" method="post">
Student Id: <input type="text" name="SId" />
Student Name:<input type="text" name="SName" />
Marks: <input type="text" name="marks" />
<input type="submit"  value= "Insert"/>
</form>
</body>
</html>

PHP代码

<?php
$con = mysql_connect("localhost","root","1234");
if (!$con)
{
die('Could not connect:'.mysql_error());
}
mysql_select_db("my_db",$con);
mysql_query("INSERT INTO Student(StudId,StudName,Marks)
VALUES ('9876543', 'Ahmed', '90')");
mysql_query("INSERT INTO Student(StudId,StudName,Marks)
VALUES ('12344556', 'Mohammed', '95')");
mysql_close($con);
?>

每个php脚本都必须在服务器上运行。在您的情况下,您没有使用服务器。您应该安装xamp或wamp服务器,然后在localhost上运行您的文件。

步骤1:从下载并安装examplehttps://www.apachefriends.org/

步骤2:将包含html和php文件的文件夹复制或移动到c:\example\htdocs目录步骤3:安装后打开控制面板并启动apache服务器

步骤4:打开浏览器并键入url localhost/your_folder_name

注意:文件夹名称不应包含空格或"-"。将你的html文件命名为index.html

最新更新