使用AJAX将SQL表加载到HTML表中

时间:2017-04-03 16:20:08

标签: javascript php jquery ajax

我正在尝试使用AJAX将多个SQL查询加载到HTML表中。我一直在使用一个教程来使用AJAX将值加载到一个选项框中作为我的基础但不能让它工作。

HTML文件底部的AJAX脚本:

<script>
$(document).ready(function($) {
  var list_target_id = "tableDiv";
  var list_select_id = "year";
  var initial_target_html = "Fill in option boxes first";

  $('#'+list_target_id).html(initial_target_html);

  $('#'+list_select_id).change(function(e) {
    var year = $(this).val();
    var month = $(month).val();
    var companyID = $(company).val();

    if (year == "" && companyID == "" && month == "") {
       $('#'+list_target_id).html(initial_target_html);
    } else {
      $.ajax({url: 'ajax-getvalues2.php?month='+month+'&year='+year&companyID='+companyID,
             success: function(output) {

                $('#'+list_target_id).html(output);
            },
          error: function (xhr, ajaxOptions, thrownError) {
            alert(xhr.status + " "+ thrownError);
          }});
        }
    });
});
</script>

Ajax的getvalues2.php:

<?php  
$connection = mysqli_connect("localhost", "Query", "Query", "database1");

$companyID = mysqli_real_escape_string($connection, $_GET['companyID']);
$month = mysqli_real_escape_string($connection, $_GET['month']);
$year = mysqli_real_escape_string($connection, $_GET['year']);

$date = $year + "/" + $month + "/01";

$date1 = $date.setMonth(date.setMonth()-1);
$date2 = $date.setMonth(date.setMonth()-2);
$date3 = $date.setMonth(date.setMonth()-3);
$date4 = $date.setMonth(date.setMonth()-4);
$date5 = $date.setMonth(date.setMonth()-5);
$date6 = $date.setMonth(date.setMonth()-6);

echo     '<table id="myTable" class="table table-hover table-bordered">';

mysqli_select_db($connection, "database1");

$array = mysqli_query($connection, "SELECT DISTINCT buildingList.siteName, buildingList.siteId FROM buildingList INNER JOIN meterList ON buildingList.siteId = meterList.siteId WHERE buildingList.companyID = '$companyID' AND meterList.meterType = '$meterType'");


for (i=0, i < array.length, i++){
$siteId = $array[i];


$q2= mysqli_query($connection, "SELECT value FROM meterList WHERE siteId = '$siteId' AND meterType.meterList = '$meterType' AND date = '$date1'");
$q3= mysqli_query($connection, "SELECT value FROM meterList WHERE siteId = '$siteId' AND meterType.meterList = '$meterType' AND date = '$date2'");
$q4= mysqli_query($connection, "SELECT value FROM meterList WHERE siteId = '$siteId' AND meterType.meterList = '$meterType' AND date = '$date3'");
$q5= mysqli_query($connection, "SELECT value FROM meterList WHERE siteId = '$siteId' AND meterType.meterList = '$meterType' AND date = '$date4'");
$q6= mysqli_query($connection, "SELECT value FROM meterList WHERE siteId = '$siteId' AND meterType.meterList = '$meterType' AND date = '$date5'");
$q7= mysqli_query($connection, "SELECT value FROM meterList WHERE siteId = '$siteId' AND meterType.meterList = '$meterType' AND date = '$date6'");

echo '<td>' array[i] "</td>";
echo '<td contenteditable='true' type="text"  onkeyup="consumption()">"  "</td>";
echo '<td>' .$row['q2']. '</td>';
echo '<td></td>';
echo '<td>' .$row['q3']. '</td>';
echo '<td></td>';
echo '<td>' .$row['q4']. '</td>';
echo '<td></td>';
echo '<td>' .$row['q5']. '</td>';
echo '<td></td>';
echo '<td>' .$row['q6']. '</td>';
echo '<td></td>';
echo '<td>' .$row['q7']. '</td>';
echo '<td></td>';

}

echo "</table>";

mysqli_free_result($result);
mysqli_close($connection);

?>

月份应该输入到php文件中作为“01”的1月和年份应该输入为2016年的“2016”。然后它应该合并给出一个日期,如年/月/ 01。

我不确定我哪里出错了,但我可以请一些建议。在此先感谢:)

1 个答案:

答案 0 :(得分:0)

您的脚本无法编译,因此您无法查看任何内容。例如,行:

echo '<td>' array[i] "</td>";

应该是:

echo '<td>' . array[i] . "</td>";

他们可能是其他错误。打开PHP错误的错误报告的方法是:

error_reporting(E_ERROR | E_WARNING | E_PARSE | E_COMPILE_ERROR | E_STRICT);

在你的剧本中第一个\&lt; \?php之后立即加上一行。