【问题标题】:variable on php query with getJson使用 getJson 进行 php 查询的变量
【发布时间】:2015-09-17 11:22:24
【问题描述】:

我最近提出了一个问题,但我的网站仍然遇到问题:

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

这是 php.ini 文件。我将变量放在 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 类中:

      <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

但如果我排除 $shiftdate = $_GET["shiftdate"]; 以及我将其提供给查询的部分,我没有任何错误。

【问题讨论】:

    标签: javascript php json html twitter-bootstrap


    【解决方案1】:

    问题只在这一行 $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>
    

    【讨论】:

    • 但是当我这样做时,导航器给了我这个错误:localhost:8080/byp1/GET?shiftdate=11-11-01
    • 我的错,我仍然习惯 Stack over Flow。谢谢你的耐心。找不到对象!在此服务器上找不到请求的 URL。引用页面上的链接似乎是错误的或过时的。请将该错误通知该页面的作者。如果您认为这是服务器错误,请联系网站管理员。错误 404 localhost Apache/2.4.12 (Win32) OpenSSL/1.0.1l PHP/5.6.8
    • 并且表单代码在我的 url 上生成 GET?shifdate
    • 对不起,action 属性应该替换为 method="GET"。很抱歉!
    【解决方案2】:

    您需要考虑几件事情。

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

    另外,在您的查询中,您应该使用经过清理的 $_GET['variable'] 版本。

    示例:Sanitizing a Date

    如果您没有问题的 $_GET['variable'] 会发生什么?更多错误。你可以使用

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

    我建议您使用 odbc_prepare 和 odbc_execute 进行查询。它更安全,使您的代码更具可读性。

    odbc_prepare on php.net

    【讨论】:

    • 也许我应该重新定义我接受这个项目的方式。问题是,我的企业有这个连接到 sql server 以生成报告的 excel 解决方案,我的老板要求我开发一个解决方案,从 excel 迁移到任何其他允许制作仪表板并显示 KPI 的应用程序。考虑到我不了解信息,我开始调查并决定我必须通过 php odbc 连接到数据库并在 html 上打印查询结果,此时我所拥有的就是你所看到的,但根据你告诉我的,也许我应该研究另一种方式来思考这个解决方案
    • 并且有很多查询,所以我可能每次尝试连接都会遇到一些麻烦...顺便说一句,我在尝试开发此解决方案时所获得的知识量令人难以置信,即使我还没有任何功能......
    • 只需使用一个类来连接您的数据库。重用代码并正确维护它非常容易。
    • 很抱歉,你说的课程是什么意思?
    • 即使其他答案解决了一个准时的问题,你也会向我解释我的问题的基本情况
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-28
    • 2016-01-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多