How do I know if the form received in php is an Insert, delete or update?

Asked

Viewed 73 times

0

I need to make a CRUD system with html and PHP, in which PHP takes a certain measure according to the type of request (whether it is register, update or delete) but I don’t know how to pass this from html to PHP.

HTML:

<form action="index.php" method="POST">
            <div class="form-group">
                <input type="nome" name="nome" class="form-control"  placeholder="Nome: ">
            </div>

            <div class="form-group">
                <input type="email" name="email" class="form-control" placeholder="Email: ">
            </div>

            <div class="form-group">
                <input type="date" name="nascimento" class="form-control" placeholder="Data de nascimento: ">
            </div>

            <div class="form-group">
                <input type="password" name="senha" class="form-control" placeholder="Senha:">
            </div>

            <button type="submit" name='cadastro' class="btn btn-primary">Cadastrar</button>
        </form>

PHP:

<?php 

// 'cadastro' refere-se ao name do submit, mas nao funcionou
    if($_POST['cadastro']) { 

        $banco = mysqli_connect('127.0.0.1', 'root', '', 'crud');

        $nome = $_POST['nome'];
        $email = $_POST['email'];
        $senha = $_POST['senha'];
        $nascimento = $_POST['nascimento'];

        $sql = "INSERT INTO usuario VALUES ('', '$nome' , '$email' , '$senha', '$nascimento')";

        echo "Cliente cadastrado com sucesso!";

        mysqli_query($banco, $sql);
        mysqli_close($banco);

        echo "Cliente cadastrado com sucesso!";
    }
?>
  • eu preciso saber reconhecer oq o usuario quer, se é inserir algo, remover algo ou alterar algo... You forgot the most important thing, which is the user trying to find flaws. There will always be someone to change the 'attr name' of your button to edit/delete something that does not exist or does not belong to it.

1 answer

0

Dear good night, you’re going the right way, however, first you must create two more Ubmit one to edit and the other to register and put two more conditions to execute the call action.

vc is unable to enter anything pq, vc has not passed the fields of your table user and has a '' before $name, the correct form would be this(but replace according to the name of your table in the database) :

$sql = "INSERT INTO usuario(nome, email,senha,nascimento) VALUES ('$nome' , '$email' , '$senha', '$nascimento')";

  • Good night! The Insert is already working, but I don’t know how to recognize in php when it’s an Insert or an update, because each type is a different function. I tried to do something like " if(registration) {} but it didn’t work

  • well that’s not php is SQL.

  • i need to know what user wants, whether to insert something, remove something or change something, the rest is already ready

  • vc must create two more Ubmit(IF) to edit one and another to register the html within the tag form and put two more conditions (IF ) to execute the call action.

  • @Viniciushenriquecamposdear take a look at this article link, teaches step by step to create, read, edit and delete, in this article is used PDO, for mysqli was discontinued.

Browser other questions tagged

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