How do I check for registration in php?

Asked

Viewed 166 times

-3

$nome_cad = $_POST["nome_ent_c"];
$cpf_cad = $_POST["cpf_ent_c"];
$tel_cad = $_POST["tel_ent_c"];
$end_cad = $_POST["end_ent_c"];
$invite_cad = $_POST["cod_convite"];
$codgerado = $_POST["codgerado"];
$senha = $_POST["senhacad"];
$username = $_POST["user_ent_c"];

$selectinvite = "select codinvitelogin from login";

$resultado_select = mysqli_query($conn, $selectinvite);
$resultado_select -> fetch_all(MYSQLI_ASSOC);

if($invite_cad == $resultado_select){
        
    $comandoSQL = "INSERT INTO login (nomelogin,cpflogin,telefone,endlogin,codigologinusado,codinvitelogin,senha,username) VALUES ('$nome_cad','$cpf_cad','$tel_cad','$end_cad','$invite_cad','$codgerado','$senha','$username');";
    $_SESSION['msg'] = "<span style='color: green';>Administrador cadastrado com sucesso!</span>";
    header("Location: cadastrologin.php");   
    
}else{
    $_SESSION['msg'] = "<span style='color: red';>Erro: Administrador nao cadastrado com sucesso!</span>";
    header("Location: cadastrologin.php");
}

I wanted when the user typed his invitation code on input the program checks if the code reported exists in the database; if true (exists), it inserts the register. If it does not exist, display error message.

This code I made does not return errors, but also does not work.

  • fetch_all returns a array. You must enter the index "codinvitelogin" when checking if the condition is true. You can also use the query SELECT codinvitelogin FROM login WHERE codinvitelogin = '$invite_cad';. Use the function mysqli_error to verify possible errors

2 answers

0

1 - Taking advantage of your routine

The fetch_all() function by default returns a array

$resultado_select -> fetch_all(MYSQLI_ASSOC);

see how to "print" the array

$resultArray = $result->fetch_all(MYSQLI_ASSOC);
print_r($resultArray);

Returned example

Array ( [0] => Array ( [codinvitelogin] => 12345 ) [1] => Array ( [codinvitelogin] => 23456 ) [2] => Array ( [codinvitelogin] => 34567 ) [3] => Array ( [codinvitelogin] => 45678 ) [4] => Array ( [codinvitelogin] => 03422 ) [5] => Array ( [codinvitelogin] => 23478 ) )

When comparing the variable $invite_cad with the array will always give false

if($invite_cad == $resultado_select){

Use a foreach and check whether the variable value exists in the array $invite_cad

$result = "";

foreach ($resultArray as $item) {   // $resultArray é o array inicial
    if ($item['codinvitelogin'] === $invite_cad) {
        //se o valor da variavel $invite_cad existir no array atribuímos um valor a variável $result
        $result = "cadastrar";
        break;
    }
}

Life that follows :-)

if($result == "cadastrar"){

    $comandoSQL = "INSERT .......             
    $_SESSION['msg'] = "<span style='color: blue';>OK: Administrador cadastrado com sucesso!</span>";
    
}else{

       $_SESSION['msg'] = "<span style='color: red';>Erro: Administrador nao cadastrado com sucesso!</span>";

}

 header("Location: cadastrologin.php");

2 - A more direct way

<?php
session_start();

$servidor = "localhost";
$usuario = "root";
$senha = "";
$dbname = "bancotcc";
$conn = mysqli_connect($servidor, $usuario, $senha, $dbname);


if (!$conn) {
    die("Connection pifada: " . mysqli_connect_error());
}

$invite_cad = $_POST["cod_convite"];


//Utilize a cláusula WHERE que é usada para extrair apenas os registros que atendem a uma condição especificada
$selectinvite = "select codinvitelogin from login WHERE codinvitelogin = '$invite_cad'";

$resultado_select = mysqli_query($conn, $selectinvite);

//resultado da query
$resultado_select = mysqli_fetch_assoc($resultado_select);

if($invite_cad == $resultado_select['codinvitelogin']){

    $nome_cad = $_POST["nome_ent_c"];
    $cpf_cad = $_POST["cpf_ent_c"];
    $tel_cad = $_POST["tel_ent_c"];
    $end_cad = $_POST["end_ent_c"];
    $codgerado = $_POST["codgerado"];
    $senha = $_POST["senhacad"];
    $username = $_POST["user_ent_c"];
    
    $comandoSQL = "INSERT INTO login (nomelogin,cpflogin,telefone,endlogin,codigologinusado,codinvitelogin,senha,username)
    VALUES ('$nome_cad','$cpf_cad','$tel_cad','$end_cad','$invite_cad','$codgerado','$senha','$username')";
    
    $result = mysqli_query($conn, $comandoSQL);  
    
    $_SESSION['msg'] = "<span style='color: green';>OK: Administrador cadastrado com sucesso!</span>";
    
}else{
       $_SESSION['msg'] = "<span style='color: red';>Erro: Administrador nao cadastrado com sucesso!</span>";

}

header("Location: cadastrologin.php");

mysqli_close($conn);

?>
  • Hello, good night, good night! I am trying to use the most direct form, because it seems simpler to me, it is 'working', because it returns that was registered successfully, but when I put to enter the data in the database, as successfully registered, but does not insert, would know the pq? pfv

  • @Bruno, how is the connection string?

  • $server = "localhost"; $user = "root"; $password = ""; $dbname = "bancotcc"; //Create connection $Conn = mysqli_connect($server, $user, $password, $dbname);

  • @Bruno, did you see the edited answer? managed to solve?

  • Yes, thank you, from my heart

0

Opa Bruno.

First, if your PHP version is too old, fetch_all does not work. Typically fetch_assoc is more used.

Anyway, one of the problems is that your $resultado_select is not a string and you’re comparing it to a string. As much as you want to return only the codinvitelogin field, the return of fetch_all is a vector (because when performing a select, you can have multiple lines as a return). I mean, your if will always give false.

To change that, you would need to run through the vector and compare to the field directly, something like:

foreach ($resultado_select as $linha) {
  if($invite_cad == $linha->codinvitelogin){ //você precisa falar com qual campo daquela linha vai comparar
    //código a ser executado;
  }    
}

However, a simpler way to do this would be to search the table directly if there is any record with that data. You can do this through a query concatenation similar to this:

$query = "select codinvitelogin from login where codinvitelogin = " . $_POST["cod_convite"];

Therefore, if there is return above, it means that the user code is valid, otherwise the return will be an empty vector.

if(empty($resultado_array)) //ou !empty() caso queira verificar em ordem contrária
    echo "Convite inválido"; 
else
    echo "Convite válido"; 

Browser other questions tagged

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