Ir para conteúdo

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

Michelangela

[RESOLVIDO] Comparando campos de tabelas diferentes

Recommended Posts

Caros Colegas,

 

Como sou iniciante em php eu estou com uma certa dificuldade em comparar campos (iguais) de duas tabelas, e se for verdade ele executa a ação.

 

O sistema é o seguinte:

É um questionário que o aluno vai responder, e para acessar este formulário ele digita sua matrícula, se a matricula tiver cadastrada na tabela de aluno e não estiver gravada na tabela de resposta, ele responde o questionário.

 

Com muito suor ^_^ consegui fazer com que ele compare se a matricula digitada já foi cadastrada na tabela de resposta... Mas eu queria que visse se a matricula digitada é de um aluno (cadastrado na tabela de aluno) e ver se esta mesma matricula já foi cadastrada na tabela que grava as respostas.

 

Minhas tabelas estão assim:

 

Tabela "alunos"

 

| matricula | nome |

 

| 021183 | Michele |

 

Tabela "resposta"

 

| matricula | perg01 | data |

 

| 021183 | 1 | 2007-06-04 18:06:30|

 

E meu código está assim (form.php):

 

<body><form id='form1' name='form1' method='post' action='envia.php'><input name='matricula' type='hidden' value='<?=$_POST['matricula'];?>' /><?phpinclude "conn.php";$matricula = $_POST['matricula'];// BUSCA NO BANCO DE DADOS A MATRICULA$sql = mysql_query("SELECT * FROM resposta WHERE matricula = '$matricula' ") or print (mysql_error());$rowsql =  mysql_num_rows($sql);$val = mysql_fetch_array($sql);// Verifica se a matricula digitada ja esta cadastrada no banco de respostasif($rowsql>=1 && $val['matricula']==$matricula){echo"Você já respondeu este Formulário!";// VER SE O Nº DA MATRÍCULA É VÁLIDO}elseif ( ($matricula == '000000') || ($matricula == '111111') || ($matricula == '222222') || ($matricula == '333333') || ($matricula == '444444') || ($matricula == '555555') || ($matricula == '666666') || ($matricula == '777777') || ($matricula == '888888') || ($matricula == '999999')){echo"Digite uma matrícula válida!";// SE O ALUNO NÃO RESPONDEU O QUESTIONÁRIO APARECE O QUESTIONÁRIO}else{echo"   <h3>Teste de Avaliação</h3>  <p>1ª) Pergunta 01</p>  <p>	<label>	<input type='radio' name='perg01' value='1' />resposta 01</label>	<br />	<label>	<input type='radio' name='perg01' value='2' />resposta 02</label>	<br />	<label>	<input type='radio' name='perg01' value='3' />resposta 03</label>	<br />	<label>	<input type='radio' name='perg01' value='4' />resposta 04</label>	<br />	<label>	<input type='radio' name='perg01' value='5' />resposta 05</label>	<br />  </p>  <input type='submit' name='Submit' value='enviar' />";}?></form> </body></html>
Será que alguém pode me ensinar como faço?

Compartilhar este post


Link para o post
Compartilhar em outros sites

coloca uma queri como fez nas respostas só que para a tabela de alunostipo$sql = mysql_query("SELECT matricula FROM alunos WHERE matricula = '$matricula' ");$rowsql = mysql_num_rows($sql);if($rowsql == 0){ echo "Somente para alunos cadastrados!"; exit;}

Compartilhar este post


Link para o post
Compartilhar em outros sites

coloca uma queri como fez nas respostas só que para a tabela de alunostipo$sql = mysql_query("SELECT matricula FROM alunos WHERE matricula = '$matricula' ");$rowsql = mysql_num_rows($sql);if($rowsql == 0){ echo "Somente para alunos cadastrados!"; exit;}

Você diz pra eu colocar onde? depois do 1º select? Onde eu encaixo ?

Compartilhar este post


Link para o post
Compartilhar em outros sites

Na mesma query, você já pode consultar pela matrículo em ambas as tabelas. Algo assim:

 

SELECT resposta.* FROM resposta, alunos WHERE resposta.matricula=alunos.matricula AND resposta.matricula='$matricula'

Detalhe: como você tem uma condição ali que informa se o aluno já respondeu, se a matrícula é válida ou se exibirá o questionário, é interessante separar em duas queries, uma para a consulta das respostas, outra para a validação da matrícula. Assim você continua informando o usuário corretamente.

 

Tente aí. Qualquer coisa, posta ;)

 

[]s

Anderson Mello

Compartilhar este post


Link para o post
Compartilhar em outros sites

Deu certo!

 

Eu fiz assim:

 

<form id='form1' name='form1' method='post' action='envia.php'><input name='matricula' type='hidden' value='<?=$_POST['matricula'];?>' /><?phpinclude "conn.php"; $matricula = $_POST['matricula'];// VERIFICA SE O ALUNO ESTA CADASTRADO NA TABELA DE ALUNOS$sql = mysql_query("SELECT * FROM alunos WHERE matricula = '$matricula' ") or print (mysql_error());$rowsql = mysql_num_rows($sql);if($rowsql == 0){echo "Somente para alunos cadastrados!";exit;} ?><?php// BUSCA NO BANCO DE DADOS A MATRICULA$sql = mysql_query("SELECT * FROM resposta WHERE matricula = '$matricula' ") or print (mysql_error());$rowsql =  mysql_num_rows($sql);$val = mysql_fetch_array($sql);// Verifica se a matricula digitada ja esta cadastrada no banco de respostasif($rowsql>=1 && $val['matricula']==$matricula){echo"Você já respondeu este Formulário!";// VER SE O Nº DA MATRÍCULA É VÁLIDO}elseif ( ($matricula == '000000') || ($matricula == '111111') || ($matricula == '222222') || ($matricula == '333333') || ($matricula == '444444') || ($matricula == '555555') || ($matricula == '666666') || ($matricula == '777777') || ($matricula == '888888') || ($matricula == '999999')){echo"Digite uma matrícula válida!";// SE O ALUNO NÃO RESPONDEU O QUESTIONÁRIO APARECE O QUESTIONÁRIO}else{echo"   <h3>Teste de Avaliação</h3>  <p>1ª) Pergunta 01</p>  <p>	<label>	<input type='radio' name='perg01' value='1' />resposta 01</label>	<br />	<label>	<input type='radio' name='perg01' value='2' />resposta 02</label>	<br />	<label>	<input type='radio' name='perg01' value='3' />resposta 03</label>	<br />	<label>	<input type='radio' name='perg01' value='4' />resposta 04</label>	<br />	<label>	<input type='radio' name='perg01' value='5' />resposta 05</label>	<br />  </p>  <input type='submit' name='Submit' value='enviar' />";}?></form>

Ta muito bagunçado né? :rolleyes: Mas oq importa é q ta pegando!

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.