几个月前,我正在开发一个项目,并将其发布在这里寻求帮助: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> </p>
<hr />
<p style="text-align:center; font-size: 12px;">© 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/