使用getJson查询php上的变量

variable on php query with getJson

本文关键字:变量 php getJson 查询 使用      更新时间:2023-09-26

我最近问了一个问题,但我的网站上仍然有问题:

我有一个php,其中包含一个到SQL服务器的查询,从html中获取一个var,但我需要知道是否必须将带有某种方法的var发送到php?我的意思是,json在ok中,我在文本字段上写了日期,但它没有得到php结果。我需要插入一些按钮来在插入日期后调用它,或者它应该是自动的,只插入var?

这就是php。我把变量放在WHERE的最后一个声明中。

<?php
function getArraySQL(){
$dsn = "prueba";
$connect = odbc_connect( $dsn, '', '' );
$shiftdate = $_GET["shiftdate"];  //one of the variables that i need to input on my query
$query = "  SELECT hist_statusevents.reason, Sum(hist_statusevents.duration/3600) AS 'Duracion'
FROM hist_statusevents, hist_eqmtlist, hist_exproot
WHERE hist_exproot.shiftindex = hist_statusevents.shiftindex AND hist_statusevents.shiftindex = hist_eqmtlist.shiftindex AND hist_statusevents.eqmt = hist_eqmtlist.eqmtid AND (hist_eqmtlist.eqmtid='SVEDALA') AND hist_statusevents.category In ('2') AND hist_exproot.ddmmyy =$shiftdate
GROUP BY hist_statusevents.reason
ORDER BY Duracion DESC";
if(!$rs = odbc_exec($connect, $query)) die();
$rawdata = array();
$i=0;
while($row = odbc_fetch_array($rs))
{
$rawdata[$i] = $row;
$i++;
}
odbc_close( $connect );
return $rawdata;
}
$myarray = getArraySQL();
echo json_encode($myarray);

在另一边,我的html:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>Report Monitor</title>
  <link rel="stylesheet" href="css/bootstrap.min.css">
  <link rel="stylesheet" href="css/estilos.css">
  <script src='js/jquery.js'></script>
  <script src='js/bootstrap.js'></script>
</head>
<body>
  <header>
    <nav class="navbar navbar-inverse navbar-static-top" role="navigation">
    </nav>
  </header>
  <section class="main container">
    <div class="row">
      <section class="post col-md-12">
        <div class="miga-de-pan">
          <ol class="breadcrumb">
            <li><a href="#">Inicio</a>
            </li>
            <li><a href="#">Inputs</a>
            </li>
            <li class="active">Reports</li>
          </ol>
        </div>
      </section>
      <section class="post col-md-12">
        <input type="text" placeholder="Fecha del Turno" id="shiftdate">
      </section>
      <div class="row">
        <div class="clas col-md-4">
          <h4>Indicador 1</h4>
          <script>
            $(document).ready(function() {
              shiftdate = $('#shiftdate').val()
              $.getJSON('dbquery_plus_shiftdate.php', {
                shiftdate: shiftdate
              }, function(data) {
                $.each(data, function(key, val) {
                  $('ul').append('<li Reason="' + val.reason + '">' + val.reason + '-' + val.Duracion + '</li>');
                });
              });
            });
          </script>
          <ul></ul>
        </div>
        <div class="clas col-md-4">
          <h4>Indicador 2</h4>	
          <script>
            $(document).ready(function() {
              $.getJSON('dbquery.php', function(data) {
                $.each(data, function(key, val) {
                  $('ul2').append('<li Reason="' + val.reason + '">' + val.reason + '-' + val.Duracion + '</li>');
                });
              });
            });
          </script>
          <ul2></ul2>
        </div>
        <div class="clas col-md-4">
          <h4>Indicador 3</h4>	
          <script>
            $(document).ready(function() {
              $.getJSON('dbquery.php', function(data) {
                $.each(data, function(key, val) {
                  $('ul3').append('<li Reason="' + val.reason + '">' + val.reason + '-' + val.Duracion + '</li>');
                });
              });
            });
          </script>
          <ul3></ul3>
        </div>
      </div>
    </div>
  </section>
  <footer></footer>
</body>
</html>

第一个json函数是我试图插入参数shifdate的函数。

并且shifdate被插入到节类上:

      <section class="post col-md-12">
        <input type="text" placeholder="Fecha del Turno" id="shiftdate">
      </section>

另外,php在chrome上给了我这个结果:

Notice: Undefined index: shiftdate in C:'xampp'htdocs'byp1'dbquery_plus_shiftdate.php on line 5
Warning: odbc_exec(): SQL error: [Microsoft][SQL Server Native Client 10.0][SQL Server]Executing SQL directly; no cursor., SQL state 01000 in SQLExecDirect in C:'xampp'htdocs'byp1'dbquery_plus_shiftdate.php on line 12

但是如果我排除CCD_ 1和我将其提供给查询的部分,我就没有任何错误。

您需要考虑以下几点。

每次需要数据时进行连接是一个非常糟糕的主意。这将使您的生活更难维护代码。我建议使用数据库包装器。

此外,在您的查询中,您应该使用$_GET['variable']的净化版本。

示例:对日期进行消毒

如果你没有问题中的$_GET[变量],会发生什么?更多错误。你可以使用

if (isset($_GET['variable'])) 
    $var = $_GET['variable'];
else 
    $variable = "some default";

我建议您对查询使用odbc_prepare和odbc_execute。它更安全,使代码更可读。

php.net 上的odbcprepare

问题只在这一行$shiftdate = $_GET["shiftdate"];

您会得到错误,因为$_get没有shiftdate键。

首先,更改此

<section class="post col-md-12">
    <input type="text" placeholder="Fecha del Turno" id="shiftdate">
</section>

到这个

<form method="GET" class="post col-md-12">
    <input type="text" placeholder="Fecha del Turno" id="shiftdate" name="shiftdate">
</form>