Memanggil foreign key di php

Saya sudah membuat 2 tabel:

Saya sudah membuat codenya:

1)index.php

<?php
$conn = mysql_connect("localhost","root","");
mysql_select_db("test_it",$conn);
$sql = "select * from tabel_nama inner join tabel_hobi on tabel_nama.nm_org=tabel_hobi.nm_hobi";
$result = mysql_query($sql);
?>
<html>
<head>
 <title>CRUD</title>
</style></head>
<body>
<table border="1" cellpadding="5" cellspacing="0">
 <tr bgcolor="#3b3b3b" style="color:#fff;">
  <th>Nama</th>
  <th>Hobi</th>
  <th>Option</th>
  </tr>
 <?php while($data = mysql_fetch_array($result)){?>
 <tr>
  <td><?php echo $data['nm_org'];?></td>
  <td><?php echo $data['nm_hobi'];?></td>
  <!-- tambah link edit & delete -->
  <td>
   <a href="edit.php?id=<?php echo $data['id'];?>">Edit</a> |
   <a href="delete.php?id=<?php echo $data['id'];?>">Delete</a>
  </td>
 </tr>
 <?php }?>
</table>
<br>
<br>
<!-- <a href="form.php" align="center">Input</a> -->
<input onclick="window.location.href = 'form.php'" type="button" value="Input" />
</body>

</html>

2)edit.php

<?php
$conn = mysql_connect("localhost","root","");
mysql_select_db("test_it",$conn);
if($_POST){

 $sql = "update nama set nm_org='{$_POST['nm_org']}'
 where id='{$_POST['id']}'";
 mysql_query($sql);

 $sql = "update hobi set nm_org='{$_POST['nm_org']}' where id='{$_POST['id']}'";
 mysql_query($sql);
 echo "Data telah di edit";
}
$hobi = (int) $_GET['id_hobi'];
$sql = "select * from tabel_nama inner join tabel_hobi on tabel_nama.id_nama=tabel_hobi.id_hobi where tabel_hobi.hobi='$nm_hobi'";
$result = mysql_query($sql);
$data = mysql_fetch_array($result);
?>
<form name="form1" action="" method="post">
 <dl>
  <dt>Nama</dt>
  <dd><input type="text" name="nama" value="<?php echo $data['nm_org'];?>"/></dd>

  </dd>
  <dt>Hobi</dt>
  <dd><input type="text" name="hobi" value="<?php echo $data['nm_hobi'];?>"/></dd>
 </dl>
 <input type="hidden" name="id" value="<?php echo $data['id'];?>"/>
</form>
<tr>
				<td></td>
				<td><input type="submit" value="OK!"></td>
			</tr>

3)simpan.php

<?php
if($_POST){
 $conn = mysql_connect("localhost","root","");
 mysql_select_db("test_it",$conn);
 if (isset($_POST['simpan'])) {
   $data = implode(",", $_POST['nm_hobi']);
   mysql_query("INSERT into nm_hobi(nm_hobi) VALUES('".$data."')");
  }

 $sql = "insert into tabel_nama (nm_org) values ('{$_POST['nm_org']}')";
 mysql_query($sql) or die('404');

 $sql = "select max(id) as last_id from tabel_nama limit 1";
 $hasil = mysql_query($sql);
 $row = mysql_fetch_array($hasil);
 $lastId = $row['last_id'];

 $sql = "insert into tabel_hobi (id_nm_org,id_nama)
 values ('$lastId','{$_POST['nm_hobi']}')";
 mysql_query($sql) or die('404');
 ?>
		<ul>
			<li><a href="index.php" align="center">LIST DATA</a></li>
		</ul>
		<?php
	}?>

4)form.php

<form name="form1" action="simpan.php" method="post">
 <dl>
  <dt>Nama</dt>
  <dd><input type="text" name="nama"/></dd>

  <tr>
    <td width="60px" valign="top">Hobi:</td>
    <br>
    <td valign="top">
     <label><input type="checkbox" name="hobi[]" value="Nonton">Nonton</label><br>
     <label><input type="checkbox" name="hobi[]" value="Menulis">Menulis</label><br>
     <label><input type="checkbox" name="hobi[]" value="Traveling">Traveling</label><br>
     <label><input type="checkbox" name="hobi[]" value="Otomotif">Otomotif</label><br>
     <label><input type="checkbox" name="hobi[]" value="Fotografi">Fotografi</label><br>
     <label><input type="checkbox" name="hobi[]" value="Programming">Programming</label>
    </td>
   </tr>
  <br>
  <br>
  <dd><input type="submit" value="Simpan"/></dd>
  <br>
  <dd><input onclick="window.location.href = 'index.php'" type="button" value="Kembali"/></dd>
 </dl>
</form>

5)delete.php

<?php
$id = (int) $_GET['id'];
if($id){
 $conn = mysql_connect("localhost","root","");
 mysql_select_db("test_it",$conn);

 mysql_query("delete from kolom_nama where id='{$id}'");

 mysql_query("delete from kolom_hobi where id='{$id}'");
}

header("Location: index.php");
exit;

Pertanyaannya:

  1. Bagaimana metode pemanggilan foreign key atau inner join?
  2. Cara agar tabel sinkron ke database ketika menginput?
  3. Ketika Saya menginput, tabel tabel_nama terisi tapi tidak dengan tabel_hobi.
  4. Bagaimana mencocokkan foreign key(id_nm_org) dengan primary key(id_nama)?
avatar barakamon
@barakamon

9 Kontribusi 0 Poin

Dipost 4 tahun yang lalu

1 Jawaban:

Jawaban Terpilih

Problem solved. :)

avatar barakamon
@barakamon

9 Kontribusi 0 Poin

Dipost 4 tahun yang lalu

Tanggapan

Saya salah di metode "INSERT INTO"nya.. hehehehe

bisa minta full codingannya yang sudah bener ga mas, saya mau belajar

Login untuk ikut Jawaban