How to insert data coming from a form in the database [PDOstatment - CRUD]

1

I'm doing a simple CRUD for insertion of data obtained from a form. I did the function but it does not work and does not show any errors. I believe this is because information from form is not arriving via $_POST .

I would like to know what is missing and how can I make this connection, according to the code I am doing below!

OBS: I can connect to the database!

Form:

<!DOCTYPE html>
<html lang="PT-BR">
<head>
    <meta charset="utf-8">
    <title>Login</title>
</head>
<body>
    <form action="../../controllers/UserController.php" method="post" name="formLogin">
        <label>E-mail</label>
        <div class="email">
            <input type="text" name="user[email]" required>
        </div>  

        <!--INSERIR A CONFIRMAÇÃO DE E-MAIL-->

        <label>Password</label>
        <div class="password">
            <input type="password" name="user[password]" required>
        </div>  
        <!--INSERIR A CONFIRMAÇÃO DE SENHA-->

        <label>Nível de Acesso</label>
        <div class="">  
            <label>
                <input type="radio" name="user[level]" value="1">
                Gerente
            </label>
            <label>         
                <input type="radio" name="user[level]" value="2">
                Membro
            </label>
        </div>

        <div>
            <label>
                <input type="radio" name="user[type]" value="1">
                Nível 1
            </label>
            <label>
                <input type="radio" name="user[type]" value="2">
                Nível 2
            </label>
        </div>  

        <input type="hidden" name="action" value="insert">

        <div class="submit">
            <input type="submit" name="submit">
        </div>

</body>
</html>

Model User.php

 <?php
require_once(__DIR__."../../helpers/Connection.php");

class User {
    public $id;
    public $email;
    public $password;
    public $level;
    public $type;

    public function __construct($attributes) {
        $this->id = isset($attributes['id']) ? $attributes['id'] : null;
        $this->email = $attributes['email'];
        $this->password = $attributes['password'];
        $this->level = $attributes['level'];
        $this->type = $attributes['type'];
    }

    public function insert(){
        $connect = Connection::connect();
        $stm = $connect->prepare("INSERT INTO user(email, password, level, type) VALUES (':email', ':password', ':level', ':type')");
        $stm = bindValue(":email", $this->email, PDO::PARAM_STR);
        $stm = bindValue(":password", $this->password, PDO::PARAM_STR);
        $stm = bindValue(":level", $this->level, PDO::PARAM_INT);
        $stm = bindValue(":type", $this->type, PDO::PARAM_INT);
        return $stm->execute();

    }
}
?>

UserController.php

<?php
require_once("../models/Users.php");
/**
* 
*/
class UserController {

    public static function insert(){
        $user = new User($_POST["user"]);
        $user->insert();

    }
}

?>
    
asked by anonymous 15.03.2017 / 16:00

1 answer

1

Placeholders (: email,: password) do not need quotation marks, they serve as substitutes for the values you are going to pass.

<?php
    require_once(__DIR__."../../helpers/Connection.php");

    class User {
        public $id;
        public $email;
        public $password;
        public $level;
        public $type;

        public function __construct($attributes) {
            $this->id = isset($attributes['id']) ? $attributes['id'] : null;
            $this->email = $attributes['email'];
            $this->password = $attributes['password'];
            $this->level = $attributes['level'];
            $this->type = $attributes['type'];
        }

        public function insert(){
            $connect = Connection::connect();
            $stm = $connect->prepare("INSERT INTO user(email, password, level, type) VALUES (:email, :password, :level, :type)");
            $stm = bindValue(":email", $this->email, PDO::PARAM_STR);
            $stm = bindValue(":password", $this->password, PDO::PARAM_STR);
            $stm = bindValue(":level", $this->level, PDO::PARAM_INT);
            $stm = bindValue(":type", $this->type, PDO::PARAM_INT);
            return $stm->execute();

        }
    }
    ?>
    
23.05.2017 / 17:15