How to enter data from a form in the database [Pdostatment - CRUD]

Asked

Viewed 151 times

0

I’m making a CRUD simple to insert data obtained from a form. I did the function but it does not work and does not present any error. I believe it’s because of the information coming from form are not arriving via $_POST.

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

OBS: I can have connection 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();

    }
}

?>
  • It will not quote simply on placeholders. Review the names of inputs also

  • why are you using mvc structure ? the request is not even passing the controller.

  • I’m still learning, Edilson. That’s why I’m asking what’s wrong...

  • @rray which placeholders? I only know placeholders in html and have none there... And review the name of inputs how? Because, as I understand it, it was communicating with php

  • Placeholders are 'markup' that you put to exchange them for values. VALUES (':email', ':password', ':level', ':type') not going to single quotes at all of them :email, :password etc..

1 answer

0

Placeholders (:email, :password) do not need quotes, they serve as substitutes for the values you will 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();

        }
    }
    ?>

Browser other questions tagged

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