Entering data in PHP

Asked

Viewed 343 times

0

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

    <?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();
    }*/
?>

Inserts.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 is the.php validation.:

<?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>
  • conectar() is not static to be called so parent::conectar(), null is the name of a field (see the first field of the Insert)? $res seems to be left over there. How do you know when the method fails?

  • hello buddy, so I put the null because in my bank I’m referring to the ID that was created, parent::conectar()I called before to be able to connect to the bank and then enter the values, as for the test, would be able to help me ? :)

  • 1

    If it gives any error message, it is interesting to put it next to the question too.

  • This code does not give errors? (it has to give errors, has a design flaw too and the missing constructor call) has problems your code

  • @Virgilionovic create the method __construct()class inserts, when executing the same independent, does not return any error !

  • @Felipedepietro strange the problem is that your code does nothing? does not insert?

  • @Virgilionovic this, was with doubts on the insertion of data in phpoo, I tried to encode alone, so I was not successful, so I decided to ask for help haha :)

Show 2 more comments

1 answer

0


There are some coding errors of the conceptual type, well I will propose a minimum example:

Class connect

Responsible for connection 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

Model manager.

<?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

Operations officer CRUD of 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, what changes basically in your code would be in the part of valida-cadastro.php and its classes, that is, an adequacy in reality.

Browser other questions tagged

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