Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/476.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用php/mysql javascript创建一个简单的web开发测试,但不起作用_Javascript_Php_Mysql_Json_Xmlhttprequest - Fatal编程技术网

使用php/mysql javascript创建一个简单的web开发测试,但不起作用

使用php/mysql javascript创建一个简单的web开发测试,但不起作用,javascript,php,mysql,json,xmlhttprequest,Javascript,Php,Mysql,Json,Xmlhttprequest,我正在使用php mysql javascript创建一个简单的web开发测试,虽然没有语法错误,但系统不能正常工作 有人能帮我吗 index.html getAllStudent.php getStudentByID.php ConnectionManager.php Connection.php db_config.php 当我按下“获取所有学员”按钮时,控制台显示此文本 控制台日志 GEThttp://localhost/studentservice/StudentSe

我正在使用php mysql javascript创建一个简单的web开发测试,虽然没有语法错误,但系统不能正常工作

有人能帮我吗

index.html getAllStudent.php

getStudentByID.php

ConnectionManager.php

Connection.php

db_config.php

当我按下“获取所有学员”按钮时,控制台显示此文本

控制台日志
GEThttp://localhost/studentservice/StudentService/getAllStudent.php
200行
20毫秒
client.js(第9行)

(!)警告:array_push()要求参数1为array,在第18行的C:\wamp\www\studentservice\studentservice\getAllStudent.php中给出空值 调用堆栈 #时间记忆功能定位 10.0018677800{main}()..\getAllStudent.php:0 20.0054708704 ()..\getAllStudent.php:18
(!)警告:array_push()要求参数1为array,在第18行的C:\wamp\www\studentservice\studentservice\getAllStudent.php中给出空值 调用堆栈 #时间记忆功能定位 10.0018677800{main}()..\getAllStudent.php:0 20.0057709072 ()..\getAllStudent.php:18
(!)警告:array_push()要求参数1为array,在第18行的C:\wamp\www\studentservice\studentservice\getAllStudent.php中给出空值 调用堆栈 #时间记忆功能定位 10.0018677800{main}()..\getAllStudent.php:0 20.0057709216 ()..\getAllStudent.php:18
(!)警告:array_push()要求参数1为array,在第18行的C:\wamp\www\studentservice\studentservice\getAllStudent.php中给出空值 调用堆栈 #时间记忆功能定位 10.0018677800{main}()..\getAllStudent.php:0 20.0059709360 ()..\getAllStudent.php:18 {“学生”:[],“数组”:null,“成功”:1}
getAllStudent.php中的拼写错误第18行
数组\u push($response[“student”],$student)


正确:
array\u push($response[“student”],$student)

请详细说明不起作用的原因。你的期望是什么?发生了什么?另外,我觉得问题中有太多的代码需要处理。把范围缩小到有问题的部分会更好。给我们一个线索?什么不起作用?试着一步一步地调试它。你能连接到你的mysql服务器吗?您可以获取数据并返回它们吗?此示例必须将从数据库请求的数据显示为json响应@Harry。问题是我不知道错误在哪里,所以我使用控制台显示所有代码。log希望任何人都能帮助mePS:为了您更容易调试,您可以通过开发人员工具找到XHR http URL。在新选项卡中打开XHR URL,您将看到错误。@Allen Chak这是错误,谢谢您的帮助
<html>
<head>

  <title>Testing Query</title>

 <script type="text/javascript" src="script/client.js"></script>

</head>
<body>
select the desired !
   <div>
      <p> <input type = "button" id = "requestedSTD" value = "Get All Student" onclick = "GetAllStudents();"/></p>
      <p> <input type = "button" id = "requestedSTD" value = "Get Student By ID" onclick = "GetStudentByID(2);"/></p>
   </div>
</body>
</html>
function GetAllStudents()
{
    var formRequest = new FormData();
    formRequest.append('getStudent', "getAllStudent");

    var xhr = new XMLHttpRequest();
    xhr.addEventListener("load", uploadComplete, false);
    xhr.open("GET", 'StudentService/getAllStudent.php');
    xhr.send(formRequest);

}

function GetStudentByID(id)
{

   var formRequest = new FormData();
   formRequest.append('sid', id);

   var xhr = new XMLHttpRequest();
   xhr.addEventListener("load", uploadComplete, false);
   xhr.open("POST", 'StudentService/getStudentByID.php');
   xhr.send(formRequest);   

}

function uploadComplete(evt)
{

  console.log(evt.target.responseText);
}
<?php

require_once '../ConnectionManager.php';
 $response = array();
  $db = ConnectionManager::getInstance();

  $result = mysql_query("SELECT * FROM student") or die(mysql_error());

  if(mysql_num_rows($result) >0)
  {
      $response["student"] = array();

      while($roe = mysql_fetch_array($result))
     { 
      $student = array();
      $student["ID"] = $row["ID"];
      $student["Index"] = $row["Index"];
      $student["Name"] = $row["Name"];

      array_push($response["student"], $student);

     } 
    $response["success"] = 1;
    echo json_encode($response);
  }
  else
  {
      $response["success"] = 0;
      $response["message"] = "No  students found!!";

      echo json_encode($response);
  }


?>
<?php
require_once '../ConnectionManager.php';

$response = array();
$db = connectionManager::getInstance();

