Cara menggunakan checkbox value php

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters

<html>
<head>
<title>Menginput checkbox ke database</title>
</head>
<body>
<?php
mysql_connect('localhost','root','');
mysql_select_db('test');
//membuat fungsi untuk men-cek hobi yang terpilih ketika mode edit
function is_hoby_selected($id_hoby,$nim){
$jml=mysql_num_rows(mysql_query("select * from mhs where nim='$nim' and hobi like '%$id_hoby%'"));
return $jml;
}
//menyiapkan jenis hobi kedalam aray
$hobi=array('Baca','Menulis','Olah Raga','Berkebun','Nge-Game');
//mengambil data ketika edit
if(!empty($_GET['nim'])){
$detail_siswa=mysql_fetch_array(mysql_query("select * from mhs where nim=$_GET[nim]"));
}
//ketika link hapus di klik
if(!empty($_GET['hapus'])){
$hapus_siswa=mysql_query("delete from mhs where nim=$_GET[hapus]");
}
?>
<!--table utama-->
<h3>Contoh Menyimpan Value Form Checkbox ke MySQL Dalam Satu Field</h3>
<table border="0" width="100%">
<tr valign="top">
<td width="20%">
<!-- ==================== FORM SEBELAH KIRI ===================== -->
<form method="POST">
Nomor Induk : <br/>
<input type="text" name="nim" value="<?php if(!empty($_GET['nim'])){ echo $detail_siswa['nim'];}?>"/><br/>
Nama Lengkap : <br/>
<input type="text" name="nama" value="<?php if(!empty($_GET['nim'])){ echo $detail_siswa['nama'];}?>"/><br/>
Hobby : <br/>
<?php
for ($a=0;$a<count($hobi);$a++) {
if(!empty($_GET['nim'])){
if(is_hoby_selected($a,$_GET['nim'])==1){
echo '<input type="checkbox" name="hobi[]" value="'.$a.'" checked="checked"/>'.$hobi[$a].'<br/>';}
else {
echo '<input type="checkbox" name="hobi[]" value="'.$a.'"/>'.$hobi[$a].'<br/>';
}
} else {
echo '<input type="checkbox" name="hobi[]" value="'.$a.'"/>'.$hobi[$a].'<br/>';
}
}
?>
<input type="submit" name="simpan" value="Simpan"/><br/>
</form>
<!-- ==================== AKHIR FORM ===================== -->
</td>
<td width="80%" align="center">
<!-- ==================== TABEL DATA SEBEAH KANAN ===================== -->
<b>Data Siswa</b>
<?php
$sql_siswa=mysql_query("select * from mhs order by nim");
?>
<!--table data siswa-->
<table border="1" width="98%" align="center">
<tr><th>NIM</th><th>Nama</th><th colspan="2">Hobi</th></tr>
<?php
while($data_siswa=mysql_fetch_array($sql_siswa)){
echo "<tr valign=\"top\">
<td><a href=\"index.php?nim=$data_siswa[nim]\">$data_siswa[nim]</a></td>
<td>$data_siswa[nama]</td>
<td>";
$hobi_siswa_tmp=explode(',',$data_siswa['hobi']);
for ($c=0;$c<count($hobi_siswa_tmp);$c++){
$satu_hobi=$hobi_siswa_tmp[$c];
echo $hobi[$satu_hobi].", ";
};
echo '</td><td><a href="index.php?hapus='.$data_siswa['nim'].'">Hapus</a></td></tr>';
}
?>
</table>
<!-- ==================== Akhir TABEL DATA SEBEAH KANAN ===================== -->
</td>
</tr>
</table>
<?php
// PROSES PENYIMPANAN
if(!empty($_POST['simpan'])){
$hobinya='';
$jml_data=count($hobi);
$hobi_dipilih=$_POST['hobi'];
for($b=0;$b<count($_POST['hobi']);$b++){
$hobinya=$hobinya.$hobi_dipilih[$b].',';
}
$hobi_to_sql=substr(strrev($hobinya),1);
$simpan=mysql_query("insert into mhs values ('$_POST[nim]','$_POST[nama]','$hobi_to_sql')");
echo("<META HTTP-EQUIV=Refresh CONTENT=\"0.1;URL=index.php\">");
}
// AKHIR PROSES PENYIMPAAN
?>
</html>