Update modal database

Asked

Viewed 512 times

1

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

Follows the code where 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 of 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;

Observing: have a script that makes them free inputs:

<script language='JavaScript'>
 $("#btnEditar").on('click', function() {
  $('input[name="funcionario"]').removeAttr('readonly');
  $('input[name="rca"]').removeAttr('readonly');
  $('input[name="regiao"]').removeAttr('readonly');
});
</script>
  • How to edit fields with attribute readonly='readonly'?

  • @Leocaracciolo then forgot to inform I have a script that when clicking the edit button the inputs are released: <script language='Javascript'> $("#btnEditar"). on('click', Function() { $('input[name="employee"]').removeAttr('readonly'); $('input[name="rca"]').removeAttr('readonly'); $('input[name="regiao"]').removeAttr('readonly'); }); </script>

  • Now yes, understood!

  • @Leocaracciolo I edited there in the question also

  • Great, and how you’re passing variables to update.php?

2 answers

2


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

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

Another solution - in the archive atualizar.php alter $numero = $_GET['numero']; for $numero = $_POST['numero']; since it is sent via post by the form, thus becoming unnecessary to recover it via GET.

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'];`

0

Your form uses the post method for sending 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']

Browser other questions tagged

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