0

我在 PHP 中有一个文件,它调用数据库中的所有行项目,它显示一个包含所有行项目的表。我有一个 HTML 页面,其中包含一个表单,可以将项目添加到餐厅菜单的数据库中。我希望 PHP 文件用于显示此 html 页面中要调用的所有行项目,因此除了我拥有的表单之外,它还将在页面上显示表格。我该怎么做呢?

这是我的 HTML 文件代码:

<!DOCTYPE html>
    <html>
        <head>
            <link rel="stylesheet" type="text/css" href="admin_index.css"/>
            <title>B Place</title>
        </head>

        <body id = "bodyStyler">
            <h1 id ="header">
                <img src="logo.jpg" id="logo"/>
            </h1>
            <table id = "pageLinks">
                <tr>
                    <td class="buttons"><b><a href="admin_index.html">Home</a></b></td>
                    <td class="buttons"><b><a href="admin_menu.html">Menu</a></b></td>
                    <td class="buttons"><b><a href="admin_volleyball.html">Volleyball</a></b></td>
                    <td id="endButton"><b><a href="admin_blog.html">Blog</a></b></td>
                </tr>
            </table>
            <div id="content">
                <h1 id="bodyText"> Menu </h1>
                <table>
                    <form method="post" action="input_menu.php">
                    <tr>
                        <td class="chart">Product Name:</td>
                        <td>
                        <input type="text" name="productName" value="" />
                        </td>
                    </tr>
                    <tr>
                        <td class="chart">Product Type:</td>
                        <td>
                        <input type="text" name="type" value=""  />
                        </td>
                    </tr>
                    <tr>
                        <td class="chart">Product Price:</td>
                        <td>
                        <input type="text" name="price" value=""  />
                        </td>
                    </tr>
                    <tr>
                        <td class="chart">Product is a Special:</td>
                        <td>
                        <input type="checkbox" name="isSpecial" value=""  />
                        </td>
                    </tr>
                    <tr>
                        <td class="chart">Day of the Week Product is a Special:</td>
                        <td>
                        <input type="text" name="specialDay" value=""  />
                        </td>
                    </tr>
                    <tr><td>&nbsp;</td>
                        <td>
                        <input type="submit" value="Submit" />
                        </td>
                    </tr>
                </table>
                <?php include 'admin_menu_lineitems.php';?>
        </body>
    </html>

这是我想要包含的 PHP 文件(admin_menu_lineitems.php)的代码:

<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("b_place", $con);

$result = mysql_query("SELECT * FROM menu");

echo "<table border='1'>
<tr>
<th> Type </th>
<th> Product Name </th>
<th> Price </th>
</tr>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['type'] . "</td>";
  echo "<td>" . $row['productName'] . "</td>";
  echo "<td>" . $row['price'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysql_close($con);
?>
4

5 回答 5

2

最简单(也是迄今为止最常见)的方法是:

  • 将您的 HTML 页面重命名为 PHP 页面
  • 将您的 PHP 代码剪切并粘贴到最终页面的适当部分

这样,整个页面将作为 PHP 处理。

指向此页面的任何链接都必须指向重命名的页面。

如果您不将 .html 文件重命名为 .php 文件,PHP 处理器将不会对页面进行操作(默认情况下,无论如何),只会将 PHP 代码直接输出到浏览器而不运行它。

于 2012-09-23T20:33:07.227 回答
0

您可以使用 require 或 include 来实现您的目标:

http://php.net/manual/en/function.require-once.php

或...(如果必须)

http://www.w3schools.com/php/php_includes.asp

于 2012-09-23T20:34:09.493 回答
0

如果您不想重命名,可以尝试让您的服务器将 html 作为 php 文件运行。检查这篇文章: http: //php.about.com/od/advancedphp/p/html_php.htm

于 2012-09-23T20:37:12.437 回答
0

为了扩展目前的一般响应,HTML 代码应该在一个文件扩展名为 .php 而不是 .html 的文件中,以便任何 PHP 工作。我对下面的 html 做了一些小改动(记得使用结束标签),但是 PHP 似乎没问题,所以包含应该可以正常工作。

<?php
  /** (the include file) **/
  $db = mysql_connect("localhost", "username", "password");
  mysql_select_db("database", $db);
  $result = mysql_query("SELECT * FROM menu");
  while($row = mysql_fetch_array($result)) {
    echo "<tr>".
          "<td>". $row["type"] . "</td>".
          "<td>". $row["productName"] . "</td>".
          "<td>". $row["price"] . "</td>".
         "</tr>";
  }
?>
<!DOCTYPE html>
<html>
<head>
  <link rel="stylesheet" type="text/css" href="admin_index.css"/>
  <title>B Place</title>
</head>

<body id="bodyStyler">
  <h1 id="header"><img src="logo.jpg" id="logo"/></h1>
  <table id="pageLinks">
    <tr>
      <td class="buttons"><b><a href="admin_index.html">Home</a></b></td>
      <td class="buttons"><b><a href="admin_menu.html">Menu</a></b></td>
      <td class="buttons"><b><a href="admin_volleyball.html">Volleyball</a></b></td>
      <td id="endButton"><b><a href="admin_blog.html">Blog</a></b></td>
    </tr>
  </table>
  <div id="content">
    <h1 id="bodyText"> Menu </h1>

    <form method="post" action="input_menu.php">
      <table>
        <tr>
          <td class="chart">Product Name:</td>
          <td><input type="text" name="productName" value=""/></td>
        </tr>
        <tr>
          <td class="chart">Product Type:</td>
          <td><input type="text" name="type" value=""/></td>
        </tr>
        <tr>
          <td class="chart">Product Price:</td>
          <td><input type="text" name="price" value=""/></td>
        </tr>
        <tr>
          <td class="chart">Product is a Special:</td>
          <td><input type="checkbox" name="isSpecial" value=""/></td>
        </tr>
        <tr>
          <td class="chart">Day of the Week Product is a Special:</td>
          <td><input type="text" name="specialDay" value=""/></td>
        </tr>
        <tr>
          <td>&nbsp;</td>
          <td><input type="submit" value="Submit"/></td>
        </tr>
      </table>
    </form>
    <table border='1'>
      <tr>
        <th>Type</th>
        <th>Product Name</th>
        <th>Price</th>
      </tr>
      <?php include 'admin_menu_lineitems.php';?>
    </table>
  </div>
</body>
</html>
于 2012-09-23T21:01:35.747 回答
-2

这是老派,但为什么不使用 iframe?但是将页面重命名为 .php 并使用 Web 服务器查看页面会更简洁。如果你还没有,wamp / xamp 很容易。

于 2012-09-23T20:56:11.497 回答