php - 使用数据库将数据形成表

标签 php html mysql forms

几个月前,我正在开发一个项目,并将其发布在这里寻求帮助:How to submit a form results to a table on another page? 。现在我再次需要帮助。如果您对我之前的问题进行评论(请参阅此处的评论: How to submit a form results to a table on another page? ),那么该用户正在帮助我,我已经弄清楚了除了数据库之外的所有内容。当我提交表单时,页面会加载一个空白表格。如何才能获得它,以便当我输入表单信息并提交时,它会转到第二页(见下文)并将数据放入表格中?在我将数据库合并到其中之前,我已经让它工作了。

这是我的名为dispatch.php的页面的代码(这是表单页面):

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>LMCS CAD - Live Incident Status</title>
<link href="styles.css" rel="stylesheet" type="text/css" />
<link rel="shortcut icon" href="http://tabstart.com/system/icons/14476/original/favicon.ico?1306467370" />
<meta name="robots" content="noindex">
</head>
<body style="font-family: Arial, Sans-Serif; font-size: 12px;">



<div align="center">
    <h1><img src="assests/lmcslogo.png" alt="York County 911 - Live Incident Status"/></h1>
    <p>
    <script type="text/javascript">
document.write(Date());
    </script></p>
    <h1><a href="index.php">Home</a> | <a href="dispatch.php">Dispatch An Incident</a> | <a href="help.php">Help</a></h1>
    <div></div>
</div>



<div align="center">
<form id="dispatch" name="dispatch" method="post" action="indexcad.php">
  <table width="801" height="420" border="1">
    <tr>
      <td align="center" id="town">TOWN</td>
      <td><input type="text" name="town" id="town" /></td>
      </tr>
    <tr>
      <td align="center" id="location">LOCATION</td>
      <td><input type="text" name="location" id="location" /></td>
      </tr>
    <tr>
      <td align="center" id="incident_type">INCIDENT TYPE</td>
      <td><input type="text" name="incident_type" id="incident_type" /></td>
      </tr>
    <tr>
      <td align="center" id="time_date">TIME/DATE</td>
      <td><input name="time_date" type="text" id="time_date" maxlength="60" /></td>
      </tr>
    <tr>
      <td width="138" align="center"><p id="admin">ADMIN </p></td>
      <td width="228"><input name="admin" type="text" id="admin" size="4" maxlength="4" /></td>
      </tr>
  </table>
  <p>
    <input type="submit" name="button" id="button" value="Submit" /> 
      <input type="reset" name="button2" id="button2" value="Reset" />
  </p>
</form>

</div>




<p>&nbsp;</p>
<hr />
<p style="text-align:center; font-size: 12px;">&copy; 2014 Lake McHenry County Scanner</p>


</body>
</html>

这是我的页面,名为indexcad.php(这是表单数据出现的页面):

<!DOCTYPE html>
<html>
<?php
//because you use method post you can access your form value by using this code
$town = $_POST['town'];
$location = $_POST['location'];
$incident_type= $_POST['incident_type'];
$time_date= $_POST['time_date'];
$admin = $_POST['admin'];

$db = mysql_connect('localhost','root','') or die("Database error"); 
mysql_select_db('mydatabase', $db);  
$result= mysql_query("select * from cad"); 
while($row = mysql_fetch_array($result))
?>

<head>
    <title>LMCS CAD</title>
</head>
<body>
  <div align="center">
  <form action="" method="get">
 <table width="968" height="248" border="1" align="center" cellpadding="10" cellspacing="0"  rules="rows" id="incidents" style="color:#333333;border-collapse:collapse;text-align:left;">
  <tr style="color:White;background-color:#5D7B9D;font-weight:bold;font-style:italic;">
  <th scope="col">TOWN</th>
  <th scope="col">LOCATION</th>
  <th scope="col">INCIDENT TYPE</th>
  <th scope="col">TIME/DATE</th>
  <th scope="col">ADMIN</th>
  </tr>
  <tr style="color:#333333;background-color:#F7F6F3;font-weight:bold;">
  <?php
      //replace this to your old php code 
echo "<td>" .$row['town'] ."</td>"; 
echo "<td>" .$row['location'] ."</td>"; 
echo "<td>" .$row['incident_type'] ."</td>"; 
echo "<td>" .$row['time_date'] ."</td>"; 
echo "<td>" .$row['admin'] ."</td>"; 
   ?>
  </tr>
    </table>
  </form>
  </body>
</html>

最佳答案

你的脚本的总体形状可能看起来像这样(我没有精确地查看你的代码中是否有错误,只是稍微重新组织了一下......

<!DOCTYPE html>
<html>
<head>
    <title>LMCS CAD</title>
</head>
<body>
  <div align="center">
  <form action="" method="get">
 <table width="968" height="248" border="1" align="center" cellpadding="10" cellspacing="0"  rules="rows" id="incidents" style="color:#333333;border-collapse:collapse;text-align:left;">
  <tr style="color:White;background-color:#5D7B9D;font-weight:bold;font-style:italic;">
  <th scope="col">TOWN</th>
  <th scope="col">LOCATION</th>
  <th scope="col">INCIDENT TYPE</th>
  <th scope="col">TIME/DATE</th>
  <th scope="col">ADMIN</th>
  </tr>
  <tr style="color:#333333;background-color:#F7F6F3;font-weight:bold;">
    <?php
    $town = $_POST['town'];
    $location = $_POST['location'];
    $incident_type= $_POST['incident_type'];
    $time_date= $_POST['time_date'];
    $admin = $_POST['admin'];

    $db = mysqli_connect('localhost','root','') or die("Database error"); 
    mysqli_select_db($db, 'mydatabase');  
    $result= mysqli_query($db, "select * from cad"); 
    while($row = mysqli_fetch_array($result))
    {
        echo "<td>" .$row['town'] ."</td>"; 
        echo "<td>" .$row['location'] ."</td>"; 
        echo "<td>" .$row['incident_type'] ."</td>"; 
        echo "<td>" .$row['time_date'] ."</td>"; 
        echo "<td>" .$row['admin'] ."</td>"; 
    }
    ?>
  </tr>
    </table>
  </form>
  </body>
</html>

关于php - 使用数据库将数据形成表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27714433/

相关文章:

php - 使用 PHP 从远程 MySQL 创建 CSV 文件

javascript - 如何使用jsoup选择html文档的叶标签

javascript - 如何在 keydown 上启动计时器并在 keyup 上停止?

php - 如何使用 PHP 限制数据库中 html 表格中每行的单元格数量

mysql - 无法在 MySQL 数据库中创建数据类型为 TEXT 的列

php - Laravel 在 View 中检测路由组

php - 使用 PHP 但不使用 MySQL 列出在线用户的最佳方法?

java - 如何从 php 执行 java 函数?

javascript - 无法让图像适合正文元素的背景

mysql - 基于 2 个表的查询,其中计算了重复的 id 金额