Save populated form data to another table

Asked

Viewed 47 times

0

The code below receives data from a table that I can edit and save again, but I was trying to use this data and insert them in another table but I’m not getting.

Code receiving the data:

<?php
session_start();
include_once("conexao.php");
$id = filter_input(INPUT_GET, 'id', FILTER_SANITIZE_NUMBER_INT);
$result_usuario = "SELECT * FROM usuarios WHERE id = '$id'";
$resultado_usuario = mysqli_query($conn, $result_usuario);
$row_usuario = mysqli_fetch_assoc($resultado_usuario);
?>
<!DOCTYPE html>
<html lang="pt-br">
    <head>
        <meta charset="utf-8">
        <title>Agendamento de manicure</title>     
    </head>
    <body>
        <a href="cad_usuario.php">Cadastrar</a><br>
        <a href="index.php">Listar</a><br>
        <h1>Editar UsuĂ¡rio</h1>
        <?php
        if(isset($_SESSION['msg'])){
            echo $_SESSION['msg'];
            unset($_SESSION['msg']);
        }
        ?>
        <form method="POST" action="proc_cad_agendamento.php">
            <input type="hidden" name="id" value="<?php echo $row_usuario['id']; ?>">
            <label>Nome: </label>
            <input type="text" name="nome" placeholder="Digite o nome completo" value="<?php echo $row_usuario['nome']; ?>"><br><br>

            <label>CPF: </label>
            <input type="text" name="cpf" placeholder="Digite o seu melhor e-mail" value="<?php echo $row_usuario['cpf']; ?>"><br><br>

            <input type="submit" value="Editar">
        </form>
    </body>
</html>

Code processing data to insert into another table:

<?php
session_start();
include_once("conexao.php");
$id = filter_input(INPUT_POST, 'id', FILTER_SANITIZE_NUMBER_INT);
$nome = filter_input(INPUT_POST, 'nome', FILTER_SANITIZE_STRING);
$cpf = filter_input(INPUT_POST, 'cpf', FILTER_SANITIZE_STRING);

$result_usuario = "INSERT INTO agendamento (id, nome, cpf) VALUES ('$id', '$nome', '$cpf', NOW())";
$resultado_usuario = mysqli_query($conn, $result_usuario);
if(mysqli_insert_id($conn)){
    $_SESSION['msg'] = "<p style='color:green;'>Usuário cadastrado com sucesso</p>";
    header("Location: index.php");
}else{
    $_SESSION['msg'] = "<p style='color:red;'>Usuário não foi cadastrado com sucesso</p>";
    header("Location: cad_usuario.php");
}
  • why, re-insert the data into another table?

  • The code appears to be correct. You have checked whether the table has the correct field types and size to receive this data?

  • Let’s go as the "Jack" (in parts)... 1 - Which error appears or you are treating the errors? 2 - What is the name of the file that processes the insertion in the new table?

  • Vinicius de jesus, because I have a service scheduling system, I have located the customer, here comes a form with the customer data that is already registered in a table, this form has 3 more fields employee, date and time when sending it has to record this data in another table so that I can locate the scheduling per client or per employee.

  • @Magichat there and where I am not understanding,as there is the IF ELSE it does not show error because it redirects to message that is inside the IF ELSE however if I comment the IF ELSE it is blank without any error and does not write the data in the table, already check the error check this enabled.

  • What is the name of the file that inserts the data in the other table?

  • proc_cad_scheduling.php that would be the second code I posted up there

  • anyone would have any more tips?

Show 3 more comments
No answers

Browser other questions tagged

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