Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have to join three tables together to be able to present a query but I am not sure how, I keep on getting errors and I am not sure of the reason why.

The PHP code I was given is :
<html>
<body>
<link rel="stylesheet" href="laporan.css">  
<?php
 $pendaftaran = $_POST["kod_pendaftaran"];
 $con = mysqli_connect("localhost","root","");
 if (!$con)
  die('SAMBUNGAN TDK BERJAYA'.mysql_connect_error());
  mysqli_select_db($con, "spak");
  print"<h3>SENARAI MAKLUMAT PENDAFTARAN </h3>";
  print"<h3> MAKLUMAT PENDAFTARAN: ".$pendaftaran."</h3>";
  print"<table border = '1'>";
  print"<tr>";
  print"<th>kod_pendaftaran</th>";
  print"<th>nama_pelajar</th>";
  print"<th>tingkatan</th>";
  print"<th>kelas</th>";
  print"<th>nama_kelab</th>";
  print"<th>guru_penasihat</th>";
  print"<th>tarikh_daftar</th>";
  print"</tr>";
  $sql = "select pendaftaran.kod_pendaftaran, maklumat_pelajar.nama_pelajar, maklumat_pelajar.tingkatan, maklumat_pelajar.kelas, maklumat_kelab.nama_kelab, maklumat_kelab.guru_penasihat, maklumat_kelab.tarikh_daftar
  from ((pendaftaran
  inner join maklumat_pelajar on pendaftaran.id_pelajar = maklumat_pelajar.id_pelajar)
  inner join maklumat_kelab on pendaftaran.kod_kelab = maklumat_kelab.kod_kelab)

 WHERE kod_kelab = "."'".$pendaftaran."'";


  $result = mysqli_query($con,$sql);
  while ($row = mysqli_fetch_array($result))
  {
  	echo' <tr> 
  	<td>'.$row["kod_pendaftaran"].'</td>
  	<td>'.$row["nama_pelajar"].'</td>
  	<td>'.$row['tingkatan'].'</td> 	
    <td>'.$row["kelas"].'</td> 
  	<td>'.$row["nama_kelab"].'</td>
  	<td>'.$row["guru_penasihat"].'</td>
  	<td>'.$row["tarikh_daftar"].'</td>
	     </tr>';
  }
  print"</table>";
  
 ?>
 </body>
 </html>


and the HTML page is :
<html>
<body>
<link rel="stylesheet" href="laporan.css">
<h3> SENARAI AHLI KELAB</h3>
<form action = "query3table1.php" method ="post">
	PILIH KELAB :
	<select id= "kod_pendaftaran" name="kod_pendaftaran">
	
      	<option value="K01">Kelab Pengguna</option>
      	<option value="K02">Kelab Kerjaya</option>
      	<option value="K03">Kelab Kesenian dan Kebudayaan</option>
      	<option value="K04">Kelab Pelancongan dan Fotografi</option>
      	<option value="K05">Kelab Tunas Bestari/ Keusahawanan</option>
      	<option value="K06">Kelab Pencegahan Jenayah</option>
      	<option value="K07">Kelab Pertanian</option>
        <option value="K08">Kelab Pusat Sumber</option>
      	<option value="K09">Kelab Koperatif Muda</option>
        <option value="K10">Kelab MUET</option>
      	<option value="K11">Kelab Debat</option>
      	
	</select>

	
	<input type ="submit" value="Cari" name="submit">
</form>
</body>
</html>


What I have tried:

I have tried switching up the coding with ones that I found on the internet but it doesn't seem to work.
Posted
Updated 26-Jun-18 20:39pm
Comments
David_Wimbley 26-Jun-18 13:59pm    
Without detailing what the error is, no one is going to really be able to help you as we can't run your code since we dont have access to your computer or your servers.

You aught to update your question with what your error message is and if possible, post your DB schema with some sample data so it is easy to replicate your join errors.
Richard Deeming 26-Jun-18 14:22pm    
Agreed, but it's "ought[^]", not "aught[^]". :)

1 solution

SQL
select 
	pendaftaran.kod_pendaftaran, 
	maklumat_pelajar.nama_pelajar, 
	maklumat_pelajar.tingkatan, 
	maklumat_pelajar.kelas, 
	maklumat_kelab.nama_kelab, 
	maklumat_kelab.guru_penasihat, 
	maklumat_kelab.tarikh_daftar
from 
(
	(
		pendaftaran
			inner join maklumat_pelajar on pendaftaran.id_pelajar = maklumat_pelajar.id_pelajar
	)
			inner join maklumat_kelab on pendaftaran.kod_kelab = maklumat_kelab.kod_kelab
)
where kod_kelab = ''

When you formating your SQL, you can see that you just do not follow the right syntax for SELECT-FROM-JOIN-WHERE...
Read the manual: MySQL :: MySQL 8.0 Reference Manual :: 13.2.10.2 JOIN Syntax[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900