So I wrote a program that generates a simple report from a MySQL database. However, after changing the code to accept user input from an HTML form, I suddenly get a variety of errors, all originating from an undefined index (the variables are showing up as null).
The program works perfectly if I don't accept user input through the form.
What I've tried so far...
- Switching from POST to GET.
- Using an if(isset statement.
Not much, I know, but my knowledge of PHP doesn't go far beyond this.
Here are the specific errors:
"Undefined variable: B_DATE in ... on line 35" and "Undefined variable: E_DATE in ... on line 36"
There are more, but they all originate from the above issue.
Before I post the code, I put a debug array at the beginning of the code and it output the following:
$debug = true;
if ($debug === true)
{
echo '<hr />
<h4>Debug Area</h4>
<pre>';
print_r(array($_GET, $_POST));
echo '</pre>
<hr />';
}
Debug Area
Array
(
[0] => Array
(
[formID] => IRCcalculatepercentageform
[B_DATE] => 2014-10-12
[E_DATE] => 2014-10-13
)
[1] => Array
(
)
)
and
if(!isset($_GET['IRCcalculatepercentageform'])){
echo "No value";
}
returns "No value".
Here is the code for the HTML form:
<form method="get" action="IRCcalculatepercentage1.php">
<input type="hidden" name="formID" value="IRCcalculatepercentageform" />
<p> Type in the date you want to perform your calculation on (year, month, day): "0000-00-00" </p>
<p> Beginning Date: <input type="text" name="B_DATE" /></p>
<p> Ending Date: <input type="text" name="E_DATE" /></p>
<input type="submit" value="Submit" />
</form>
</body>
</html>
And here is the program code:
<?php //calculate percentage
error_reporting(E_ALL);
ini_set('display_errors', '1');
require_once 'IRCconfig.php';
$connection =
new mysqli($db_hostname, $db_username, $db_password, $db_database);
if ($connection->connect_error) die($connection->connect_error);
$B_DATE = $_GET['B_DATE'];
$E_DATE = $_GET['E_DATE'];
/* Computes the number of cells from column
'C_LAB' that contains the term 'Y'*/
$query1 = "SELECT C_LAB, DATE
FROM CLIENT_CHECKIN
WHERE DATE BETWEEN '$B_DATE' AND '$E_DATE'
HAVING C_LAB = 'Y'";
$result1 = $connection->query($query1);
if (!$result1) die($connection->error);
$rows1 = $result1->num_rows;
for ($j = 0 ; $j < $rows1 ; ++$j)
{
$result1->data_seek($j);
echo 'Computer lab: ' . $result1->fetch_assoc()['C_LAB'] . '<br><br>';
$count1 = $j;
}
/*Computes the number of cells from column 'C_LAB'
that contain the term 'N'*/
$query2 = "SELECT C_LAB, DATE
FROM CLIENT_CHECKIN
WHERE DATE BETWEEN '$B_DATE' AND '$E_DATE'
HAVING C_LAB = 'N'";
$result2 = $connection->query($query2);
if (!$result2) die($connection->error);
$rows2 = $result2->num_rows;
for ($l = 0 ; $l < $rows2 ; ++$l)
{
$result2->data_seek($l);
echo 'Computer lab: ' . $result2->fetch_assoc()['C_LAB'] . '<br><br>';
$count2 = $l;
}
$total = ($count1 + 1) + ($count2 + 1);
echo "The number of clients who used the computer lab is ", $count1 + 1, "<br><br>",
"The total number of clients who did not use the computer lab is ", $count2 + 1, "<br><br>",
"The total number of clients today is ", $total, "<br><br>";
echo "The percentage of clients who used the computer lab today is ", (($count1 + 1) / $total) * 100,
"%";
$result1->close();
$connection->close();
?>