Reputation: 303
I get the following 5 errors on my webpage:
Notice: Undefined index: town in C:\xampp\htdocs\dispatch1.php on line 51
Notice: Undefined index: location in C:\xampp\htdocs\dispatch1.php on line 52
Notice: Undefined index: incident_type in C:\xampp\htdocs\dispatch1.php on line 53
Notice: Undefined index: time_date in C:\xampp\htdocs\dispatch1.php on line 54
Notice: Undefined index: admin in C:\xampp\htdocs\dispatch1.php on line 55
Here is my code for my output table that is getting the data from the database (which it does show the data from the database fine):
<?php include("manage_post.php"); ?>
<!DOCTYPE html>
<html lang='en'>
<head>
<meta http-equiv="refresh" content="2" >
</head>
<body>
<div align="center" class="CSSTableGenerator" height="100%">
<form action="dispatch1.php" method="get" id="dispatch">
<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>
Then, this is my "manage_post.php" page that posts the data to the database from the form:
<?php
if( $_POST )
{
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("mydatabase", $con);
$town = $_POST['town'];
$location = $_POST['location'];
$incident_type = $_POST['incident_type'];
$time_date = $_POST['time_date'];
$admin = $_POST['admin'];
$town = mysql_real_escape_string($town);
$location = mysql_real_escape_string($location);
$incident_type = mysql_real_escape_string($incident_type);
$time_date = mysql_real_escape_string($time_date);
$admin = mysql_real_escape_string($admin);
$query = "
INSERT INTO `mydatabase`.`cad` (`town`, `location`, `incident_type`, `time_date`, `admin`) VALUES ('$town', '$location', '$incident_type', '$time_date', '$admin');";
mysql_query($query);
mysql_close($con);
}
?>
I have tried to go through it and figure out what is wrong with it, but just can't find it. If you need the form code, let me know. Another problem with my PHP code that is weird is that the table instead of being vertical, it's horizontal and goes all the way off the screen across. Here is a picture of it. And then I have to scroll horizontally to view all of it.
Upvotes: 1
Views: 1180
Reputation: 1643
You can avoid all the if...isset and just dump what is in POST to variables named the same as the posted indexes. You really only need if/isset to see what is in a specific posted variable but if it doesn't matter this is the simplest way to avoid the error you are encountering.
foreach($_POST as $k=>$v){
${$k}=mysql_real_escape_string($v);
}
Upvotes: 0
Reputation: 340
It's problem in this part of the code
$town = $_POST['town'];
$location = $_POST['location'];
$incident_type= $_POST['incident_type'];
$time_date= $_POST['time_date'];
$admin = $_POST['admin'];
There is no index 'town', 'location',... in the array $_POST. You can easily fix the Notices by using isset so you can check if the index exists. If it does, assign the value to the variable $town ...
if(isset($_POST['town'])) { ... }
Upvotes: 0
Reputation: 1455
$ _POST
or $ _GET
are two special functions of PHP that are used to get variables from a user-filled form. While using these functions, a user may encounter an error
- Notice: Undefined index.
This error can be avoided with the help of PHP isset ()
. This error will be notified, but that depends on the configuration of the server. Notice: Undefined index is a minor error and hence not notified by default. With the help of the error_reporting
function, the type of error reported can be changed.
Upvotes: 0
Reputation: 218808
In the first file you show, you're trying to access POST values:
$town = $_POST['town'];
$location = $_POST['location'];
$incident_type= $_POST['incident_type'];
$time_date= $_POST['time_date'];
$admin = $_POST['admin'];
But nothing has been POSTed yet. You're still displaying the form that's going to be posted. How can displaying a form rely on that same form having already been submitted?
In order to conditionally check if the values have been POSTed, you can do the same thing you're doing in your second file:
if( $_POST )
{
// use the POST values
}
Optionally, you can conditionally check on each individual value:
if( isset($_POST['town']) )
{
// use the town value
}
if( isset($_POST['location']) )
{
// use the location value
}
// etc.
As for what you want to display when there are no values, that's up to you.
Upvotes: 3
Reputation: 593
Checking variable
if(isset($_POST['town'])) {
$town = $_POST['town'];
}
if(isset($_POST['location'])) {
$location = $_POST['location'];
}
Upvotes: 0