Insertion of data in PHP [closed]

0

Hello everyone, I have a question that is the following, I am not able to insert data in PHP, give pfv help :) :) Here is the code: This is my Connection.class.php

    <?php

class Conexao {
    private $host = "localhost";
    private $user = "root";
    private $senha = "";
    private $banco = "pessoas";
    private $conexao;

    function __construct($host,$user,$senha,$banco,$conexao) {
        $this->host = $host;
        $this->user = $user;
        $this->senha = $senha;
        $this->banco = $banco;
        $this->conexao = $conexao;
    }

    function conectar(){
        $this->conexao = mysqli_connect($this->host,$this->user ,$this->senha ,$this->banco );
        return $this->conexao;

    }

    function fecha(){
        mysqli_close($this->conexao);
    }


}
    /*try{
        parent::conectar();
        if(mysqli_connect_errno() =! 0){
            throw new Exception('fudeu');
        }
    } catch (Exception $e) {
        $e->getMessage();
    }*/
?>

Insert.class.php:

<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Insere
 *
 * @author felipepietro
 */

require_once 'Conexao.class.php';
class Insere extends Conexao {
    private $nome;
    private $sobrenome;

    function getNome() {
        return $this->nome;
    }

    function getSobrenome() {
        return $this->sobrenome;
    }

    function setNome($nome) {
        $this->nome = $nome;
    }

    function setSobrenome($sobrenome) {
        $this->sobrenome = $sobrenome;
    }

    function insere(){
        $link = parent::conectar();
        $sql = "INSERT INTO pessoas(NULL,'nome','sobrenome') VALUES ('{$this->setNome()}','{$this->setSobrenome()}')";
        $res = mysqli_query($link, $sql);
    }

}

this the valida-cadastro.php:

<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

    require_once 'classes/Conexao.class.php';
    require_once 'classes/Insere.class.php';

    $insere = new Insere();
    $insere->getNome($_POST['nome']);
    $insere->getSobrenome($_POST['sobre']);
    $insere->insere();

?>

and finally my HTML:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <form method="POST" action="valida-cadastro.php">
            <label>
                Nome:
                <input type="text" name="nome" id="nome">
            </label>
            <label>
              Sobrenome:
              <input type="text" name="sobre" id="sobre">
            </label>
            <input type="submit" value="Cadastrar">
        </form>
    </body>
</html>
    
asked by anonymous 11.04.2017 / 02:01

1 answer

0

There are some coding errors of the conceptual type, well I'll propose a minimal example:

Class connect

Responsible for connecting to the database MySQL

<?php       
    class connect extends mysqli
    {
        private $host = "localhost";
        private $user = "root";
        private $pass = "senha";
        private $db   = "pessoas";
        private $port = 0;
        private $socket = "";

        public function __construct($host = "", $user = "", $pass = "", 
                                    $db = "", $port = 0, $socket = "")
        {
            if ($host !== "") $this->host = $host;
            if ($user !== "") $this->user = $user;
            if ($pass !== "") $this->pass = $pass;
            if ($db !== "") $this->db = $db;
            if ($port !== 0) $this->port = $port;
            if ($socket !== "") $this->socket = $socket;
            parent::__construct($this->host, $this->user, $this->pass,
                                $this->db, $this->port, $this->socket);
        }

        public function __destruct()
        {
            $this->close();
            unset($this);
        }
    }

Model class insert

Responsible for the template.

<?php       
    class insert
    {
        private $id;
        private $name;
        private $lastName;
        public function __construct($id = null, $name = "", $lastName = "")
        {
            $this->id = $id;
            $this->name = $name;
            $this->lastName = $lastName;
        }
        public function getId()
        {
            return $this->id;
        }
        public function setId($id)
        {
            $this->id = $id;
            return $this;
        }
        public function getName()
        {
            return $this->name;
        }
        public function setName($name)
        {
            $this->name = $name;
            return $this;
        }    
        public function getLastName()
        {
            return $this->lastName;
        }    
        public function setLastName($lastName)
        {
            $this->lastName = $lastName;
            return $this;
        }
    }

Class insertdal

Responsible for operations CRUD of the model.

<?php    
    class insertdal
    {
        private $connect;
        public function __construct(connect $connect)
        {
            $this->connect = $connect;
        }

        public function add(insert $model)
        {
            $sql  = 'INSERT INTO  tb_insert(name, lastname) VALUES(?,?)';
            $stmt = $this->connect->prepare($sql);
            $name = $model->getName();
            $last = $model->getLastName();
            $stmt->bind_param('ss', $name, $last);
            $stmt->execute();
            $model->setId($this->connect->insert_id);
            return $model;
        }
    }

Page index.php

Summary execution of the code.

<?php
    require_once 'connect.php';
    require_once 'insert.php';
    require_once 'insert_dal.php';

    $connect = new connect();
    $dal     = new insertdal($connect);
    $model   = new insert(null, 'first name', 'lastname');

    $model   = $dal->add($model);

    var_dump($model);

This is a basic example to manage a table in your database, which basically changes your code would be in the valida-cadastro.php part and its classes, that is, a fit in reality.

    
11.04.2017 / 04:11