How to return values from two distinct php tables

Asked

Viewed 173 times

2

I am a beginner and wanted to create a function that would return me from the name Cpf, user and password of the person in the system. Where the name and Cpf fields belong to the user table and the user and password fields belong to users.

Way I tried and failed

          <script type='text/javascript'>
                    $(document).ready(function(){
                        $("input[name='nome']").blur(function(){
                                var $cpf = $("input[name='cpf']");
                                var $usuario = $("input[name='usuario']");
                                var $senha = $("input[name='senha']");

                                $.getJSON('function.php',{ 
                                        nome: $( this ).val() 
                                },function( json ){
                                        $cpf.val( json.cpf );
                                        $usuario.val( json.usuario );
                                        $senha.val( json.senha );

                                });
                        });
                });
        </script> 

My Function.php

     function retorna($nome, $conn){
            $result = "SELECT CPF, USUARIO, SENHA FROM USUARIOS A INNER JOIN USUAR B ON B.NOME = A.NOME WHERE nome = '$nome' LIMIT 1";
            $resultado = mysqli_query($conn, $result);
            if($resultado->num_rows){
                    $row = mysqli_fetch_assoc($resultado);
                    $valores['cpf'] = $row['cpf'];
                    $valores['usuario'] = $row['usuario'];
                    $valores['senha'] = $row['senha'];

            }else{
                    $valores['cpf'] = '';
                    $valores['usuario'] = '';
                    $valores['senha'] = '';

            }

            return json_encode($valores);

    }

    if(isset($_GET['nome'])){
            echo retorna($_GET['nome'], $conn);
    }

1 answer

1

Are the two tables related? If so, how cod_usuario. I believe that it is not possible to make one select with inner setting 2 different types of values for the same field.
In this case I would recommend that you create a view in the bank and select it. Example :

create view vw_user as
  select a.cpf, b.usuario, b.senha  
  from usuario a, user b 
  where a.cod_usuario = b.cod_usuario
;

Where within php code you place the query

select * from vw_user where nome = $nome

Of course, claiming that the name of the user table person is the same as the name of the users table. Otherwise, I suggest that this login is done by Cpf number, or that it uses another key to find the user.

EDIT 1: I created a dummy database to illustrate my thinking (without adding the view, to make it simpler)

create database logincliente;
use logincliente;

create table pessoa(
cod_pessoa int not null auto_increment primary key,
nome_pessoa varchar(50)
);

create table login(
cod_login int not null auto_increment primary key,
cod_pessoa int not null,
login varchar(50),
senha varchar(50)
);

alter table login add constraint fk_loginPessoa foreign key (cod_pessoa) references pessoa (cod_pessoa);


insert into pessoa(nome_pessoa) values 
('Flávio'),
('Castro'),
('Reginaldo'),
('Péricles');

insert into login (cod_pessoa,login,senha) values 
(1,'flaviko','12345'),
(2,'kastr','stqvb'),
(3,'reginaldo','reginaldo'),
(4,'pokkter','033line');



select p.nome_pessoa, l.login, l.senha from pessoa p, login l where nome_pessoa = 'péricles' and p.cod_pessoa = l.cod_pessoa;

Try this last select, and see if this is the result you want :)

  • I tried to leave the same script and only change my select after creating the view and persists error .

  • I created an SQL script that basically illustrated what I tried to explain (I didn’t use view as it is not so necessary at the time)

  • it would take a javascript like the one I mentioned to fill in the next fields

  • this Insert I made you can adapt to your code

  • this select I made, just put a php variable in the middle

  • in case using this code within my Function.php does not give, I think I need to search a new script to fill all data from a field

Show 1 more comment

Browser other questions tagged

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