Recovers values using classes and Pdo without going through the url

Asked

Viewed 126 times

1

Good afternoon, I have a class where I pass all the methods insert, update, delete, consulta(fechall) and consulta(fech($id))

Below I show the class of consulta(fech($id))

public function selId($id){
    $sqlId = "SELECT * FROM web_cadcli WHERE idcad_cliente = ':id_us', nom_cliente = $this->getNome($nome), ema_cliente = $this->getEmail($email), senha_email = $this->senha($senha)  ";
    $sqlId = $this->con->conectar()->prepare($sqlId);
    $sqlId->bindParam(':id_us', $this->cod,   PDO::PARAM_INT);
    $sqlId->bindParam(1,        $this->nome,  PDO::PARAM_STR);
    $sqlId->bindParam(2,        $this->email, PDO::PARAM_STR);
    $sqlId->bindParam(3,        $this->senha, PDO::PARAM_STR);
    $sqlId->execute();
    return ($sqlId->fetch(PDO::FETCH_ASSOC));
    }//retorna o Id do Usuario - testar

I want to search values in the database with ID and later recover in my form

Below I show my form with the code where I need to recover the values.

<?PHP
        $Objeto = new Usuario(); 
        $usuario = $Objeto->selId(); ?><!--SELECIONO O ID DO METODOS selId-->

          <input name="id_ui" type="hidden" value="id_usu"/>
          <div class="form-group">

            <label for="exampleInputEmail1">Nome</label>
            <input type="nome" class="form-control" id="exampleInputEmail1" value="<?php $usuario->setNome($nome); ?>">
          </div>

          <div class="form-group">
            <label for="exampleInputPassword1">Email</label>
            <input type="email" class="form-control" id="exampleInputPassword1" value="<?php $usuario->setNome($nome) ?>">
          </div>

          <div class="form-group">
            <label for="exampleInputFile" disable>Imagem</label>
            <input type="file" id="exampleInputFile">
           </div>

          </form>
  • Methods set() usually do not return value. If you want to print the value in value input do not forget the echo or print

1 answer

0


First need to hit your query, or you named place holders that are those have two dots followed by an ex name: :id or place holders that are the questions.

Change:

$sqlId = "SELECT * FROM web_cadcli WHERE idcad_cliente = ':id_us', nom_cliente = $this->getNome($nome), ema_cliente = $this->getEmail($email), senha_email = $this->senha($senha)  ";

To:

$sqlId = "SELECT * FROM web_cadcli
 WHERE idcad_cliente = ?, nom_cliente = ?, ema_cliente = ?, senha_email = ?";

The second step is to tidy the Binds, each question should be 'arrayed' with its position in the query. The first value should always start with a.

$sqlId->bindParam(1, $this->cod,   PDO::PARAM_INT);
$sqlId->bindParam(2, $this->nome,  PDO::PARAM_STR);
$sqlId->bindParam(3, $this->email, PDO::PARAM_STR);
$sqlId->bindParam(4, $this->senha, PDO::PARAM_STR);

Finally displays the value of the object in the form, change the set() for get() and put a echo or print before. Remember that the return of your query is a simple array and not an object

change:

<input type="email" class="form-control" id="exampleInputPassword1" value="<?php $usuario->setNome($nome) ?>">

To:

 <input type="email" class="form-control" id="exampleInputPassword1" value="<?php echo $usuario['nome']; ?>">

Browser other questions tagged

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