How to search and show data

Asked

Viewed 118 times

2

Well. I need to do a search in one table and through this search pull the data from another. But I ended up getting lost in logic ...

Example of what I’m doing:

The user informs the data he wants:

    <td >
        <font >Nota:</font>
    </td>
    <td >
        <input name="tx_nota_fisc" type="text" maxlength="15" size="10" value="">
        <button type="button" style="width:30" onClick="f_veri_nota();"><image src="../sai_imag/ref1.ico" > </button>                   
    </td>

f_veri_nota() calls the next screen by passing the data that was typed.

w_tx_nota = document.sai_frm_alte_novo_cara_peri.tx_nota_fisc.value;
document.getElementById("ifrm_peri").src = ("sai_frm_alte_novo_cara_peri1.php?w_tx_nota="+w_tx_nota);

And in the screen input I check if the typed data is in the tabela Nota. And in case I do select catching the sequencia of that typed note and compare it with the fk_nota that I have in another table, so take the data.

<?
$w_tx_nota = $_GET['w_tx_nota'];
$w_querybusca = "select * from sai_nota where num_nf = '$w_tx_nota';"; --> verifica se existe o dado digitado na tabela

$w_querybusca = "select " --> E nesse select quero pegar a sequencia do num_nf e comparar ele com uma fk que se encontra em outra tabela. Para assim popular os campos.
?>

The problem: How to take the sequence of a text field and compare it with the fk in another table?

Structure:

create table sai_nota
(
  sequencia_nota integer not null,
  num_nf character varying(9)
)

create table peri
(
  sequencia_peri integer not null,
  fk_nota serial not null,
  //outros campos
)

*Fictitious names

  • 2

    I couldn’t quite grasp your question. Could you rewrite it or explain it better?

  • It’s like you have the name and the Cpf in one table and the rest of the individual’s information in another table?

  • You can include in the question the structures of the tables involved, so we understand better?

  • That’s right @Marcosvinicius, and Cpf is like fk in that other table!

  • You can or want to do all this in one query and send the result to the other page @Bruno?

  • It could be in a single query, because I think it would be a simpler way to work @Marcosviniciusnasc.Pereira!

  • The crowd is already responding but improves the name of these attributes/variables, both in the bank and in the code I guarantee you will not regret. From a read on "Names of variables..."

  • I mentioned in the question, these are "fictitious names" !

Show 3 more comments

1 answer

3


If I understand correctly, you can do everything in a single query, no need to separate:

SELECT
    peri.* /* substitua pela lista de campos que quer */
FROM sai_nota
    INNER JOIN peri
    ON peri.fk_nota = sai_nota.sequencia_nota
WHERE num_nf = '$w_tx_nota';

I’d use one too query parameterized, with mysqli or PDO, instead of embedding the PHP variable directly into the query string. This would make you less vulnerable to SQL Injection.

  • @ bfavaretto, that’s just what I needed! Thank you!

  • I’m happy to help!

Browser other questions tagged

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