mysqly connection to php

Asked

Viewed 56 times

0

hello,

I am studying PHP but I have a code that is not storing in BD, I have debugged it all, the error tests n return nothing, it is only blank but n stores

follows code:

<?php 
    require 'classeFornecedor.php';

    $fornecedor = new fornecedor();
    $fornecedor->nome = filter_input(INPUT_POST, 'nome', FILTER_SANITIZE_STRING);
    $fornecedor->cnpj = filter_input(INPUT_POST, 'cnpj', FILTER_SANITIZE_STRING);
    $fornecedor->cadastrarFornecedor();

?>
<?php


class fornecedor{
    
    private $con;
    private $nome;
    private $cnpj;
    
   
    
    public function __set($atributo, $valor){
        $this->$atributo = $valor;
    }
    public function __get($atributo){
        return $this->$atributo;
    }
    
    public function cadastrarFornecedor(){
        require 'classeConexao.php';
        
        $query = "INSERT INTO('nome', 'cnpj') VALUES ('$this->nome', '$this->cnpj')";
        $resultado = mysqli_query($conn, $query);
        return 'cadastro.php';
    }
}
?>
<?php

    
   
    
$host = "localhost";
$database = "agendamento";
$user = "root";
$pass = "";
   
$conn = mysqli_connect($host, $user, $pass, $database);
        mysqli_select_db($conn,$database);
        
 

?>
Register

<div id="menu">
        <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
            <a class="navbar-brand" href="index.php">Login</a>
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
    <div class="collapse navbar-collapse" id="navbarNavAltMarkup">
        <div class="navbar-nav">
            <a class="nav-item nav-link" href="agendamento.php">Agendamento <span class="sr-only">(current)</span></a>
            <a class="nav-item nav-link active" href="cadastro.php">Cadastro</a>
            <a class="nav-item nav-link" href="consulta.php">Consulta</a>

        </div>
    </div>
    </nav>
</div>
<header>
        <img id="imagem" alt="Conibase - do basico ao acabamento" src="logosite.png">
    </header>
    <div class="shadow-lg p-3 mb-5 bg-white rounded" id="cadastro">
        <header>
            <h2 id="head">
            Cadastro de Fornecerdor
            </h2>
        </header>
            <form action="cadastro-int.php" method="post" >
                <input name="nome" class="form-control form-control-sm" id="fornecedor" type="text" placeholder="Nome">
                <input name="cnpj" class="form-control form-control-sm" id="cnpj" type="text" placeholder="CNPJ">
                <button type="submit" id="botao" class="btn btn-outline-danger">Cadastrar</button>
            </form>
    </div>

  • Not giving another error too? the variables you are assigning a value, are private, in case you should use __set

2 answers

3


I saw a part of your code that I believe is the reason you are not entering the data. Your 'INSERT' is without the table name.

It’s like this:

<?php 
  
  "INSERT INTO('nome', 'cnpj') VALUES ('$this->nome', '$this->cnpj')";

?>

Should stay like this:

<?php

  "INSERT INTO tbl_nome_da_tabela(nome, cnpj) VALUES ('$this->nome', '$this->cnpj')";

?>

Quotation marks (in the name of the columns) are also not required.

  • FUNCIONOUUU, THANK YOU.

  • Nothing, brother. Silly thing. It happens to me direct. Rs. If the answer helped you, you can mark it as "accept".

0

it was the name of the table and the extra quotes. Just one more question. I used the direct sqli to store the information, but I encoded it in another version by inserting it in a connected class(), and instantiating it in my vendor class in a __Construct, but when I was going to call the mysqli_query asks for 2 parameters, connection and input query there my code was like this

mysqli_query($this->con->connect(), $query);

being $this->con the attribute where I stored the instance in my __Construct but in executing he made that mistake:

mysqli_query() expects Parameter 1 to be mysqli, null Given in C: wamp64 www Project classeFornecedor.php on line 24

follows code

<?php
require 'classeConexao.php';

class fornecedor{
    
    private $con;
    private $nome;
    private $cnpj;
    
    Public function __contruct(){
      $this->con = new conexao();
    }
     
    public function __set($atributo, $valor){
        $this->$atributo = $valor;
    }
    public function __get($atributo){
        return $this->$atributo;
    }
    
    public function cadastrarFornecedor(){
        $this->con->conectar();
        $query = "INSERT INTO fornecedor(nome, cnpj) VALUES ('$this->nome', '$this->cnpj')";
        $result = mysqli_query($this->con->conectar(), $query);
        if($result){
        return 'ok';
        }else{
          return 'error!';
        }
   
    }
}
?>
<?php 
class conexao{
  
  
  private $host;
  private $user;
  private $pass;
  private $database;
  
  public function __construct(){
    $this->host = "localhost";
    $this->user = "root";
    $this->pass = "";
    $this->database = "agendamento";
  }
  
  public function conectar(){
  
    $conn = mysqli_connect($this->host, $this->user, $this->pass, $this->database);
    mysqli_select_db($conn, $database);
    }
}
  
?>

I was trying to execute him again agr only q this time I put a Return on the connect function to return the variable with mysqli_connect but I got that other mistake

Fatal error: Uncaught Error: Call to a Member Function conectar() on null in C: wamp64 www Project classeFornecedor.php on line 22

  • Geovane, I thought you’d better come up with a new question for that. But from what I am seeing there, you are not missing, when instantiating the supplier class, already pass the instance of the connected class as parameter to the vendor class constructor and then take this parameter within the supplier class (in your constructor) and attribute it to the $con ? I don’t know if I can be clear.

Browser other questions tagged

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