Code problem ( Find values between a date and another in the database )

Asked

Viewed 87 times

0

Okay, here’s the problem. I’m working on a code that picks up a series of flights that are available between a date and another date entered by the user. Dates are entered through the datepicker in js, but I think that part is working well and is not related to the problem. However I have not had success yet, the data is never returned. I have already used the if conditions to change the return of the data but it is still in error. I think it has to see the $_GET parameter misapplied. I would very much appreciate your support if further information is required and ask for coolocarei.

 <?php

if (isset($_GET["botao"])){
$parms = array();
$filter = array();

$query = "SELECT V.ID FROM voo as V WHERE ";
    if (isset($_GET["origem"]) && !empty($_GET["origem"])) {
        $filter[] = "(V.IDLocalOrigem=?)";
        $parms[] = $_GET["origem"];
    }

    $query = "SELECT V.ID FROM voo as V WHERE ";
    if (isset($_GET["destino"]) && !empty($_GET["destino"])) {
        $filter[] = "(V.IDLocalDestino=?)";
        $parms[] = $_GET["destino"];
    }

    $query = "SELECT V.ID FROM voo as V WHERE ";
    if (isset($_GET["data"]) && !empty($_GET["data"])) {
        $filter[] = "(V.DataPartida>=?)";
        $parms[] = $_GET["data"];
    }

    $query = "SELECT V.ID FROM voo as V WHERE ";
    if (isset($_GET["entre"]) && !empty($_GET["entre"])) {
        $filter[] = "(V.DataChegada<=?)";
        $parms[] = $_GET["entre"];
    }

}       


        $query .= explode(' AND ',$filter);

   ?>       

        <pre><?php var_dump($filter); ?></pre>
  <?php



    /*$query= "select V.ID, V.IDLocalOrigem, L1.Nome, V.IDLocalDestino,".
    " L2.Nome, V.IDAviao, V.DataPartida, V.DataChegada,".
    " V.PrecoBilhete, V.Estado, L1.Nome as nomeOrigem, L2.Nome as nomeDestino".
    " FROM Voo as V".
    " INNER JOIN Local as L1 ON V.IDLocalOrigem = L1.ID".
    " INNER JOIN Local as L2 ON V.IDLocalDestino = L2.ID".
    " WHERE ((V.IDLocalOrigem=?) OR (? is NULL)) AND".
    " ((V.IDLocalDestino=?) OR (? is NULL)) AND".
    " ((V.DataPartida>=?) OR (? is NULL)) AND".
    " ((V.DataPartida<=?) OR (? is NULL)) AND".
    " ((V.PrecoBilhete=?) OR (? is NULL)) AND".
    " ((V.Estado=?) OR (? is NULL))";*/


      // RESOLVER: Pesquisar todos os voos entre datas
    $stmt = $mydb->prepare($query);
    $Origem = !empty($_GET["origem"]) ? $_GET["origem"] : null;
    $Destino =!empty($_GET["destino"]) ? $_GET["destino"] : null;
    $DataPartida = null;
    if (!empty($_GET["data"])){
        $DataPartida = $_GET["data"].' 00:00:00';   
    }
    if (!empty($_GET["entre"])){
        $DataChegada = $_GET["entre"].' 23:59:59';  
    }

    $Preco = null;

    $Estado = null;

    $stmt->bind_param("iiii", $Origem, $Destino, 
    $DataPartida $DataChegada);

    $stmt->execute();


    $result = $stmt->get_result();
    $todosDados = $result->fetch_all(MYSQL_ASSOC);

    foreach($todosDados as $r   )
        {
        echo "<h1>Ida</h1>";

        echo "<table border=1>";
        echo "<tr>";
        echo "<td>";

        echo "Origem:".$r["nomeOrigem"]."</br>";
        echo "Destino:".$r["nomeDestino"]."</br>";
        echo "DataPartida:".$r["DataPartida"]."</br>";
        echo "DataChegada:".$r["DataChegada"]."</br>";
        echo "Preço:".$r["PrecoBilhete"]."</br>";
        echo "Estado:".$r["Estado"]."</br>";
        echo '<a href="lugares.php?idvoo='.$r["ID"].'" class="btn btn-primary">Comprar</a>';

        echo "</td>";
        echo "</tr>";
        echo "</table>";
        }   

    $result->free();


    $stmt->bind_param("iiii", $Origem, $Destino, 
    $DataPartida, $DataChegada);

    $stmt->execute();


    $result = $stmt->get_result();
    $todosDados = $result->fetch_all(MYSQL_ASSOC);




    foreach($todosDados as $r   )
        {
        echo "<h1>Ida</h1>";

        echo "<table border=1>";
        echo "<tr>";
        echo "<td>";

        echo "Origem:".$r["nomeOrigem"]."</br>";
        echo "Destino:".$r["nomeDestino"]."</br>";
        echo "DataPartida:".$r["DataPartida"]."</br>";
        echo "DataChegada:".$r["DataChegada"]."</br>";
        echo "Preço:".$r["PrecoBilhete"]."</br>";
        echo "Estado:".$r["Estado"]."</br>";
        echo '<a href="lugares.php?idvoo='.$r["ID"].'" class="btn btn-primary">Comprar</a>';

        echo "</td>";
        echo "</tr>";
        echo "</table>";
        }   

    $result->free();
    $stmt->close();
