如何通过PHP加载HTML文件并从mySQL插入数据? [关闭]

问题描述:

I have an HTML file we'll call it dashboard.html.

I have a PHP file called index.php that will load on the webserver by default.

I have a mySQL database called myDb.

From index, how do I load the HTML file dashboard and INSERT data from my SQL database into the appropriate places?

Right now the html file may have a line that reads: John Doe . Instead of having a name in that span, I need to replace it with the appropriate PHP to insert the appropriate name from my SQL database. I actually already know how to echo the data from mySQL to the . What I don't know how to do is load the HTML from the PHP file.

我有一个HTML文件,我们称之为dashboard.html。 p>

我有一个名为index.php的PHP文件,默认情况下将加载到Web服务器上。 p>

我有一个名为myDb的mySQL数据库。 p>

来自 索引,如何将HTML文件仪表板和INSERT数据从我的SQL数据库加载到适当的位置? p>

现在html文件可能有一行:John Doe。 我需要用适当的PHP替换它以从我的SQL数据库中插入适当的名称,而不是在该范围内有名称。 我实际上已经知道如何将来自mySQL的数据回显到。 我不知道该怎么做是从PHP文件加载HTML。 p> div>

There are many ways to it:

1st: Put your html code into your index.php and echo your data on the right place. Simpliest one.

2nd: Use Keys in your html and replace it with the data. For example:

In dashboard.html

<input type="text" value="%DATA%" />

in your index.php

$holeFile = file_get_contents('dashboard.html');
$holeFile = str_replace('%DATA%', $resultSet['column']);
// ...

If you choose this way, there are already nice template engines like smarty.

3rd: Use DOM Objects in PHP and replace it. Mostly the same as 2nd, but in a nicer way. See here

Just follow follow the steps

index.php

$name= "xxxxx";
include "dashboard.php";

dashboard.php

<html>
<head></head>
<body>
Hello,<b><?php print($name);?></b>
</body>
</html>

Change your file name dashboard.html to dashboard.php and put $name variable in index.php file first then include dashboard.php file. Hope it will solve your problem