将数据表单到具有数据库的表

时间:2014-12-31 00:09:29

标签: 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?),这个用户正在帮助我,除了数据库之外,我得到了一切。当我提交表单时,页面加载一个空白表。当我输入表单信息并提交表单信息时,如何获取它,然后转到第2页(见下文)并将数据放入表中?在我将数据库合并到其中之前,我之前已经开始工作了。

这是我的代码,名为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>

1 个答案:

答案 0 :(得分:1)

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

<!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>