Why do not you bring me the data of the sql query?

0

Good afternoon I'm doing a query on mysql given a date bring me the year and month and the reference for that month, the query is good but it's not bringing me anything, someone to help me find the error.

<?php
//comprobamos que las fechas existan
if(isset($_POST["fecha_desde"])){
   $fecha_desde = $_POST["fecha_desde"];
}
if (isset ($_POST["fecha_hasta"])){
   $fecha_hasta = $_POST["fecha_hasta"]; 
   $fecha_desde = strtotime($fecha_desde);
   $fecha_hasta = strtotime($fecha_hasta);
   $fecha_desde = date('Y-m',$fecha_desde);
   $fecha_hasta = date('Y-m',$fecha_hasta);

   $rs = mysqli_query($mysqli,"SELECT referencia, DATE_FORMAT(fecha_pedido, '%Y%m'), SUM(cantidad), COUNT(referencia) FROM pedidos_detalle INNER JOIN pedidos ON pedidos_detalle.id_pedido = pedidos.id WHERE $_POST[fecha_desde] = DATE_FORMAT(fecha_pedido, '%Y%m') BETWEEN '$fecha_desde' AND '$fecha_hasta' GROUP BY referencia, DATE_FORMAT(fecha_pedido, '%Y%m'), ORDER BY referencia, DATE_FORMAT(fecha_pedido, '%Y%m') ASC");
   while ($row = mysqli_fetch_row($rs)){
       echo "<option value='$row[0]'>$row[0], $row[1]</option>"; 
?> 
    
asked by Christian Dagnover 23.06.2016 в 21:09
source

3 answers

3

In addition to closing the if and the while, check the query, because where is poorly structured.

 WHERE $_POST[fecha_desde] = DATE_FORMAT(fecha_pedido, '%Y%m') BETWEEN '$fecha_desde' AND '$fecha_hasta'

You have left an AND and a variable, or that condition is not possible

    
answered by 25.06.2016 / 21:29
source
1
SELECT 
p.referencia, 
DATE_FORMAT(pd.fecha_pedido, '%Y%m') as 'fecha', 
SUM(pd.cantidad), 
COUNT(p.referencia) 
FROM pedidos_detalle pd INNER JOIN pedidos p
ON pd.id_pedido = p.id
WHERE pd.fecha_pedido >= $fecha_desde 
AND pd.fecha_pedido  <= $fecha_hasta
GROUP BY p.referencia, fecha
ORDER BY fecha asc;

You could publish the structure of your tables to see it better. And try the bd directly if something returns your query with the dates that you designate. Inside your query, you make $ _POST [date_of_from] but you have already entered it in $ date_from.

    
answered by 23.06.2016 в 21:38
1

You have 2 brackets without closing in your code, One of an IF and the other of the WHILE. Start by closing them, or if you already have them right post the correct code.

    
answered by 23.06.2016 в 21:43