Sam B.
Sam B.

Reputation: 303

Form Data to a Table With a Database

A few months ago I was working on a project and posted it here for help: How to submit a form results to a table on another page?. Now I need help again. If you go down to this comment on my previous question (see comment here: How to submit a form results to a table on another page?), this user was helping me and I got everything figured out except the the database. When I submit my form, the page loads with a blank table. How can I get it so when I enter the form information and submit it, it then goes to the 2nd page (see below) and puts the data into the table? I had it working before, before I incorporated the database in it.

Here is my code for the page called dispatch.php (which is the the form page):

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

And here is my page called indexcad.php (which is the page the form data comes up on):

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

Upvotes: 0

Views: 73

Answers (1)

fpierrat
fpierrat

Reputation: 804

The general shape of your script could look like this (i didn't look precisely if smthg wrong in your code, just reorganized it a bit...

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

Upvotes: 1

Related Questions