Save form data

Asked

Viewed 72 times

-3

Guys, I’m new at this, I made an html form and I can’t save the data, how do I do that?

  • 1

    Welcome Fagner, so that the community can help you it is important that you detail more your question, put as your code is so far.

2 answers

2

First you will create the form:

<form action="action.php" method="post">
    <label>Nome:</label><br>
    <input type="text" name="name"><br>
    <label>Email:</label><br>
    <input type="email" name="email"><br>
    <label>Senha:</label><br>
    <input type="password" name="password"><br>
    <input type="submit" value="Submit">
</form>

In the action.php:

<?php 
    $meuBancoDeDados='nome do seu banco de dados';
    $username='nome do usuário de acesso ao banco de dados';
    $password='senha do usuário de acesso ao banco de dados';

    // Aqui será criada uma conexão com o banco de dados, estou usando PDO para isso
    try {
        $pdo = new PDO('mysql:host=localhost;dbname='.$meuBancoDeDados, $username, $password);
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    } catch(PDOException $e) {
        echo 'Error: ' . $e->getMessage();
    }

    // Aqui são definidas as variáveis vindas do formulário
    $nome=$_POST["name"];
    $email=$_POST["email"];
    $password=$_POST["password"];

    // Aqui é executado a inserção no banco de dados
    $stmt = $pdo->prepare('INSERT INTO minhaTabela (nome,email,password) VALUES(:nome,:email,:password)');
    $stmt->bindParam(":nome",$nome);
    $stmt->bindParam(":email",$email);
    $stmt->bindParam(":password",$password);
    if($stmt->execute()):
        echo "Inserido";
    else:
        echo "Erro ao inserir";
    endif;
?>

Remembering that this is just a simple sketch, all entries should be handled to avoid attacks, etc.

Always seek to study and learn more about the language that will be used and understand the functions

Manual - PDO

Understanding the PDO class - Medium

0

You can put the action in the tag <form>, and inside it will put an input with the type submit. This input will be the send form button

As in the case of w3school https://www.w3schools.com/html/html_forms.asp

<form action="/action_page.php">
  First name:<br>
  <input type="text" name="firstname" value="Mickey"><br>
  Last name:<br>
  <input type="text" name="lastname" value="Mouse"><br><br>
  <input type="submit" value="Submit">
</form>

Browser other questions tagged

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