if(isset($_POST["sid"]))
{
 $id = $_POST["sid"];
 $result = mysql_query("SELECT * FROM student WHERE ID = $id");

 if(!empty($result))
 {
    if(mysql_num_row($result) >0)
    {
      $row = mysql_fetch_array($result);

     $student = array();
     $student["ID"] = $row["ID"];
     $student["Index"] = $row["Index"];
     $student["Name"] = $row["Name"];

    $response ["success"] = 1;
    $response["$student"] = array();

    array_push($reponse["student"], $student);
    echo json_encode($response);
    }
    else
    {
     $response["success"] = 0;
     $response["message"] = "No Student found with this ID!!";
     echo json_encode($response);
    }
 }
 else
 {
    $response["success"] = 0;
    $response["message"] = "No Student found with this ID!!";
    echo json_encode($response);
 }

}
else
{
    $response["success"] = 0;
    $response["message"] = "Required feild(s) is missing!!";
    echo json_encode($response);
}

?>
<?php
require_once 'Connection.php';

class ConnectionManager
{
    static $connection = null;
    public static function  getInstance()
    {
        if(ConnectionManager::$connection == null);
           ConnectionManager::$connection = new Connection;
        return ConnectionManager::$connection; 
    }

    private function __construct()
    {   
    }
    private function __clone() 
    {
    }
}
?>
<?php

  class Connection
  {
     function __construct()
    {

      $this->connect();   
    }
    function __destruct()
    {

     $this->close();
    }
    function connect()
    {

       require_once __DIR__ . '/db_config.php';

       $connection = mysql_connect(SERVER, USER, PASSWORD) or die(mysql_error());
       $dbConnect = mysql_select_db(DATABASE) or die(mysl_error());
       return $connection;
    }

    function close()
    {
      mysql_close();
    }
  }
?>
<?php

 define('USER', "***");
 define('PASSWORD', "***");
 define('DATABASE', "studentservice");
 define('SERVER', "localhost");

?>
GET http://localhost/studentservice/StudentService/getAllStudent.php

200 OK
        20ms    
client.js (line 9)

<br />
<font size='1'><table class='xdebug-error xe-warning' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Warning: array_push() expects parameter 1 to be array, null given in C:\wamp\www\studentservice\StudentService\getAllStudent.php on line <i>18</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0018</td><td bgcolor='#eeeeec' align='right'>677800</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp\www\studentservice\StudentService\getAllStudent.php' bgcolor='#eeeeec'>..\getAllStudent.php<b>:</b>0</td></tr>
<tr><td bgcolor='#eeeeec' align='center'>2</td><td bgcolor='#eeeeec' align='center'>0.0054</td><td bgcolor='#eeeeec' align='right'>708704</td><td bgcolor='#eeeeec'><a href='http://www.php.net/array_push' target='_new'>array_push</a>
(  )</td><td title='C:\wamp\www\studentservice\StudentService\getAllStudent.php' bgcolor='#eeeeec'>..\getAllStudent.php<b>:</b>18</td></tr>
</table></font>
<br />
<font size='1'><table class='xdebug-error xe-warning' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Warning: array_push() expects parameter 1 to be array, null given in C:\wamp\www\studentservice\StudentService\getAllStudent.php on line <i>18</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0018</td><td bgcolor='#eeeeec' align='right'>677800</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp\www\studentservice\StudentService\getAllStudent.php' bgcolor='#eeeeec'>..\getAllStudent.php<b>:</b>0</td></tr>
<tr><td bgcolor='#eeeeec' align='center'>2</td><td bgcolor='#eeeeec' align='center'>0.0057</td><td bgcolor='#eeeeec' align='right'>709072</td><td bgcolor='#eeeeec'><a href='http://www.php.net/array_push' target='_new'>array_push</a>
(  )</td><td title='C:\wamp\www\studentservice\StudentService\getAllStudent.php' bgcolor='#eeeeec'>..\getAllStudent.php<b>:</b>18</td></tr>
</table></font>
<br />
<font size='1'><table class='xdebug-error xe-warning' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Warning: array_push() expects parameter 1 to be array, null given in C:\wamp\www\studentservice\StudentService\getAllStudent.php on line <i>18</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0018</td><td bgcolor='#eeeeec' align='right'>677800</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp\www\studentservice\StudentService\getAllStudent.php' bgcolor='#eeeeec'>..\getAllStudent.php<b>:</b>0</td></tr>
<tr><td bgcolor='#eeeeec' align='center'>2</td><td bgcolor='#eeeeec' align='center'>0.0057</td><td bgcolor='#eeeeec' align='right'>709216</td><td bgcolor='#eeeeec'><a href='http://www.php.net/array_push' target='_new'>array_push</a>
(  )</td><td title='C:\wamp\www\studentservice\StudentService\getAllStudent.php' bgcolor='#eeeeec'>..\getAllStudent.php<b>:</b>18</td></tr>
</table></font>
<br />
<font size='1'><table class='xdebug-error xe-warning' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Warning: array_push() expects parameter 1 to be array, null given in C:\wamp\www\studentservice\StudentService\getAllStudent.php on line <i>18</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0018</td><td bgcolor='#eeeeec' align='right'>677800</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp\www\studentservice\StudentService\getAllStudent.php' bgcolor='#eeeeec'>..\getAllStudent.php<b>:</b>0</td></tr>
<tr><td bgcolor='#eeeeec' align='center'>2</td><td bgcolor='#eeeeec' align='center'>0.0059</td><td bgcolor='#eeeeec' align='right'>709360</td><td bgcolor='#eeeeec'><a href='http://www.php.net/array_push' target='_new'>array_push</a>
(  )</td><td title='C:\wamp\www\studentservice\StudentService\getAllStudent.php' bgcolor='#eeeeec'>..\getAllStudent.php<b>:</b>18</td></tr>
</table></font>
{"student":[],"Array":null,"success":1}