Search by date in PHP

1

Good morning, I have the following query that makes me a query by dates to SQL SERVER:

            $query="SELECT art_Clave, art_Costo, kar_Fecha, kar_Cantidad, kar_Origen, alma_Existencia FROM tArticuloKardex WHERE art_Clave='".$buscar."' AND (kar_Fecha BETWEEN '".$fecha1."' AND '".$fecha2."')";
        $resultado=sqlsrv_query($conn, $query);
        //se desplegaran los resultados en la tabla
        echo "<table border=1>";
        echo "<tr>";
        echo "<th>SKU</th>";
        echo "<th>COSTO</th>";
        echo "<th>FECHA DE MOVIMIENTO</th>";
        echo "<th>PIEZAS</th>";
        echo "<th>TICKET</th>";
        echo "<th>EXISTENCIA FINAL</th>";
        echo "</tr>";
        while($row=sqlsrv_fetch_array($resultado)){
            echo '<tr>';
            echo '<td>'.$row['art_Clave'].'</td>';
            echo '<td>'.$row['art_Costo'].'</td>';
            echo '<td>'.$row['kar_Fecha'].'</td>';
            echo '<td>'.$row['kar_Cantidad'].'</td>';
            echo '<td>'.$row['kar_Origen'].'</td>';
            echo '<td>'.$row['alma_Existencia'].'</td>';
            echo '</tr>';           
        }
        echo "</table>";

And I pass the information to you by the following form:

<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="POST" name="buscar">
        <input type="text" name="buscar" id="buscar" placeholder="Buscar:">
        <input type="date" name="fecha1" id="fecha1">
        <input type="date" name="fecha2" id="fecha2">
        <input type="submit" name="enviar" value="Buscar">
    </form>

And he sends me the following warning:

  

Warning: sqlsrv_fetch_array () expects parameter 1 to be resource,   boolean given in C: \ xampp \ htdocs \ sana_php \ kardex.php on line 37   SKU COST DATE OF MOVEMENT PIECES TICKET FINAL EXISTENCE

They could help me. THANK YOU

I forgot, the data in the database is of the DateTime type and the way I pass through POST is the following:

if($_SERVER['REQUEST_METHOD'] == 'POST'){
        $buscar=filter_var($_POST['buscar'], FILTER_SANITIZE_STRING);
        $fecha1=$_POST['fecha1'];
        $fecha2=$_POST['fecha2'];

On the other hand, if I put it in the input as text instead of date, I get the following error:

Catchable fatal error: Object of class DateTime could not be converted to string in C: \ xampp \ htdocs \ sana_php \ kardex.php on line 41

    
asked by Guillermo Ricardo Spindola Bri 24.05.2016 в 18:16
source

2 answers

1

An additional option to the answer of Gustavo Piris is to add the conversion of the text corresponding to the date in the parameter to the date format expected by the configuration of the SQL Server.

$query="SELECT art_Clave, art_Costo, kar_Fecha, kar_Cantidad, kar_Origen, alma_Existencia FROM tArticuloKardex WHERE art_Clave= ? AND (kar_Fecha BETWEEN convert(varchar(10),?, 103) AND convert(varchar(10),?, 103))";

In that case assuming you want to ensure the date format defined by the code 103 according to the table in Cast / Convert from SQL Server.

    
answered by 24.05.2016 в 22:48
0

Proof of the next. way

 $query="SELECT art_Clave, art_Costo, kar_Fecha, kar_Cantidad, kar_Origen, alma_Existencia FROM tArticuloKardex WHERE art_Clave= ? AND (kar_Fecha BETWEEN ? AND ?)";

$params = array($buscar, $fecha1, $fecha2); 
$resultado = sqlsrv_query($conn, $query, $params); 
    
answered by 24.05.2016 в 18:50