Validation with PHP and MYSQL

0

I want to make a report count by the status in which they are, I have two queries one is

SELECT 
orden_trabajo.status, COUNT(id_orden) AS cuantas 
FROM orden_trabajo  
ORDER BY 
status;

The result of this gives me the total number of orders that is 46

the other query is

SELECT 
orden_trabajo.status, COUNT(id_orden) AS cuantas 
FROM  
orden_trabajo where (fecha >= ? AND fecha <= ? ) 
ORDER BY 
status;

The result of this throws me depending on the date.

What I wanted is to join the two queries in a condition so that depending validate if the fields in the form are empty or full, which are two date1 and date2

the code stayed the following way

 function total($fecha1,$fecha2){
   include("dbconnect.php");

   if ($_POST('#fecha1').val() === '' || $_POST('#fecha2').val() === '') { 
  ##### no se si en esta parte de la validacion estoy bien ########

   $sql = $conn->prepare("SELECT orden_trabajo.status, COUNT(id_orden) AS    
   cuantas FROM orden_trabajo  ORDER BY status ");
   $sql->execute();
 if ($sql->rowCount () > 0){
   while($rows=$sql->fetch(PDO::FETCH_ASSOC)){
   echo "<strong><h2>El numero total de Ordenes: </h2></strong>";
   echo "<h1>".$rows['cuantas']."</h1>";

  }
 }else{
  echo "";
 }

 }else{
 $sql = $conn->prepare("SELECT orden_trabajo.status, COUNT(id_orden) AS 
 cuantas FROM orden_trabajo where (fecha >= ? AND fecha <= ? ) ORDER BY 
  status ");
   $sql->bindParam(1,$fecha1, PDO::PARAM_INT);
   $sql->bindParam(2,$fecha2, PDO::PARAM_INT);
   $sql->execute();
 if ($sql->rowCount () > 0){
    while($rows=$sql->fetch(PDO::FETCH_ASSOC)){
    echo "<strong><h2>El numero total de Ordenes: </h2></strong>";
    echo "<h1>".$rows['cuantas']."</h1>";

    }
  }else{
   echo "";
  }
 }
}

In the part highlighted with black, I'm not sure if I sent the fields of the form, because in that part I send an error, I would like to know if you could advise me

    
asked by Eddy Olvera 01.02.2018 в 19:34
source

1 answer

0
 function total($fecha1,$fecha2){
   include("dbconnect.php");

   $fecha1 = $_POST['fecha1'];
   $fecha2 = $_POST['fecha2']; 
   if ($fecha1 == null || $fecha2== null) {
   $sql = $conn->prepare("SELECT orden_trabajo.status, COUNT(id_orden) AS    
   cuantas FROM orden_trabajo  ORDER BY status ");
   $sql->execute();
   if ($sql->rowCount () > 0){
   while($rows=$sql->fetch(PDO::FETCH_ASSOC)){
   echo "<strong><h2>El numero total de Ordenes: </h2></strong>";
   echo "<h1>".$rows['cuantas']."</h1>";

   }
   }else{
     echo "";
   }

   }else{
   $sql = $conn->prepare("SELECT orden_trabajo.status, COUNT(id_orden) AS 
   cuantas FROM orden_trabajo where (fecha >= ? AND fecha <= ? ) ORDER BY 
   status ");
   $sql->bindParam(1,$fecha1, PDO::PARAM_INT);
   $sql->bindParam(2,$fecha2, PDO::PARAM_INT);
   $sql->execute();
  if ($sql->rowCount () > 0){
    while($rows=$sql->fetch(PDO::FETCH_ASSOC)){
    echo "<strong><h2>El numero total de Ordenes: </h2></strong>";
    echo "<h1>".$rows['cuantas']."</h1>";

     }
    }else{
     echo "";
    }
   }
  }
    
answered by 01.02.2018 в 20:42