Function to edit returning NULL PHP and MYSQL

Asked

Viewed 44 times

1

Good night to you all, I am a beginner in programming and am following a tutorial to create a stock control system.

    if(isset($_GET['id'])) {

      $idFabricante = $_GET['id'];

      var_dump($resp = $fabricante->EditFabricante($idFabricante));

    echo'  <div class="col-md-6">
          <!-- general form elements -->
          <div class="box box-primary">
            <div class="box-header with-border">
              <h3 class="box-title">Fabricante</h3>
            </div>
            <!-- /.box-header -->
            <!-- form start -->
            <form role="form" action="../../App/Database/insertfabricante.php" method="POST">
              <div class="box-body">
                <div class="form-group">
                  <label for="exampleInputEmail1">Nome da Empresa</label>
                  <input type="text" name="NomeFabricante" class="form-control" id="exampleInputEmail1" placeholder="Nome Fabricante" value="'.$resp['Fabricante']['Nome'].'">
                </div>
                <div class="form-group">
                  <label for="exampleInputEmail1">CNPJ</label>
                  <input type="text" name="CNPJFabricante" class="form-control" id="exampleInputEmail1" placeholder="CNPJ" value="'.$resp['Fabricante']['CNPJ'].'">
                </div>
                <div class="form-group">
                  <label for="exampleInputEmail1">E-mail</label>
                  <input type="text" name="EmailFabricante" class="form-control" id="exampleInputEmail1" placeholder="E-mail" value="'.$resp['Fabricante']['Email'].'">
                </div>
                <div class="form-group">
                  <label for="exampleInputEmail1">Endereco</label>
                  <input type="text" name="EnderecoFabricante" class="form-control" id="exampleInputEmail1" placeholder="Endereço" value="'.$resp['Fabricante']['Endereco'].'">
                </div>
                <div class="form-group">
                  <label for="exampleInputEmail1">Telefone</label>
                  <input type="text" name="TelefoneFabricante" class="form-control" id="exampleInputEmail1" placeholder="Telefone" value="'.$resp['Fabricante']['Telefone'].'">
                </div>
                <hr />
                <div class="box-header with-border">
              <h3 class="box-title">Representante</h3>
            </div>

                <div class="form-group">
                  <label for="exampleInputEmail1">Nome</label>
                  <input type="text" name="NomeRepresentante" class="form-control" id="exampleInputEmail1" placeholder="Nome do Representante">
                </div>
                <div class="form-group">
                  <label for="exampleInputEmail1">Telefone</label>
                  <input type="text" name="TelefoneRepresentante" class="form-control" id="exampleInputEmail1" placeholder="Telefone">
                </div>
                <div class="form-group">
                  <label for="exampleInputEmail1">E-mail</label>
                  <input type="text" name="EmailRepresentante" class="form-control" id="exampleInputEmail1" placeholder="E-mail ">
                </div>


                 <input type="hidden" name="iduser" value="'.$idUsuario.'">
                 <input type="hidden" name="idFabricante" value="'.$idFabricante.'">
              <!-- /.box-body -->

              <div class="box-footer">
                <button type="submit" name="upload" class="btn btn-primary" value="Cadastrar">Cadastrar</button>
                <a class="btn btn-danger" href="../../views/prod">Cancelar</a>
              </div>
            </form>
          </div>
          <!-- /.box -->
          </div>
</div>';

I am trying to make sure that when I click edit, automatically fill in the fields. But after using var_dump is always returning NULL.

Follows the Function with the select:

this is within a class fabricante.class.php

public function EditFabricante($idFabricante){

  $this->query = "SELECT * FROM `fabricante` WHERE 'idFabricante'= 
  '$idFabricante'";

    if($this->result = mysqli_query($this->SQL, $this->query) or die ( 
       mysqli_error($this->SQL))){

      if($row = mysqli_fetch_array($this->result)){

          $NomeFabricante = $row['NomeFabricante'];
          $CNPJFabricante = $row['CNPJFabricante'];
          $EmailFabricante = $row['EmailFabricante'];
          $EnderecoFabricante = $row['EnderecoFabricante'];
          $TelefoneFabricante = $row['TelefoneFabricante'];
          $Usuario_idUser = $row['Usuario_idUser'];

        $array = array('Fabricante' => ['Nome' => $NomeFabricante, 'CNPJ' => $CNPJFabricante, 'Email' => $EmailFabricante, 'Endereco' => $EnderecoFabricante, 'Telefone' => $TelefoneFabricante, 'Usuario_idUser' =>$Usuario_idUser,]);

       return $array; 
      }

  }else{

    return 0;
  }

}

If anyone can give a helping hand I’d like to thank you in advance!

1 answer

0

Good morning! Your error is in the Mysql query. From what it seems to me, you are turning a Mysql 'idFabricante' field into a String, to fix this you should remove the quotes.


Where is: $this->query = "SELECT * FROM fabricante WHERE 'idFabricante'='$idFabricante'";

Replace with: $this->query = "SELECT * FROM fabricante WHERE idFabricante='$idFabricante'";

Browser other questions tagged

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