Update database modal

1

I made a code that updates the data entered by the user in the database, however, he is giving the message that the data has been updated, but does not update anything. The system works like a search. The user types the number (number and primary key) and then opens a modal containing the data for that number. By clicking on the button "edit", the user has the option to edit the "employee", "rca" and "region", and then save. When I click "save" I need it to update the database.

The following is the code that edits the inputs :

<?php
include_once("../conn/conexao.php");//faz a conexao com o banco de dados

if(!empty($_POST['numerodigitado'])){

    $numerodigitado = $_POST['numerodigitado'];

    $result = "SELECT * FROM tb_numeros WHERE numero = '$numerodigitado' ";
    $resultado = mysqli_query($conexao, $result);
    $row = mysqli_fetch_assoc($resultado);

    if($resultado -> num_rows > 0){
        echo"
        <div class='modal fade' id='squarespaceModa2' tabindex='-1' role='dialog' aria-labelledby='modalLabel' aria-hidden='true'>
            <div class='modal-dialog'>
            <div class='modal-content'>
                <div class='modal-header'>
                    <button type='button' class='close' data-dismiss='modal'><span aria-hidden='true'>×</span><span class='sr-only'>Close</span></button>

                <div class='modal-body'>
                <form method='post' action='atualizar.php'>
                    <div class='form-group'>
                        <div class='col-xs-12'><center>
                        <label for='exampleInputPassword1'>Funcionário</label></center>
                        <input type='text' name='funcionario' class='form-control' id='exampleInputPassword1' value=".$row['funcionario']." style='text-align: center;' readonly='readonly' >
                        <br></div>
                    </div>

                    <div class='form-group'>
                        <div class='col-xs-4'><center>
                        <label for='exampleInputPassword1'>Número</label></center>
                        <input type='text' name='numero' class='form-control' id='exampleInputPassword1' value=".$row['numero']." style='text-align: center;' readonly='readonly' >
                        </div>
                    </div>

                    <div class='form-group'>
                        <div class='col-xs-2'><center>
                        <label for='exampleInputPassword1'>RCA</label></center>
                        <input type='text' name='rca' class='form-control' id='exampleInputPassword1' value=".$row['rca']." style='text-align: center;' readonly='readonly' >
                        </div>
                    </div>

                   <div class='form-group'>
                        <div class='col-xs-6'><center>
                        <label for='exampleInputPassword1'>Região</label></center>
                        <input type='text' name='regiao' class='form-control' id='exampleInputPassword1' value=".$row['regiao']." style='text-align: center;' readonly='readonly' >
                        <br></div>
                    </div>

                    <div class='form-group'>
                        <div class='col-xs-6'><center>
                        <label for='exampleInputPassword1'>Número Chip</label></center>
                        <input type='text' name='nchip' class='form-control' id='exampleInputPassword1' value=".$row['nchip']." style='text-align: center;' readonly='readonly' >
                        </div>
                    </div>

                    <div class='form-group'>
                        <div class='col-xs-6'><center>
                        <label for='exampleInputPassword1'>IMEI</label></center>
                        <input type='text' name='imei' class='form-control' id='exampleInputPassword1' value=".$row['imei']." style='text-align: center;' readonly='readonly' >
                        <br></div>
                    </div>

                    <div class='form-group'>
                        <div class='col-xs-12'><center>
                        <label for='exampleInputPassword1'>Cadastrado Por</label></center>
                        <input type='text' name='usuario' class='form-control' id='exampleInputPassword1' value=".$row['usuario']." style='text-align: center;' readonly='readonly' >
                        <br></div>
                    </div>

                    <div class='form-group'>
                        <div class='col-xs-6'><center>
                        <button type='button' class='btn btn-default btn-lg btn-block' role='button' id='btnEditar' ><span class='glyphicon glyphicon-pencil'></span>&nbsp;Editar</button>
                        </center></div>
                    </div>

                    <div class='form-group'>
                        <div class='col-xs-6'><center>
                        <button class='btn btn-default btn-lg btn-block' role='button' type='submit' value='Cadastrar' name='Salvar'><span class='glyphicon glyphicon-ok'></span>&nbsp;Salvar</button>
                        </center></div>
                    </div>
                </form>
                </div>
                </div>
            </div>
            </div>
        </div>

        <script>
            $(document).ready(function(){ $('#squarespaceModa2').modal(); });  
        </script>";

    } 
    else {
       echo "<script>
            alert('Número não encontrado.');
            window.location='index.php';
        </script>"; 
    }
}

Now the code for atualizar.php :

<?php
session_start();
include("../conn/conexao.php");

    $numero = $_GET['numero'];

    $funcionario= $_POST['funcionario'];
    $rca= $_POST['rca'];
    $regiao= $_POST['regiao'];
    $nome=$_SESSION['nome'];

    $up = mysqli_query($conexao, "UPDATE tb_numeros SET funcionario='$funcionario', rca='$rca', regiao='$regiao', usuario='$nome'  WHERE numero = '$numero' ")or die (mysqli_error($conexao));

    if($up):
        echo "<script>
                alert('Alterado com sucesso.');
                window.location='index.php';
            </script>"; 
    else:
        echo "<script>
                alert('Ocorreu um erro ao atualizar, entre em contato com o administrador.');
                window.location='index.php';
            </script>";
    endif;

Comment : I have a script that frees the inputs

<script language='JavaScript'>
 $("#btnEditar").on('click', function() {
  $('input[name="funcionario"]').removeAttr('readonly');
  $('input[name="rca"]').removeAttr('readonly');
  $('input[name="regiao"]').removeAttr('readonly');
});
</script>
    
asked by anonymous 29.11.2017 / 13:21

2 answers

1

A solution - change your tag form by adding action to the numero=$numerodigitado parameter so that the atualizar.php file can create the variable $numero = $_GET['numero']; required to execute the WHERE numero = '$numero' of declaration UPDATE

<form action='atualizar.php?numero=$numerodigitado' method='POST'>

Another solution - in the file atualizar.php change $numero = $_GET['numero']; to $numero = $_POST['numero']; since it is sent via post by the form, thus making it unnecessary to retrieve it via GET. p>

A remark - If there is no $_SESSION['nome']; defined at some point in your application, it is necessary to change the file atualizar.php

$nome=$_SESSION['nome'];  para '$nome=$_POST['usuario'];'
    
29.11.2017 / 14:38
1

Your form uses the post method to send data, so change the update.php file

$numero = $_GET['numero']; //altera aqui para $_POST['numero']
$funcionario= $_POST['funcionario'];
$rca= $_POST['rca'];
$regiao= $_POST['regiao'];
$nome=$_SESSION['nome'];  //altera aqui para $_POST['usuario']
    
29.11.2017 / 14:20