Help Select with prepare

Asked

Viewed 30 times

-2

I’m trying to make a select using the prepare. However, it simply cannot do this select and returns me that the user or password is wrong. Code below:

<?php
session_start();

include("conexao.php");

$consulta = $conexao->prepare("SELECT * FROM login WHERE login = ? AND senha = ?");
$consulta->bind_param("ss", $login, $senha);
$login = mysqli_real_escape_string($conexao, $_POST['login']);
$senha = mysqli_real_escape_string($conexao, $_POST['senha']);
$consulta->execute();
$row = $consulta->num_rows;

if($row == 1):
    $_SESSION['login'] = $login;
    header("Location: ../../boa/index.php");
    exit;
    else:
    $_SESSION['nao_logado'] = true;
    header("Location: ../index.php");
    exit;
    endif;

The way I’m doing this wrong? Thank you.

  • You are using the variables $login and $senha before initializing them.

  • It does not proceed your information, since I am using "PREPARE". In this case, I am running, after the variables.

  • In bind_param you are using the variables $login and $senha, that have not even been initialized yet. What does not proceed?

  • My God! My friend, if you don’t know about PREPARE, please don’t comment. Study a little https://www.php.net/manual/en/mysqli.prepare.php

  • https://www.php.net/manual/en/mysqli-stmt.bind-param.php

  • 1

    What is the need of mysqli_real_escape_string if you are already using a Prepared statement?

  • I tried to use without it, but still incorrect password

  • In var_dump it always shows the " public 'num_rows' => int 0"

  • Try to get the result with get_result() to see why this is happening

  • Already solved friend, grateful.

Show 5 more comments

1 answer

-1

The problem was that I was not shown php the result if it was positive or negative. Correct code below:

<?php
session_start();

include("conexao.php");

$consulta = $conexao->prepare("SELECT * FROM login WHERE login =? AND senha =?");
$consulta->bind_param("ss", $login, $senha);
$login = $_POST['login'];
$senha = $_POST['senha'];
$consulta->execute();
$consulta->store_result();
$row = $consulta->num_rows;


if($row == 1):
    $_SESSION['login'] = $login;
    header("Location: ../../boa/index.php");
    exit;
    else:
    $_SESSION['nao_logado'] = true;
    header("Location: ../index.php");
    exit;
    endif;

Browser other questions tagged

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