Array with PHP and Postgresql

Asked

Viewed 90 times

0

Hi, I have a problem with the feedback from the database. It’s returning me a cluttered array and with information from another bank user.

Here is the Index:

<?php
  SESSION_START();
?>
<!DOCTYPE html>
<html lang = "pt-br">
  <head>
    <!-- MetaTags para aceitar caracteres especiais, ajustar compatibilidade com navegadores e dispositivos móveis -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- Título -->
    <title>Test</title>

    <!-- Bootstrap CSS -->
    <link href="css/bootstrap.css" rel="stylesheet">
    <!-- Font-Awesome CSS -->
    <link href="css/font-awesome.min.css" rel="stylesheet">
  </head>
  <body>
    <?php echo '<pre>' . print_r($_SESSION, TRUE) . '</pre>';?>
    <!-- jQuery -->
    <script src="js/jquery.min.js"></script>
    <!-- Bootstrap JS -->
    <script src="js/bootstrap.min.js"></script>
    <script src="js/login.js"></script>
    <script>
  </body>
</html>

This is my PHP code:

include_once('connect.php');
    $msg_ok=false;
    $msg_error='Sistema fora do ar.';
    if(isset($_POST['telefone'], $_POST['nome'])):
        if($_POST['telefone']!=""):
            if($_POST['nome']!=""):
            $telefone=$_POST['telefone'];
            $nome=$_POST['nome'];
            $consulta=pg_query($connect, ("SELECT nome, cpf FROM mailing WHERE telefone='$telefone'"));
            if(pg_num_rows($consulta)>0):
                $msg_ok=true;
                $tel=pg_fetch_array($consulta);
                session_start();
                $msg_error="Logado corretamente.";
            else:
                $msg_error="Usuário ou nome incorretos.";
            endif;
        else:
            $msg_error="nome incorreta.";
        endif;
    else:
        $msg_error="Usuário não existe.";
    endif;
else:
    $msg_error="Todos os dados requiridos.";
endif;
$saidaJson=array('resposta' => $msg_ok, 'msg' => $msg_error);
echo json_encode($saidaJson);

I just want the user to enter the system by their mobile number (instead of a login and password), then PHP will return me an array with the name, age, etc...

I’m having a hard time making this return, but I’m researching, I don’t have much experience with PHP or Postgresql, so I get a little lost.

  • Let me understand. Do you want me to return an array with the data from a login? If you want to do an authentication scheme, you should return only 1 result existing or not. Now the question of return of disordered data, I could not understand very well. Maybe this your telephone is returning more than one user, because in your bank is registered more than once the same phone.

  • I managed to fix the error, below "session_start();" I declared nothing, my session was empty and the logs my browser was displaying were from a session I had opened before modifying my PHP code. To resolve this error I only declared variables to store the Postgresql return values: $_SESSION['name']=$tel[0];

No answers

Browser other questions tagged

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