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 soparent::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?– rray
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 ? :)– Felipe de Pietro
If it gives any error message, it is interesting to put it next to the question too.
– Woss
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
– novic
@Virgilionovic create the method
__construct()
class inserts, when executing the same independent, does not return any error !– Felipe de Pietro
@Felipedepietro strange the problem is that your code does nothing? does not insert?
– novic
@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 :)
– Felipe de Pietro