簡體   English   中英

使用 getJson 的 php 查詢變量

[英]variable on php query with getJson

我最近問了一個問題,但我的網站仍然有問題:

我有一個帶有對 SQL 服務器的查詢的 php,從 html 中獲取一個 var,但我需要知道是否必須使用某種方法將 var 發送到 php? 我的意思是,好的 json,我在文本字段上寫了日期,但它沒有得到 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

但如果我排除$shiftdate = $_GET["shiftdate"]; 以及我將其提供給查詢的部分我沒有任何錯誤。

問題僅在這一行$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>

您需要考慮幾件事。

每次需要數據時都連接是一個非常糟糕的主意。 這將使您的代碼更難維護。 我建議使用數據庫包裝器。

此外,在您的查詢中,您應該使用 $_GET['variable'] 的清理版本。

示例: 消毒日期

如果您沒有所討論的 $_GET['variable'] 會發生什么? 更多錯誤。 您可以使用

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

我建議您使用 odbc_prepare 和 odbc_execute 進行查詢。 它更安全,並使您的代碼更具可讀性。

php.net 上的 odbc_prepare

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM