0

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>
3
  • 1
    this comment will be done, so why not by me :-) forget mysql_... functions, use mysqli_... or pdo class, for safety reasons. Commented Dec 31, 2014 at 0:17
  • then first of all: is your connection ok and does your request bring data? Try this while($row = mysql_fetch_array($result)) {print_r($row);} to test this first point. If this is ok then let's see how to make a valid while loop in the right place... Commented Dec 31, 2014 at 0:19
  • I tried that but it didn't work, although there were no errors on the page. But when I tried it and replaced the "mysql_" with "mysqli_", then it gave me a errors on the page. But without the "mysqli_" and just "musql_", no errors were given, but it still doesn't work. Commented Dec 31, 2014 at 0:24

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

8 Comments

also try to add this at the beginning of your script (before &lt;!doctype...> (to make sure you see all error/warning/notice messages): <?php error_reporting(E_ALL); ini_set("display_errors", 1); ?> Remove it when you are not debugging any more.
This is what happens now (I replaced my "mysql_" to "mysqli_" and copy/pasted the orgranized code that you did into it), it shows these errors on the page when I try submitting it: i.imgur.com/I7w3otI.png
add parameter $db to mysqli_select_db() and mysqli_query() calls (see edited answer above).
Thank you, that solved the error messages, but now the form data still doesn't go into the table on the next page.
please echo the print_r(..) as i proposed above, so you can see if your query give you some data... and maybe echo mysqli_num_rows($result); too, to see how many entries found, as well as echo mysqli_error($db); to get info on encountered errors on mysql side...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.