mysqli_prepare () expects exactly 2 parameters, 1 given

-1

Hello, I'm developing a local system here for the store where I work and I have the following title problem when importing data to the bank.

Here are the codes:

User Class

<?php

class Usuario {

    private $id;
    private $nome;
    private $tipo;
    private $login;
    private $senha;

    function getId() {
        return $this->id;
    }

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

    function getTipo() {
        return $this->tipo;
    }

    function getLogin() {
        return $this->login;
    }

    function getSenha() {
        return $this->senha;
    }

    function setId($id) {
        $this->id = $id;
    }

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

    function setTipo($tipo) {
        $this->tipo = $tipo;
    }

    function setLogin($login) {
        $this->login = $login;
    }

    function setSenha($senha) {
        $this->senha = $senha;
    }

    public function cadastro ($nome, $login, $senha){

        $stmt = mysqli_prepare("INSERT INTO usuario (nome, login, senha) VALUES (?,?,?)");
        $stmt->bind_param($stmt, $nome, $login, $senha);
        $stmt->execute();
    }
}

And here's the connection to the bank:

$conn = mysqli_connect('localhost', 'root', '', 'sysloja', '3306');
if (!$conn) {
   die('Could not connect to MySQL: ' . mysqli_connect_error());
}
mysqli_query($conn, 'SET NAMES \'utf8\'');

mysqli_close($conn);
    
asked by anonymous 29.08.2018 / 21:04

2 answers

2

The function mysqli_bind_param () asks for two arguments the first is a string which defines the following types of arguments, as they appear to be strings use s

Change:

public function cadastro ($nome, $login, $senha){
   $stmt->bind_param($stmt, $nome, $login, $senha);

To:

public function cadastro ($con, $nome, $login, $senha){
   $stmt->bind_param($stmt, 'sss', $nome, $login, $senha);

Or you can even simplify things a bit and pass the connection itself in the constructor of Usuario and make it available to other methods.

class Usuario {
   private $con;
   public function __construct($connection){
      $this->con = $connection;
   }
   //...demais métodos e propriedades

The registration method from now on should use the connection that is an attribute of the class / object or to access it make $this->con .

With the changes the method should look like this:

public function cadastro ($nome, $login, $senha){

    $stmt = $this->con->prepare('INSERT INTO usuario (nome, login, senha) VALUES (?,?,?)');
    $stmt->bind_param('sss', $nome, $login, $senha);
    if(!$stmt->execute()){
        echo 'erro: '. $stmt->error;
    }
}
    
29.08.2018 / 21:10
0

You are passing the wrong parameter in 2 places.

One way to fix:

Add the connection as a parameter and use it in the mysqli_prepare function:

public function cadastro ($nome, $login, $senha, $conn){

    $stmt = mysqli_prepare($conn, "INSERT INTO usuario (nome, login, senha) VALUES (?,?,?)");
    $stmt->bind_param($stmt, $nome, $login, $senha);
    $stmt->execute();
}

For the direct query, it should also be fixed:

$query = 'SELECT * FROM tabela';
$result = mysqli_query($conn, $query);

or

$result = mysqli_query($conn, 'SELECT * FROM tabela');

Documentation: mysqli_query , mysqli_prepare

    
29.08.2018 / 21:12