UPDATE PHP com Javascript

Asked

Viewed 435 times

4

I am trying to carry out the UPDATE, but gives the following error:

Notice: Undefined index: id in C: wamp www Sitephp base updateFunctioning.php on line 4. Follow my code

Function

function atualizar(id){
    if(confirm("Deseja atualizar o registro?")){
        location.href = 'atualizarFuncionario.php?id=' + id;
    }
}

php record.

<!DOCTYPE html>

<html>
    <head>
        <meta charset="UTF-8">
        <link rel="stylesheet" type="text/css" href="css/estilo.css">
        <script src="JS/func.js"></script>
        <script src="JS/jquery-3.2.1.min.js"></script>
    </head>
    <body>

        <p class="cab">ALTERAÇÃO DE FUNCIONÁRIOS</p>
        <form action="base/atualizarFuncionario.php" method="POST">

          <?php

             require_once './base/conexao.php';  

               $funcionario = filter_input(INPUT_GET,"id");
               $nome_alter= filter_input(INPUT_GET,"nome");
               $cpf_alter= filter_input(INPUT_GET,"cpf");
               $tel_alter= filter_input(INPUT_GET,"tel");
          ?>



  <center> <input type="text" id="inp" name="nome" size="40" value="<?php echo $nome_alter;?>" required="" placeholder="Nome" ><br><br>
        <input type="text" name="cpf" id="inp" size="40" id="cpf" required="" value="<?php echo $cpf_alter;?>" maxlength="14" onkeyup="mascCPF(this.value)" placeholder="CPF xxx.xxx.xxx-xx"><br><br>
        <input type="text" name="tel" id="inp" size="40" value="<?php echo $tel_alter;?>" id="tel" required="" maxlength="14" onkeyup="mascTel(this.value)"  placeholder="Telefone(xx)9xxxx-xxxx"><br><br>
        <a href="#" onclick="atualizar(<?php $funcionario = $_GET['id'];?>)"><button type="submit">ATUALIZAR</button></a></center>

        </form>
    <center> <a href="base/listarFuncionario.php"><input type="submit" value="Listar"</a> </center>


    </body>
</html>

updateFuncture.php

include_once './conexao.php';
$id = $_GET['id'];
$nome = $_POST['nome'];
$cpf = $_POST['cpf'];
$telefone = $_POST['tel'];
$atualizar = "UPDATE funcionario SET nome='$nome', tel = '$telefone', cpf= '$cpf'  WHERE id= '$id'";

$atualizar = $PDO->query($atualizar);


if($atualizar){
    echo "<script>alert('Funcionario atualizado com sucesso!');  </script>";
} else {
    echo "<script>alert('Erro ao atualizar'); </script>";
     //location.href = '../formCadastro.php';
}

  • indicate which code refers to the.php registry file, for ease

  • The one in the middle, where it contains my form

  • ok I’ll edit the question, accept the edit so I fix some details

  • method="POST" you are sending the form as POST and waiting for the parameter as GET, that’s right?

  • Yes, because when I put to receive the parameter as POST it from the error

  • 1

    Dude, you send POST and try to catch GET doesn’t make any sense. It’s never gonna work

  • and that base/, All right, METHOD of FORM??

  • Yes, it is the path of my file updateFunctioning.php

  • Note that you do not need to indicate which page will go in your type=Submit, it already recognizes this by the action, follow my code that will totally work

Show 4 more comments

2 answers

2

I changed your form and your POST and what I’m sending is a functional code, IE, was tested before posting here. Note that I removed the href of your type='submit' because if it is already being indicated in the <form action='...' is already recognized the file to which he has to submit the data

<form action="atualizarFuncionario.php" method="POST">

    <?php

    require_once './base/conexao.php'; 

        $funcionario = filter_input(INPUT_GET,"id");
        $nome_alter= filter_input(INPUT_GET,"nome");
        $cpf_alter= filter_input(INPUT_GET,"cpf");
        $tel_alter= filter_input(INPUT_GET,"tel");

    ?>

    <label>ID: </label>
    <input type="hidden" name="id" value="<?php echo $nome_alter; ?>" required>

    <label>Nome: </label>
    <input type="text" id="inp" name="nome" size="40" value="<?php echo $nome_alter; ?>" required="" placeholder="Nome"><br><br>

    <label>CPF: </label>
    <input type="text" name="cpf" id="inp" size="40" id="cpf" required="" value="<?php echo $cpf_alter; ?>" maxlength="14" onkeyup="mascCPF(this.value)" placeholder="CPF xxx.xxx.xxx-xx"><br><br>

    <label>Telefone: </label>
    <input type="text" name="tel" id="inp" size="40" value="<?php echo $tel_alter; ?>" id="tel" required="" maxlength="14" onkeyup="mascTel(this.value)"  placeholder="Telefone(xx)9xxxx-xxxx"><br><br>

    <center><input type='submit' name='enviar' value='ATUALIZAR'></center>