?>

$filter displays this var_dump:

array(4) {
  [0]=>
  string(19) "(V.IDLocalOrigem=?)"
  [1]=>
  string(20) "(V.IDLocalDestino=?)"
  [2]=>
  string(18) "(V.DataPartida>=?)"
  [3]=>
  string(18) "(V.DataChegada<=?)"
}
  • What’s the problem? Not returning query data?

  • @Lucio Rubens Exactly, never returns data.

  • Set your variable $query

  • @Lúcio Rubens What do you mean? print_r or echo $query?

  • The code, because it must be a problem with the query, and you already start with: $stmt = $mydb->prepare($query);

  • @Lúcio Rubens I already posted the value of the query. Does the problem come from there? I will check.

  • @angelfmf the problem is this query. and how about creating the query dynamically depending on whether each parameter is filled in or not, instead of using this: ((V.DataPartida>=?) OR (? is NULL))? Have you confirmed if this even works?

  • @Blunt How do I create the query this way, you can give an example?

  • @angelfmf was implode instead of explode. I was wrong...

  • @angelfmf suggest a print to $query after implode. you have an unnecessarily duplicated line $query = "SELECT V.ID FROM voo as V WHERE ";

  • This is the print of the query you suggested: SELECT V.ID FROM flight as V WHERE (V.Idlocalorigin=?) AND (V.Idlocaldestination=?) AND (V.Datadeparture>=?) AND (V.Arrival<=?) Seems to be quite right?

  • @angelfmf Certissimo! bind_param is wrong because in this case there are only 4 variables

  • @Blunt The error continues to appear " mysqli_stmt::bind_param(): Number of Elements in type Definition string doesn’t match number of bind variables, I have tried modifying bind_param in several ways but it always gives the same error. I don’t understand what I’m doing wrong

  • @angelfmf what you are doing should be adding more parameters in the bind_param than those being defined in the query. You need to create another array for the parameters, just like $filter, and then add the values to that array, and then call bind_param based on that array. that way you will always have the correct number of fields.

  • @Blunt I’ve been debugging and seeing where there would be more trouble and when I did in $todosDados = $result->fetch_all(MYSQL_ASSOC) or trigger_error($result->error); this error appears: "Undefined Property: mysqli_result::$error"

Show 10 more comments

1 answer

1

Here is an example of how to mount the dynamic query:

$parms = array();
$filter = array();
$query = "(...) WHERE ";
if (isset($_GET["origem"]) && !empty($_GET["origem"])) {
    $filter[] = "(V.IDLocalOrigem=?)";
    $parms[] = $_GET["origem"];
}

[fazer o mesmo para os restantes parametros]

$query .= implode(' AND ', $filter);

[executar o query com os parametros que foram preenchidos]

Note that the code has not been tested, there may be some error.

  • Don’t worry, I’ll work with the fornceceu right away. Thank you very much*

  • Okay, I’ve written the odigo (edited above) but I can’t get past the explode function, gives a Warning that says "$filter" has to be string.

  • @angelfmf is implode, I got it wrong. I’ll fix it

  • Thanks, I fixed it, I’ve been researching and added the implode only to $filter but now the code continues. However there are still errors, I did not execute well the dynamic query. I var_dump the filter values, it may help to see if you are getting the right values. I will edit in the question

  • @angelfmf not forgetting that bind_param no longer takes all parameters, but only those that were filled in the query.

  • Only one hint: if the array $filter not be incremented, your SQL would be SELECT (campos) FROM tabela WHERE, i.e., syntax error. Initialize your variable $query with $query = "(...) WHERE 1=1 "; (case $filter not be increased, brings all.

  • @mend3 yes, when I made the code I was headfirst, I didn’t even think about it. the alternative is just put the Where case count($filter) > 0

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.