Reputation: 135
I recently asked for a problem, but I still with troubles on my site:
I have a php with a query to a SQL server, gettin a var from html, but i need to know if I have to send the var with some method to the php? i mean, the json in ok, I write the date on a text field, but it doesn get the php result. I need to insert some button to call it after insert the date or it should be automatic just inserting the var?
this is the php. I put the variable on the last declaration on 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);
On the other side, my 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>
The first json function is the one where im trying to insert the parameter shifdate.
And the shifdate is inserted on the section class:
<section class="post col-md-12">
<input type="text" placeholder="Fecha del Turno" id="shiftdate">
</section>
additionally the php gives me this result on 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
but if i exclude $shiftdate = $_GET["shiftdate"];
and the part where i give it to the query i don have any error.
Upvotes: 2
Views: 117
Reputation: 1890
The problem is only on this line
$shiftdate = $_GET["shiftdate"];
You get the error, because $_GET doesn't have a shiftdate key.
For a start, change this
<section class="post col-md-12">
<input type="text" placeholder="Fecha del Turno" id="shiftdate">
</section>
to this
<form method="GET" class="post col-md-12">
<input type="text" placeholder="Fecha del Turno" id="shiftdate" name="shiftdate">
</form>
Upvotes: 1
Reputation: 140
There are several things you need to consider.
Connecting every time you need data is a really bad idea. This will make your life harder maintaining the code. I recommend using a database wrapper.
Also, In your query, you should be using a sanitized version of the $_GET['variable'].
Example: Sanitizing a Date
And what would happen if you didn't have the $_GET['variable'] in question? More errors. You can use
if (isset($_GET['variable']))
$var = $_GET['variable'];
else
$variable = "some default";
I would recommend using odbc_prepare and odbc_execute for your queries. It is safer and makes your code more readable.
Upvotes: 1