我最近提出了一个问题,但我的网站仍然遇到问题:
我有一个对 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 的函数。
并且将转换日期插入节类中:
<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"];
以及我将其提供给查询的部分,则不会有任何错误。
最佳答案
您需要考虑几件事。
每次需要数据时都进行连接是一个非常糟糕的主意。这将使您的代码维护更加困难。我建议使用数据库包装器。
此外,在您的查询中,您应该使用 $_GET['variable'] 的清理版本。
如果您没有相关的 $_GET['variable'] 会发生什么?更多错误。您可以使用
if (isset($_GET['variable']))
$var = $_GET['variable'];
else
$variable = "some default";
我建议您使用 odbc_prepare 和 odbc_execute 进行查询。它更安全,并使您的代码更具可读性。
关于javascript - 使用 getJson 进行 php 查询的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31142066/