繁体   English   中英

为什么我不能使用 php 从我的 SQL 数据库中的 html 文档中获取数据?

[英]Why can't i fetch data from my SQL database, in a html document, using php?

所以我正在编写一个 API,在前端它使用 HTML、JS 和 CSS,在后端它使用 PHP,它可以访问我的 SQL 数据库。

我的任务是一旦单击按钮,创建一个函数,该函数将以 JSON 编码的信息发送到 .php 文件,在那里我需要将该对象从 JSON 解码为 PHP,然后连接到 SQL 数据库,并返回相关JSON 编码的信息返回到 HTML 页面,稍后的功能将能够更新/删除某些数据。

此 API 使用两个主要页面,即 HTML 和 PHP 文件,以及 SQL 数据库。 有人可以告诉我为什么这不起作用以及我如何解决它? 为了澄清我希望能够选择一个单选按钮选项,然后按 app_list_button,这将返回按 app_name 或它们的评论评级排序的所有应用程序。 任何帮助将不胜感激,如果我能够更好地构建任何东西,请告诉我。

索引.html

<div id='app_content'>
  <label>Refine search by</label>
  </br>
  <input type="radio" id="name_radio">
  <label>Name</label>
  </br>
  <input type="radio" id="rating_radio">
  <label>Rating</label>
  </br>
  <button id="app_list_button" onclick="load_list_function()">View App List</button>
  </br>
  <p id='app_list_p'></p>
  <input type="submit" name="app_detail_button" value="View App 
                Details">
  </br>
</div>
</div>
</body>
<script>
  var app_list_bttn = document.getElementById('app_list_button');
  var radio = {};
  //List apps
  function load_list_function() {
    var xhr = new XMLHttpRequest();

    //name radio checked
    radio.sortByName = document.getElementById('name_radio').checked;
    //rating radio checked
    radio.sortByRating =
      document.getElementById('rating_radio').checked;

    document.getElementById('app_list_p').innerHTML = this.responseText;

    xhr.open('GET', 'API.php', true);
    xhr.setRequestHeader('Content-Type', 'application/json');
    xhr.send(JSON.stringify(radio));
  }
</script>

API.php

<?
$conn = mysqli_connect('localhost', '30319329', 'password', 'Application');
$errors = array();
$json_response = array();

$radio_req = file_get_contents('php://input');
$radio_result = json_decode($radio_req);

//get a list of all apps
//encode into json to send back
if ($radio_result - > sortByName == true) {
  //order by name
  $results = mysqli_query($conn, "SELECT app_name FROM App");

  while ($row = mysqli_fetch_assoc($results)) {
    // You can modify the $row here as well, or filter out certain rows
    $json_response[] = $row;
  }
  header("Content-Type: application/json");
  echo json_encode($json_response);
} else($radio_result - > sortByRating == true) {
  //order by rating
  //order by name
  $results = mysqli_query($conn, 'SELECT app_name, rating FROM App, App_Review ORDER BY rating ');

  while ($row = mysqli_fetch_assoc($results)) {
    // You can modify the $row here as well, or filter out certain rows
    $json_response[] = $row;
  }

  header("Content-Type: application/json"); echo json_encode($json_response);
}
$conn - > close();
?>

最有可能的问题是:

file_get_contents('php://radio');

那不会给你任何东西,它必须是:

file_get_contents('php://input');

此外,当您执行以下操作时:

document.getElementById('app_list_p').innerHTML = this.responseText;

您设置的innerHTML你就算做完了Ajax请求之前- this在这方面是窗口,所以this.responseText永远是不确定的。 你需要更像:

xhr.onreadystatechange = function() {
    if (xhr.readyState == XMLHttpRequest.DONE) {
        document.getElementById('app_list_p').innerHTML =xhr.responseText;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM