我有两个php文件,文件A和文件B,它们从同一数据库中获取数据。文件A在站点的段落中显示数据,而文件A在AJAX中使用文件B在用graph.js
创建的图形中显示数据。除了这两个文件,它们都可以工作。
在两个文件中,我都有一个名为$employeeNumber
的变量,它是输入字段的名称,该格式由用户用来输入他们想查看其数据的任何员工编号。文件A允许我使用
$employeeNumber = $_POST["employeeNumber"];
获取输入并准确显示数据,但是文件B不允许我使用$_POST[]
,$_REQUEST[]
或$_GET[]
。相反,它迫使我对唯一编号进行硬编码。这是不希望的。
文件B用来将输入输入到表单中,并使用与输入的员工编号有关的数据更新图形。
我对文件A使用了回显$row["empnumber"];
来显示数据,对于文件B我使用了print json_encode($data);
了这是我出了错的地方吗?
文件B(除非我对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);
?>
文件A(工作正常)
<?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();
?>
这是发出请求的AJAX代码
$.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, {
这是表格代码。
<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">
答案 0 :(得分:0)
问题在于,当您通过AJAX调用时,您没有将任何数据传递到后端。
此外,当您使用method=GET
时,$_POST
数组将为空。
将您的方法更改为POST
,并包含如下数据:
$.ajax({
method: "POST",
url: "http://localhost/Graphdata.php",
data: {employeeNum: someValue}
...
})