</form>

And the code to change was this way, I created a bank and here it worked perfectly, where it will be verified if there is the ID of the employee to make UPDATE, otherwise, that is, if there is no he will INSERT

include_once './conexao.php';

    if(isset($_POST['enviar'])){

        $id = $_POST['id'];
        $nome = $_POST['nome'];
        $cpf = $_POST['cpf'];
        $tel = $_POST['tel'];

        $sql = "SELECT * FROM funcionario WHERE id = '$id' "; 
        $resulta = $conn->query($sql);
        $row = $resulta->fetch_assoc();

        if ($resulta->num_rows > 0) {
            $result = "UPDATE funcionario SET nome = '$nome', cpf = '$cpf', tel = '$tel' WHERE id = '$id' ";
        } else {
            $result = "INSERT INTO funcionario (nome, telefone, endereco) VALUES ('$nome, '$cpf', '$tel')";
        }

        $resultado = mysqli_query($conn, $result);
        echo $result;
     }
  • Just forgot to put the value on Hidden there, but that’s it!

  • the answer has been edited, but thanks @arllondias

1


It doesn’t make sense the Javascript there, you don’t need to use it, just put a field Hidden in your form as the id that will update and send as "POST" even for PHP, would look like this:

php record.:

   <!DOCTYPE html>

    <html>
            <head>
                <meta charset="UTF-8">
                <link rel="stylesheet" type="text/css" href="css/estilo.css">
    <script src="JS/func.js"></script>
    <script src="JS/jquery-3.2.1.min.js"></script>
    </head>
        <body>
            <P class="cab">
               ALTERAÇÃO DE FUNCIONÁRIOS
            </P>
            <form action="base/atualizarFuncionario.php" method="POST">

                    <?php
        require_once './base/conexao.php';            
        $funcionario = filter_input(INPUT_GET,"id");
        $nome_alter= filter_input(INPUT_GET,"nome");
        $cpf_alter= filter_input(INPUT_GET,"cpf");
        $tel_alter= filter_input(INPUT_GET,"tel");
        ?>



  <center>
    <input type="text" id="inp" name="nome" size="40" value="<?php echo $nome_alter;?>" required="" placeholder="Nome"><br><br>
    <input type="text" name="cpf" size="40" id="cpf" required="" value="<?php echo $cpf_alter;?>" maxlength="14" onkeyup="mascCPF(this.value)" placeholder="CPF xxx.xxx.xxx-xx"><br><br>
    <input type="text" name="tel" size="40" value="<?php echo $tel_alter;?>" id="tel" required="" maxlength="14" onkeyup="mascTel(this.value)" placeholder="Telefone(xx)9xxxx-xxxx"><br><br>
    <input type="hidden" name="id" size="40" value="<?php echo $funcionario?>" id="id" >
    <button type="submit">ATUALIZAR</button>
</center>



            </form>
        <center> <a href="base/listarFuncionario.php"><input type="submit" value="Listar"</a> </center>


        </body>
    </html>

and php:

include_once './conexao.php';
$id = $_POST['id'];
$nome = $_POST['nome'];
$cpf = $_POST['cpf'];
$telefone = $_POST['tel'];
$atualizar = "UPDATE funcionario SET nome='$nome', tel = '$telefone', cpf= '$cpf'  WHERE id= '$id'";

$atualizar = $PDO->query($atualizar);


if($atualizar){
    echo "<script>alert('Funcionario atualizado com sucesso!');  </script>";
} else {
    echo "<script>alert('Erro ao atualizar'); </script>";
     //location.href = '../formCadastro.php';
}
  • Pdoexception: SQLSTATE[42000]: Syntax error or access Violation: 1064 Erreur de syntaxe prãs de '1'><table class='Xdebug-error Xe-notice' dir='Ltr' border='1' cellspacing='0' ce' Ã la ligne 1 in C: wamp www Sitephp base updateFunctioning.php on line 10

  • Fatal error: Uncaught Exception 'Pdoexception' with message 'SQLSTATE[42000]: Syntax error or access Violation: 1064 Erreur de syntaxe pràs de '1'><table class='Xdebug-error Xe-notice' dir='Ltr' border='1' cellspacing='0' ce' Ã la ligne 1' in C:wamp www Sitephp base updateFunctioning.php on line 10

  • Can you send the SQL error message? input data?

  • <input type="Hidden" name="id" size="40" value="<? php echo $id = $_GET['id'];? >" id="id" > I edited this line and was able to send the information to the database

  • And the UPDATE worked

  • Thank you, you helped me a lot!

  • I had the variable name wrong, now I edited it and it’s correct. @Ruanrodrigues

Show 2 more comments

Browser other questions tagged

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