0

So I'm trying to check if an user exist before deleting it (if user exists, it will compare the passwords and then the user will be deleted), but I'm stuck in the comprobation of the user existing.

At beggining, I was making the user existing and password matching inside the isset($_REQUEST), but I thought making functions for these things will be cleaner and clearer for me (I was having problems making everything inside $_REQUEST). So I decided to make a function to check if an user exists in the database and I want it to be called inside my $_REQUEST(botonBaja) button.

What am I doing wrong?

I have this line at beggining, so $conexion is not out of scope:

<?php
include 'conexionBBDD.php';


function checkUserExist($nif){
$selectPass = "SELECT PASSWORD FROM votante WHERE NIF='".$nif."';";
$resultado= $conexion->query($selectPass);
 if (mysqli_num_rows($resultado) == 0) {
    return false;
 }else return true;
}

The previous code is called by this isset($_REQUEST), which is a button (the button is working well). $nif and $password are being collected well, trust me, I've debugged it, it and it stops when it reaches the function.

if (isset($_REQUEST['botonBaja'])) { //DELETE USERS
   $nif = $_REQUEST['nif'];
   $password = $_REQUEST['password'];

   if (checkUserExist($nif)){
       echo "The user already exist";
   }else echo "The user doesn't exist";
}

(Both codes are in the same php file)

Sandrituky
  • 131
  • 1
  • 3
  • 12
  • 1
    Possible duplicate of [Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?](https://stackoverflow.com/questions/16959576/reference-what-is-variable-scope-which-variables-are-accessible-from-where-and) –  Feb 12 '19 at 20:18
  • 2
    $conexion is out of scope –  Feb 12 '19 at 20:19
  • Add "global $conexion;" inside function checkUserExist($nif) – ARN Feb 12 '19 at 20:25
  • yes it is, that's not how variable scope works, read the duplicate. –  Feb 12 '19 at 20:27
  • You were right! I had to type "global conexion" at beggining of the function! Thank you! – Sandrituky Feb 12 '19 at 20:30

0 Answers0