I have two php files, file A and file B that get data from the same database. File A displays the data in a paragraph on the site while File B is used by AJAX to display data in a graph created with graph.js
. Both files do their job, except.
In both files I have a variable called $employeeNumber
which is the name of an input field in a form used by the user to input whichever employee number they would like to view the data of. File A allows me to use
$employeeNumber = $_POST["employeeNumber"];
to get the input and displays data accurately but File B won't allow me to use $_POST[]
, $_REQUEST[]
, or $_GET[]
. Instead it forces me to hardcode the unique number. This is not desired.
File B is meant to get the input entered into the form, and update the graph with the data relating to the employee number entered.
I used echo $row["empnumber"];
for File A to display the data and for File B I used print json_encode($data);
Could this be where I went wrong?
File B (Doesn't update graph unless I hardcode employeeNum
)
<?php
header('Content-Type: application/json');
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "mydb";
//BELOW IS WHERE THE ISSUE LIES.
//WHEN I HARDCODE THE UNIQUE NUMBER LIKE THIS, THE GRAPH UPDATES ACCORDINGLY. WITH ACCURATE DATA FROM THE EMPLOYEE THAT THE UNIQUE NUMBER BELONGS TO.
$employeeNum = 8020;
//THIS (BELOW) I ANTICIPATED WOULD GET THE EMPLOYEE NUMBER FROM THE USERS INPUT IN THE FORM WHICH I HAVE NOW INCLUDED BUT IT PRODUCES AN ERROR.
$employeeNum = $_POST["employeeNum"]; //this is line 9
//HERE IS THE ERROR WHEN I OPEN THE Graphdata.php FILE
<br />
<b>Notice</b>: Undefined index: employeeNum in <b>C:\wamp64\www\Graphdata.php</b> on line <b>9</b><br />
<br />
<b>Warning</b>: Invalid argument supplied for foreach() in <b>C:\wamp64\www\Graphdata.php</b> on line <b>22</b><br />
<br />
<b>Fatal error</b>: Uncaught Error: Call to a member function close() on bool in C:\wamp64\www\Graphdata.php:26
Stack trace:
#0 {main}
thrown in <b>C:\wamp64\www\Graphdata.php</b> on line <b>26</b><br />
//Connect to database
$dbconnect = new mysqli($servername, $username, $password, $dbname);
//If failed to connect
if(mysqli_connect_error()){
die("Database connection error".mysqli_connect_error());
}
//Create query
$sql = "SELECT Mathematics, English, Afrikaans, Geography, Physics, History, Life_Orientation FROM studentresults WHERE employeeNumber = $employeeNum";
//Run query
$result = $dbconnect->query($sql);
//Produce result
$data = array();
foreach ($result as $row){ //THIS IS LINE 22
$data[] = $row;
}
$result -> close();
$dbconnect -> close();
//Print the result
print json_encode($data);
?>
File A (Works fine)
<?php
//connecting to database
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "mydb";
$employeeNum = $_POST["employeeNum"];
$connect = new mysqli($servername, $username, $password, $dbname);
//checking connection
if (mysqli_connect_error()) {
die ("Database connection failed: ". mysqli_connect_error());
}
// Query the database
$sql = "SELECT Salary FROM myEmployees WHERE employeeNumber = $employeeNum";
$result = $connect->query($sql);
//print out result
while($row = mysqli_fetch_array($result)) {
echo $row["English"];
}
$result -> close();
?>
Here's the AJAX code making the request
$.ajax({
//Graphdata.php is the name of FILE B
url: "http://localhost/Graphdata.php",
type: "GET",
success : function(data){
console.log(data);
var Salary = [];
var timeWorked = [];
var Age = [];
var userid = [];
for(var i in data){
userid.push("UserID " + data[i].userid);
Salary.push(data[i].Salary);
Age.push(data[i].Age);
timeWorked.push(data[i].timeWorked);
}
//Create the graph
var ctx = document.getElementById('myChart').getContext('2d');
var chart = new Chart(ctx, {
Here is the form code.
<form action="Graphdata.php" method="post">
<p class="name">Username</p>
<input class="inp1" type="text" name="name">
<p class="password">Password</p>
<input class="inp2" type="password" name="email">
<p class="employeeid">Employee ID</p>
<input id = "employeeNum" class="inp3" type="int" name="employeeNum">