Problem with PHP code

-1

I have a code that should show me certain data according to the date that the user sends by form, that is, I need to be shown a certain number of orders in a certain range of dates but it turns out that the table that I am shown this empty, attached form code and who performs the action:

<html>
<body style="background-color:#47D4A2;">
<h1 align=center> Tomy Resfrescos S.A</h1>
<h2> Listados de pedidos </h2>
<h4> Ingrese rango de fechas: </h4>
<form action="http://localhost/prueba/ResultadoDePedidos.php"   method="GET"id="formulario">    
Fecha inicial:<input  name="fecha1" type="date">
<br>
Fecha final:<input name="fecha2" type="date">
<br>
<input type="submit" value="continuar"/>
</form>
</body>
</html>

and this is the other:          

<?php
include("conexion.php");
$fecha1=$_GET['fecha1'];
$fecha2=$_GET['fecha2'];
$link=Conectarse();
$result=mysql_query("SELECT num_ped,fec_rep FROM pedidos WHERE fec_rep>= 
$fecha1 AND fec_rep<= $fecha2",$link);
?>                  
<TABLE BORDER=1 CELLSPACING=1
CELLPADDING=1>
<TR><TD>&nbsp;Numero de Pedido</TD><TD>&nbsp;Fecha de Entrega&nbsp;</TD> </TR>
<?php
while($row= mysql_fetch_array($result)) {
printf("<tr>
<td>&nbsp;%s</td>
<td>&nbsp;%s&nbsp;</td>
</tr>",
$row["0"],$row["1"]);
}
mysql_free_result($result);
mysql_close($link);
?>  
</table> 
</body>
</html>

The problem is that the table with the names of the columns is shown, the connection is successful but the table is empty and I do not understand why.

    
asked by sebastian 29.11.2016 в 23:14
source

3 answers

0

Modify your php file with this code, with this it should be solved, and I recommend that you use the POST method, it is safer than GET :

<?php
include("conexion.php");
$fecha1=$_GET['fecha1'];
$fecha2=$_GET['fecha2'];
$link=Conectarse();
$result=mysql_query("SELECT num_ped,fec_rep FROM pedidos WHERE fec_rep BETWEEN '$fecha1' AND '$fecha2' ",$link);
?>  

<!doctype html>
<html>
<head>
<meta charset="utf-8">
</head>

<body>                
<TABLE BORDER=1 CELLSPACING=1 CELLPADDING=1>
<TR>
<TD>&nbsp;Numero de Pedido</TD>
<TD>&nbsp;Fecha de Entrega&nbsp;</TD> 
</TR>
<?php

while ($row = mysql_fetch_row($result)){
echo "<tr>
<td>'".$row[0]."'</td>
<td>'".$row[1]."'</td>
</tr>";
                    } 

mysql_free_result($result);
mysql_close($link);
?>  
</table> 
</body>
</html>
    
answered by 30.11.2016 в 00:22
0

Try changing your query this way:

SELECT num_ped,fec_rep FROM pedidos WHERE fec_rep >= STR_TO_DATE($fecha1, '%d/%m/%Y') AND fec_rep <= STR_TO_DATE($fecha2, '%d/%m/%Y')
    
answered by 30.11.2016 в 15:12
0

If you are using MYSQL the default format of the date is YYYY-MM-DD and the format in which you are sending the date you send is dd / mm / yyyy so you have to change the format. If you are using a calendar for the selection of the date you can change the format there but change the format of the date from php Ex:

$a_fecha1 = explode("/",$fecha1);
$ffecha1 = $a_fecha1[2]."-".$a_fecha1[1]."-".$a_fecha1[0] 
    
answered by 16.12.2016 в 18:27