mysqli_prepare() expects Exactly 2 Parameters, 1 Given

Asked

Viewed 212 times

-1

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

Follow 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);

2 answers

2


The function mysqli_bind_param() asks for two arguments the first is a string that defines the types of the following arguments, as it seems 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 also simplify things a little and pass the connection itself in the Usuario and make it available for other methods.

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

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

With the changes the method should stay that way:

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;
    }
}

0

You are passing wrong parameter in 2 places.


A way to correct:

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

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 direct consultation, it should also be corrected:

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

or

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

Documentation: mysqli_query, mysqli_prepare

Browser other questions tagged

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