Problems search php Pdo

Asked

Viewed 96 times

0

I’m making a field to search but returns me some errors. My code:

class Prog{
  private $DtBase;

   public function setdata($DtBase){
    $this->data = $DtBase;
   }
   public function getdata(){
    return convertarData($this->data);
   }  

   public function convertarData($data)
   {

    $exp = explode("/",$data);
    return $exp[2].'-'.$exp[1].'-'.$exp[0].'-';
   }
}

progDAO

class ProgDAO{

private $conn;

public function __construct($connection){
    $this->conn = $connection;
}

public function Busca($obj){
        $results = array();
        $stmt = $this->conn->prepare('SELECT * FROM GTCLogist WHERE DtBase = "'.$obj->getdata().'" ');
        $stmt->execute();
        if($stmt) {
            while($row = $stmt->fetch(PDO::FETCH_OBJ)) {
                $prog = new Prog();
                $prog->setid($row->ID);
                $prog->setst($row->DsStatus);
                $prog->setcarreta($row->CdCarreta);
                $prog->setplaca($row->NrPlaca);
                $prog->setmot(stripslashes($row->DsMotorista));
                $prog->setsaida(date('d/m/Y', strtotime($row->DtSaida)));
                $prog->setorig($row->DsOrigem);
                $prog->setdest($row->DsDestino);
                $prog->setprev(date('d/m/Y', strtotime($row->DtPrevChegDest)));
                $prog->setcarga($row->DsCarga);
                $prog->setadfin($row->DsAdFin);
                $prog->setagen($row->DsAgendas);
                $prog->setmal($row->DsMalote);
                $prog->setobs($row->DsObservacao);
                $results[] = $prog;
            }
        }
    return $results;
}

  progControl

class Comando{

private $conn;

   public function __construct($connec) {
    $this->conn = $connec;
   }

   public function Busca(Comando $obj){
    $dao = new ProgDAO($this->conn);
    return $dao -> Busca($obj);
   }
}

  progPrecontrole

include_once ('../connection_open.php');

include_once ('../model/prog.php');
include_once ('progControle.php');
include_once ('../DAO/progDAO.php');

$dataPost = $_POST['data'];

$objProg = new Prog();
$objProg->setdata($dataPost);

$objComando = new Comando();
$objComando->Busca($objProg);


header ("location: ../view/busca.php");

include_once ('../connection_close.php');

   

<html>
<head>
    <title>Busca</title>
</head>
<body>
    <h1 align="center">Campo para busca</h1>
    <form action="../controller/progPrecontrole.php" method="POST" onsubmit="return valid();">
        Data: <input type="date" id="data" name="data">
        <input type="submit" class="success round button" value="Buscar"/>
    </form>
</body>
</html>

Problems:

Warning: Missing argument 1 for Comando::__Construct(), called in C: xampp htdocs Teste2 controller progPrecontrole.php on line 16 and defined in C: xampp htdocs Teste2 controller progControle.php on line 7

Notice: Undefined variable: connec in C: xampp htdocs Teste2 controller progControle.php on line 8

Catchable fatal error: Argument 1 passed to Comando::Busca() must be an instance of Comando, instance of Prog Given, called in C: xampp htdocs Teste2 controller progPrecontrole.php on line 17 and defined in C: xampp htdocs Teste2 controller progControle.php on line 11

  • 1

    The last error vc reversed the variables must pass $objComando in Busca() and not $objProg. Since progControl code is not there there is no way to tell exactly how to resolve but the error message already helps enough ;)

  • I’ll edit the question he’s there just not signaled. I’ve edited signaling, I don’t know if it helps.

1 answer

1


Missing argument 1 for Command::__Construct()

Failed to pass argument(variable or value) in constructor Comando, probably here:

$objComando = new Comando(); //<---- cadê o argumento que é a conexão com o banco?
$objComando->Busca($objProg);

Argument 1 passed to Command::Search() must be an instance of Command, instance of Prog Given

Was passed the wrong guy to Busca() must be a Comando and not a Prog according to the error message but I think its intention is to pass Prog even since this object has the method setData() for this just fix the type in the method signature.

Change:

class Comando{
   //código omitido
   public function Busca(Comando $obj){

To:

class Comando{
   //código omitido
   public function Busca(Prog $obj){

Tips not related to the main problem

  • Use Prepared statements the right way.

Change:

$stmt = $this->conn->prepare('SELECT * FROM GTCLogist WHERE DtBase = "'.$obj->getdata().'" ');

To:

$stmt = $this->conn->prepare('SELECT * FROM GTCLogist WHERE DtBase = ?');
$stmt->bindValue(1, $obj->getdata());
  • Convert the date simply and correctly(has a - the most in this method)

Change:

public function convertarData($data){
   $exp = explode("/",$data);
   return $exp[2].'-'.$exp[1].'-'.$exp[0].'-';
}

To:

 public function convertarData($data){
    $data = DateTime::createFromFormat('d/m/Y', $data);
    return $data->format('Y-m-d');
 }
  • Perfect, but reported another errorCatchable fatal error: Argument 1 passed to Comando::Busca() must be an instance of Prog, instance of Comando given, called in C:\xampp\htdocs\Teste2\controller\progPrecontrole.php on line 15 and defined in C:\xampp\htdocs\Teste2\controller\progControle.php on line 11

  • @Kevin. F vc changed the guy in the function statement?

  • Yes I put Prog envés de Comando as said above, now it has been. It only made one more mistake in the function to convert the date Fatal error: Call to undefined function convertarData() in C:\xampp\htdocs\Teste2\model\Prog.php on line 115

  • @Kevin. F in the busca() of DAO, of the print_r($obj) see what comes up

  • Something like:Prog Object ( [id:Prog:private] => [NrPlaca:Prog:private] => [data] => )

  • @Kevin. F came in white does the following in Prog after private $DtBase; add, private $data;

  • Okay, now he’s back like this [data:Prog:private] but the error persists.

  • @Kevin. F the setdata() seems not to be working properly.

  • If I take the convertarData after the return works, does not return me error.

  • @Kevin. F shows how you’re calling

  • It’s all there in the class Prog I just changed the Function as you posted up on the reply.

  • I guess I’m not calling in nor a place so the mistake.

  • @Kevin. F the problem to be elsewhere, if you follow the flow will see q $obj has the getdata() and could not give that Undefined method error.

  • Yeah, well, I didn’t detect what might be.

Show 9 more comments

Browser other questions tagged

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