Search code snippets, questions, articles...

INSERT mvc OOP

<?php
//index.php
        if(isset($_POST['submit'])){
            
            $firstname = $_POST['firstname'];
            $lastname  = $_POST['lastname'];
            $email     = $_POST['email'];
            
            $obj = new Control(); // assign the object for controller class
            $obj->insert($firstname,$lastname,$email); // Pass the data in insert function
            //header("location: classes/control.class.php");
            echo "<script>alert('Inserted')</script>";
            echo "<script>window.open('index.php','_self')</script>";
        }

    ?>

    <?php
    //control.class.php
    class Control extends Model {

        public function insert($firstname,$lastname,$email) {

                $obj = new model();
                $obj->createUser($firstname,$lastname,$email);
        }

    }
    ?>
<?php
//model.class.php
    class Model extends Dbh{
        protected function createUser($firstname,$lastname,$email) {

            $sql = 'INSERT INTO users (firstname,lastname,email) VALUES (?,?,?)';
            $stmt = $this->connect()->prepare($sql);
            $stmt->execute([$firstname,$lastname,$email]);
        }
    }
Best JSON Validator, JSON Tree Viewer, JSON Beautifier at same place.

INSERT using OOP and MVC model

Search Index Data (The code snippet can also be found with below search text)

INSERT
mvc
OOP
Was this helpful?
0 Comments