UPDATE registration 0 in column even passing the value

Asked

Viewed 32 times

0

I’m trying to edit the data in my database using a form. This same form pulls from a table the data to be edited, in one part of the form there is a select to Voce select the desired field coming from another table.

The problem is that when I pull the data from this other table to register in the desired table, the value entered in the value from the other table registers the value 0 in the database, if I put any number in the value of the select, it registers normally. Only by coming the value of another table that does not.

 <?php 

            include_once '../conecta_banco.php';
            $query = $conecta->prepare("SELECT unidade.*, serv_impressao.*
            FROM serv_impressao as serv_impressao 
            RIGHT JOIN unidades_prevent as unidade 
            on (unidade.id_unidade = serv_impressao.unidade_fk) WHERE serv_impressao.ativo = 1 ");
            $query->execute();
            $fetchAll = $query->fetchAll();
            foreach($fetchAll as $dados_impressao){

                echo '<div id="editServerImpressao?id='.$dados_impressao['id'].'" class="modal modal-fixed-footer" style="width:600px;height:8  00px;">';
                echo '    <div class="modal-content ">';
                echo '        <i class="material-icons left">edit</i><h1 class="flow-text">'.$dados_impressao['unidade'].'</h1>';
                echo '        <div class="col s12 l16" >';
                echo '            <form method="POST" action="../model/editar_dados/edit_info_serv_impressao_envia.php">';
                echo '                  <input type="hidden" name="id" value="'.$dados_impressao['id'].'"> ';
                echo '                   <div class="input-field col s6">';
                echo '                      <input id="hostname" type="text" class"validate" value="'.$dados_impressao['hostname'].'" name="hostname" maxlength="20" autocomplete="off" ">';
                echo '                      <label for="hostname">Hostname</label>';
                echo '                   </div>';
                echo '                   <div class="input-field col s6">';
                echo '                      <input id="endereco_ip" type="text" class"validate" value="'.$dados_impressao['endereco_ip'].'" name="endereco_ip" maxlength="20" autocomplete="off" ">';
                echo '                      <label for="endereco_ip">Endereço IP</label>';
                echo '                   </div>';

                echo '                   <div class="input-field col s12">';
                echo '                      <select name="unidade_fk">';
                echo '                          <option value="'.$dados_impressao['unidade_fk'].'" selected >'.$dados_impressao['unidade'].'</option>';
                                                $select = $conecta->prepare("SELECT unidade FROM unidades_prevent WHERE ativo = 1 ORDER BY unidade ASC");
                                                $select->execute();
                                                $fetchAll = $select->fetchAll();
                                                foreach($fetchAll as $unidades){
                                                    echo '<option value ="'.$unidades['sigla'].'">'.$unidades['unidade'].'</option>';
                                                    //no value acima ocorre o erro, setando um valor numero qualquer ele edita normal, 
                                                    //mas quando vem da outra tabela ele nao cadastra
                                                }
                echo '                      </select>';
                echo '                      <label>Unidade</label>';
                echo '                   </div>';

                echo '                   <div class="input-field col s12">';
                echo '                      <input id="descricao" type="text" class"validate" value="'.$dados_impressao['descricao'].'" name="descricao" maxlength="100" autocomplete="off" ">';
                echo '                      <label for="descricao">Descrição</label><br><br>';
                echo '                      <input class="btn left waves-effect waves-green btn-flat #0d47a1 blue darken-3 white-text" type="submit" value="Editar" name="editar"> ';
                echo '                   </div>';
                echo '            </form>    ';
                echo '        </div>';
                echo '    </div>';
                echo '    <div class="modal-footer">';
                echo '        <a href="#!" class="modal-action modal-close waves-effect waves-green btn-flat">Fechar</a>';
                echo '    </div>';
                echo '</div>';

            }

        ?>

I am using a right Join because from the other table to which I am trying to edit the data, it pulls the drive field to appear on the screen. I tried to put a foreign key but without success.

Follow the other code that sends to the database.

<?php

session_start();
include_once '../../conecta_banco.php';

//botao com o name alterar
$envia=filter_input (INPUT_POST, 'editar', FILTER_SANITIZE_STRING);
if($envia){
    //recupera os dados do form
    $id = filter_input(INPUT_POST, 'id', FILTER_SANITIZE_NUMBER_INT);
    $endereco_ip = filter_input (INPUT_POST, 'endereco_ip', FILTER_SANITIZE_STRING);
    $unidade = filter_input (INPUT_POST, 'unidade_fk', FILTER_SANITIZE_NUMBER_INT);
    $descricao = filter_input (INPUT_POST, 'descricao', FILTER_SANITIZE_STRING);
    $hostname = filter_input (INPUT_POST, 'hostname', FILTER_SANITIZE_STRING);

    $query="UPDATE serv_impressao set endereco_ip = :endereco_ip, unidade_fk = :unidade_fk, descricao = :descricao, hostname = :hostname WHERE id = ".$id." ";

    $atualiza = $conecta->prepare($query);
    $atualiza->bindParam(':endereco_ip', $endereco_ip);
    $atualiza->bindParam(':unidade_fk', $unidade);
    $atualiza->bindParam(':descricao', $descricao);
    $atualiza->bindParam(':hostname', $hostname);

    if($atualiza->execute()){
        //fazer algo que atualizou com sucesso
        header("Location: ../../view/consulta_rapida.php");
    }else{
        header("Location: cadastroErro.php");
    }
}else{
    echo "erro ao cadastrar";
}

?>

1 answer

0

I found the wrong.

Wrong

 $select = $conecta->prepare("SELECT unidade FROM unidades_prevent WHERE ativo = 1 ORDER BY unidade ASC");

Correct

 $select = $conecta->prepare("SELECT * FROM unidades_prevent WHERE ativo = 1 ORDER BY unidade ASC");

In the query to do the editing in the database, only the drive was selected, I did not pay attention to that. Just putting the * to select everything and it worked.

Browser other questions tagged

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