Sie sind auf Seite 1von 16

Materi CRUDS PHP

Menggunakan mysqli
1. buat file php cruds.php untuk menampilkan data dari database mysql dengan teknik mysqli
<?php
$conn= mysqli_connect("localhost","root","","pelatihan");
$result=mysqli_query($conn,"select * from peserta");
echo "<table border=1>";
echo "<tr><td align='center'>NOMOR</td><td align='center'>NPM</td>";
echo "<td align='center'>NAMA</td><td align='center'>PASSWORD</td>";
echo "<td align='center'>JENIS KELAMIN</td><td align='center'>AGAMA</td>";
echo "<td align='center'>HOBBY</td><td align='center'>KRITIK<td>";
echo "</tr>";
$nomor=0;
$nomor=$nomor+1;
while($row=mysqli_fetch_array($result))
{ echo "<tr><td>".$nomor."</td>";
echo "<td>".$row['NPM']."</td>";
echo "<td>".$row['NAMA']."</td>";
echo "<td>".$row['PASSWD']."</td>";
echo "<td>".$row['JENIS_KELAMIN']."</td>";
echo "<td>".$row['AGAMA']."</td>";
echo "<td>".$row['HOBBY']."</td>";
echo "<td>".$row['KRITIK']."</td>";
echo "</tr>";
$nomor++;
}
echo "</TABLE>";
?>
Hasilnya :

2. update cruds.php untuk proses penambahan form input data


<form method="post" >
<table>
<tr><td>NPM</td><td><input type="text" name="NPM" id="NPM" /></td></tr>
<tr><td>Nama</td><td><input type="text" name="nama" id="nama" /></td></tr>
<tr><td>Password</td><td><input
type="password" name="Password" id="Password"
/></td></tr>
<tr><td>Jenis Kelamin</td><td><input type="radio" name="JK" id="JK" value="Lakilaki"/>Laki-laki
<input type="radio" name="JK" id="JK"
value="Perempuan"/>Perempuan</td></tr>
<tr><td>Agama</td><td> <select name="agama" id="agama">
<option value="">[P i l i h]</option>

<option value="Islam">Islam</option>
<option value="Katholik">Katholik</option>
<option value="Protestan">Protestan</option>
<option value="Hindu">Hindu</option>
<option value="Budha">Budha</option>
<option value="Lainnya">Lainnya</option>
</select></td></tr>
<tr><td>Hobby</td><td><input type="checkbox" name="musik" value="Musik" />Musik
<input type="checkbox" name="membaca" value="Membaca" />Membaca
<input type="checkbox" name="komputer" value="Komputer"
/>Komputer</td></tr>
<tr><td>Kritik</td><td><textarea
wrap="OFF"
rows="5"
cols="20"
name="kritik"></textarea></td></tr>
<tr><td><input type="submit" name="save" id="save" value="Save"/></td></tr>
</table>
</form>
<?php
$conn= mysqli_connect("localhost","root","","pelatihan");
$result=mysqli_query($conn,"select * from peserta");
echo "<table border=1>";
echo "<tr><td align='center'>NOMOR</td><td align='center'>NPM</td>";
echo "<td align='center'>NAMA</td><td align='center'>PASSWORD</td>";
echo "<td align='center'>JENIS KELAMIN</td><td align='center'>AGAMA</td>";
echo "<td align='center'>HOBBY</td><td align='center'>KRITIK<td>";
echo "</tr>";
$nomor=$nomor+1;
while($row=mysqli_fetch_array($result))
{ echo "<tr><td>".$nomor."</td>";
echo "<td>".$row['NPM']."</td>";
echo "<td>".$row['NAMA']."</td>";
echo "<td>".$row['PASSWD']."</td>";
echo "<td>".$row['JENIS_KELAMIN']."</td>";
echo "<td>".$row['AGAMA']."</td>";
echo "<td>".$row['HOBBY']."</td>";
echo "<td>".$row['KRITIK']."</td>";
echo "</tr>";
$nomor++;
}
echo "</TABLE>";
?>
Hasilnya

3. materi selanjutnya adalah proses simpan data dari form ke mysql dengan perubahan yang harus
dilakukan sbb
update / ubah file cruds.php menjadi sbb:
<form method="post" action="crud_simpan.php" >
<table>
<tr><td>NPM</td><td><input type="text" name="NPM" id="NPM" /></td></tr>
<tr><td>Nama</td><td><input type="text" name="nama" id="nama" /></td></tr>
<tr><td>Password</td><td><input type="password" name="Password" id="Password" /></td></tr>
<tr><td>Jenis Kelamin</td><td><input type="radio" name="JK" id="JK" value="Laki-laki"/>Lakilaki
<input type="radio" name="JK" id="JK"
value="Perempuan"/>Perempuan</td></tr>
<tr><td>Agama</td><td> <select name="agama" id="agama">
<option value="">[P i l i h]</option>
<option value="Islam">Islam</option>
<option value="Katholik">Katholik</option>
<option value="Protestan">Protestan</option>
<option value="Hindu">Hindu</option>
<option value="Budha">Budha</option>
<option value="Lainnya">Lainnya</option>
</select></td></tr>
<tr><td>Hobby</td><td><input type="checkbox" name="musik" value="Musik" />Musik
<input type="checkbox" name="membaca" value="Membaca" />Membaca
<input type="checkbox" name="komputer" value="Komputer" />Komputer</td></tr>
<tr><td>Kritik</td><td><textarea
wrap="OFF"
rows="5"
cols="20"
name="kritik"></textarea></td></tr>
<tr><td><input type="submit" name="save" id="save" value="Save"/></td></tr>
</table>
</form>
<?php
$conn= mysqli_connect("localhost","root","","pelatihan");
$result=mysqli_query($conn,"select * from peserta");
echo "<table border=1>";
echo "<tr><td align='center'>NOMOR</td><td align='center'>NPM</td>";

echo "<td align='center'>NAMA</td><td align='center'>PASSWORD</td>";


echo "<td align='center'>JENIS KELAMIN</td><td align='center'>AGAMA</td>";
echo "<td align='center'>HOBBY</td><td align='center'>KRITIK<td>";
echo "</tr>";
$nomor=$nomor+1;
while($row=mysqli_fetch_array($result))
{ echo "<tr><td>".$nomor."</td>";
echo "<td>".$row['NPM']."</td>";
echo "<td>".$row['NAMA']."</td>";
echo "<td>".$row['PASSWD']."</td>";
echo "<td>".$row['JENIS_KELAMIN']."</td>";
echo "<td>".$row['AGAMA']."</td>";
echo "<td>".$row['HOBBY']."</td>";
echo "<td>".$row['KRITIK']."</td>";
echo "</tr>";
$nomor++;
}
echo "</TABLE>";
?>
Dan buat file crud_simpan.php
<?php
$npm
= $_POST['NPM'];
$nama
= $_POST['nama'];
$passwd
= $_POST['Password'];
$JK
= $_POST['JK'];
$agama
= $_POST['agama'];
$musik
= $_POST['musik'];
$membaca
= $_POST['membaca'];
$komputer
= $_POST['komputer'];
$kritik
= $_POST['kritik'];
if (isset($musik)){$hobby = $musik; };
if (isset($membaca)){$hobby = $hobby.','.$membaca; };
if (isset($komputer)){$hobby = $hobby.','.$komputer; };
$conn = mysqli_connect("localhost","root","","pelatihan");
$query = "INSERT INTO peserta (nama,npm,passwd,jenis_kelamin,agama,hobby,kritik)
VALUES('".$nama."','".$npm."','".$passwd."','".$JK."','"
.$agama."','".$hobby."','".$kritik."')";
mysqli_query($conn,$query) or die("err".mysqli_error());
header('location: cruds.php');
?>
Hasilnya ketika diinput sbb:

Ketika ditekan save hasilnya sbb

4. selanjutnya menambahkan proses delete , perlu ditambahkan tombol delete atau link delete di recordnya
<form method="post" action="crud_simpan.php" >
<table>
<tr><td>NPM</td><td><input type="text" name="NPM" id="NPM" /></td></tr>
<tr><td>Nama</td><td><input type="text" name="nama" id="nama" /></td></tr>
<tr><td>Password</td><td><input type="password" name="Password" id="Password" /></td></tr>
<tr><td>Jenis Kelamin</td><td><input type="radio" name="JK" id="JK" value="Laki-laki"/>Laki-laki
<input type="radio" name="JK" id="JK" value="Perempuan"/>Perempuan</td></tr>
<tr><td>Agama</td><td> <select name="agama" id="agama">
<option value="">[P i l i h]</option>
<option value="Islam">Islam</option>
<option value="Katholik">Katholik</option>
<option value="Protestan">Protestan</option>
<option value="Hindu">Hindu</option>
<option value="Budha">Budha</option>
<option value="Lainnya">Lainnya</option>
</select></td></tr>
<tr><td>Hobby</td><td><input type="checkbox" name="musik" value="Musik" />Musik
<input type="checkbox" name="membaca" value="Membaca" />Membaca
<input type="checkbox" name="komputer" value="Komputer" />Komputer</td></tr>
<tr><td>Kritik</td><td><textarea
wrap="OFF"
rows="5"
cols="20"
name="kritik"></textarea></td></tr>
<tr><td><input type="submit" name="save" id="save" value="Save"/></td></tr>
</table>
</form>

<?php
$conn= mysqli_connect("localhost","root","","pelatihan");
$result=mysqli_query($conn,"select * from peserta");
echo "<table border=1>";
echo "<tr><td align='center'>NOMOR</td><td align='center'>NPM</td>";
echo "<td align='center'>NAMA</td><td align='center'>PASSWORD</td>";
echo "<td align='center'>JENIS KELAMIN</td><td align='center'>AGAMA</td>";
echo "<td align='center'>HOBBY</td><td align='center'>KRITIK</td>";
echo "<td align='center'>DELETE</td>";
echo "</tr>";
$nomor=$nomor+1;
while($row=mysqli_fetch_array($result))
{ echo "<tr><td>".$nomor."</td>";
echo "<td>".$row['NPM']."</td>";
echo "<td>".$row['NAMA']."</td>";
echo "<td>".$row['PASSWD']."</td>";
echo "<td>".$row['JENIS_KELAMIN']."</td>";
echo "<td>".$row['AGAMA']."</td>";
echo "<td>".$row['HOBBY']."</td>";
echo "<td>".$row['KRITIK']."</td>";
echo "<td><a href='delete.php?npm=$row[0]'>Delete</a></td>";
echo "</tr>";
$nomor++;
}
echo "</TABLE>";
?>
Hasilnya sbb:

Buat file php delete.php untuk proses delete sbb:


<?php
$npm = $_GET['npm'];
$conn=mysqli_connect("localhost","root","","pelatihan");
$querydelete = "delete from peserta where NPM ='$npm' ";
mysqli_query($conn,$querydelete);
header ('location: cruds.php');
?>

Jika salah satu link diclick(missal mistar gawang1234)

5. selanjutnya kita tambahkan proses edit / update dengan cara sbb


- kita tambahkan link atau tombol di tiap record
- kemudian kita update cruds.php untuk bisa membaca data dari yang kita pilih
<form method="post" action="crud_simpan.php" >
<table>
<tr><td>NPM</td><td><input type="text" name="NPM" id="NPM" /></td></tr>
<tr><td>Nama</td><td><input type="text" name="nama" id="nama" /></td></tr>
<tr><td>Password</td><td><input type="password" name="Password" id="Password" /></td></tr>
<tr><td>Jenis Kelamin</td><td><input type="radio" name="JK" id="JK" value="Laki-laki"/>Laki-laki
<input type="radio" name="JK" id="JK" value="Perempuan"/>Perempuan</td></tr>
<tr><td>Agama</td><td> <select name="agama" id="agama">
<option value="">[P i l i h]</option>
<option value="Islam">Islam</option>
<option value="Katholik">Katholik</option>
<option value="Protestan">Protestan</option>
<option value="Hindu">Hindu</option>
<option value="Budha">Budha</option>
<option value="Lainnya">Lainnya</option>
</select></td></tr>
<tr><td>Hobby</td><td><input type="checkbox" name="musik" value="Musik" />Musik
<input type="checkbox" name="membaca" value="Membaca" />Membaca
<input type="checkbox" name="komputer" value="Komputer" />Komputer</td></tr>
<tr><td>Kritik</td><td><textarea wrap="OFF" rows="5" cols="20" name="kritik"></textarea></td></tr>
<tr><td><input type="submit" name="save" id="save" value="Save"/></td></tr>
</table>
</form>
<?php
$conn= mysqli_connect("localhost","root","","pelatihan");
$result=mysqli_query($conn,"select * from peserta");
echo "<table border=1>";
echo "<tr><td align='center'>NOMOR</td><td align='center'>NPM</td>";
echo "<td align='center'>NAMA</td><td align='center'>PASSWORD</td>";
echo "<td align='center'>JENIS KELAMIN</td><td align='center'>AGAMA</td>";

echo "<td align='center'>HOBBY</td><td align='center'>KRITIK</td>";


echo "<td align='center'>DELETE</td>";
echo "<td align='center'>EDIT</td>";
echo "</tr>";
$nomor=$nomor+1;
while($row=mysqli_fetch_array($result))
{ echo "<tr><td>".$nomor."</td>";
echo "<td>".$row['NPM']."</td>";
echo "<td>".$row['NAMA']."</td>";
echo "<td>".$row['PASSWD']."</td>";
echo "<td>".$row['JENIS_KELAMIN']."</td>";
echo "<td>".$row['AGAMA']."</td>";
echo "<td>".$row['HOBBY']."</td>";
echo "<td>".$row['KRITIK']."</td>";
echo "<td><a href='delete.php?npm=$row[0]'>Delete</a></td>";
echo
"<td><a
href='cruds.php?
npm=$row[0]&nama=$row[1]&password=$row[2]&JK=$row[3]&agama=$row[4]&hobby=$row[5]&kri
tik=$row[6]'>Edit</a></td>";
echo "</tr>";
$nomor++;
}
echo "</TABLE>";
?>
Hasilnya sbb:

Kemudian menampilkan data yang diklik ke form dengan mengubah file cruds.php
<form method="post" action="crud_simpan.php" >
<table>
<tr><td>NPM</td><td><input type="text" name="NPM" id="NPM"
value="<?php if(isset($_GET['npm'])){echo $_GET['npm']; } ?>"
/></td></tr>
<tr><td>Nama</td><td><input type="text" name="nama" id="nama"
value="<?php if(isset($_GET['nama'])){echo $_GET['nama']; } ?>"
/></td></tr>
<tr><td>Password</td><td><input type="password" name="Password" id="Password"

value="<?php if(isset($_GET['password'])){echo $_GET['password']; } ?>"


/></td></tr>
<tr><td>Jenis Kelamin</td><td>
<?php
if(isset($_GET['JK'])=='Laki-laki')
{
echo "<input type='radio' name='JK' value='Laki-laki' checked/>Laki-laki";
echo "<input type='radio' name='JK' value='Perempuan'/>Perempuan";
}
elseif (isset($_GET['JK'])=='Perempuan')
{
echo "<input type='radio' name='JK' value='Laki-laki' />Laki-laki";
echo "<input type='radio' name='JK' value='Perempuan' checked/>Perempuan";
}
else
{
echo "<input type='radio' name='JK' value='Laki-laki' />Laki-laki";
echo "<input type='radio' name='JK' value='Perempuan' />Perempuan";
}
?>
</td></tr>
<tr><td>Agama</td><td>
<select name="agama" id="agama">
<option value="">[P i l i h]</option>
<option value="Islam" <?php if ($_GET['agama']=='Islam'){ echo "selected";}
>>Islam</option>
<option value="Katholik" <?php if ($_GET['agama']=='Katholik'){ echo "selected";}
>>Katholik</option>
<option value="Protestan" <?php if ($_GET['agama']=='Protestan'){ echo "selected";}
>>Protestan</option>
<option value="Hindu" <?php if ($_GET['agama']=='Hindu'){ echo "selected";}
>>Hindu</option>
<option value="Budha" <?php if ($_GET['agama']=='Budha'){ echo "selected";}
>>Budha</option>
<option value="Lainnya" <?php if ($_GET['agama']=='Lainnya'){ echo "selected";}
>>Lainnya</option>
</select></td></tr>
<tr><td>Hobby</td><td><input type="checkbox" name="musik" value="Musik"
<?if(strpos($_GET['hobby'],'Musik')!==false){echo 'checked';}?>
/>Musik
<input type="checkbox" name="membaca" value="Membaca"
<?if(strpos($_GET['hobby'],'Membaca')!==false){echo 'checked';}?>
/>Membaca
<input type="checkbox" name="komputer" value="Komputer"
<?if(strpos($_GET['hobby'],'Komputer')!==false){echo 'checked';}?>
/>Komputer</td></tr>
<tr><td>Kritik</td><td><textarea wrap="OFF" rows="5" cols="20" name="kritik">
<?php if(isset($_GET['kritik'])){echo $_GET['kritik']; } ?>
</textarea></td></tr>
<tr><td><input type="submit" name="save" id="save" value="Save"/></td></tr>
</table>

?
?
?
?
?
?

</form>
<?php
$conn= mysqli_connect("localhost","root","","pelatihan");
$result=mysqli_query($conn,"select * from peserta");
echo "<table border=1>";
echo "<tr><td align='center'>NOMOR</td><td align='center'>NPM</td>";
echo "<td align='center'>NAMA</td><td align='center'>PASSWORD</td>";
echo "<td align='center'>JENIS KELAMIN</td><td align='center'>AGAMA</td>";
echo "<td align='center'>HOBBY</td><td align='center'>KRITIK</td>";
echo "<td align='center'>DELETE</td>";
echo "<td align='center'>EDIT</td>";
echo "</tr>";
$nomor=$nomor+1;
while($row=mysqli_fetch_array($result))
{ echo "<tr><td>".$nomor."</td>";
echo "<td>".$row['NPM']."</td>";
echo "<td>".$row['NAMA']."</td>";
echo "<td>".$row['PASSWD']."</td>";
echo "<td>".$row['JENIS_KELAMIN']."</td>";
echo "<td>".$row['AGAMA']."</td>";
echo "<td>".$row['HOBBY']."</td>";
echo "<td>".$row['KRITIK']."</td>";
echo "<td><a href='delete.php?npm=$row[0]'>Delete</a></td>";
echo
"<td><a
href='cruds.php?
npm=$row[0]&nama=$row[1]&password=$row[2]&JK=$row[3]&agama=$row[4]&hobby=$row[5]&kritik=$r
ow[6]'>Edit</a></td>";
echo "</tr>";
$nomor++;
}
echo "</TABLE>";
?>
Ketika diclick rudi maka akan tampil sbb

Selanjutnya proses simpannya akan berubah karena ada yang insert data dan update data
<?php
$npm
$nama
$passwd
$JK
$agama
$musik
$membaca
$komputer
$kritik

= $_POST['NPM'];
= $_POST['nama'];
= $_POST['Password'];
= $_POST['JK'];
= $_POST['agama'];
= $_POST['musik'];
= $_POST['membaca'];
= $_POST['komputer'];
= $_POST['kritik'];

if (isset($musik)){$hobby = $musik; };
if (isset($membaca)){$hobby = $hobby.','.$membaca; };
if (isset($komputer)){$hobby = $hobby.','.$komputer; };
$conn = mysqli_connect("localhost","root","","pelatihan");
$cari= "select * from peserta where npm =$npm ";
$result= mysqli_query($conn,$cari);
$hasil=mysqli_num_rows($result) ;
if ($hasil>0)
{ $query = "UPDATE peserta set nama='".$nama."',passwd='".$passwd."',jenis_kelamin='".$JK
."',agama='".$agama."',hobby='".$hobby."',kritik='".$kritik."' where npm='".$npm."'";
}
else
{ $query = "INSERT INTO peserta (nama,npm,passwd,jenis_kelamin,agama,hobby,kritik)
VALUES('".$nama."','".$npm."','".$passwd."','".$JK."','"
.$agama."','".$hobby."','".$kritik."')";
}
mysqli_query($conn,$query) or die("err".mysqli_error());
header('location: cruds.php');
?>
Contoh jika rudi akan diubah nama dan hobby, click edit di baris rudi dan update anto serta hobbynya dan
save

6. selanjutnya untuk proses searching dengan menambahkan textbox diatas header nomor , npm, sampai
kritik seperti berikut: ubah file cruds.php
<form method="post" action="crud_simpan1.php" >
<table>
<tr><td>NPM</td><td><input type="text" name="NPM" id="NPM"
value="<?php if(isset($_GET['npm'])){echo $_GET['npm']; } ?>"
/></td></tr>
<tr><td>Nama</td><td><input type="text" name="nama" id="nama"
value="<?php if(isset($_GET['nama'])){echo $_GET['nama']; } ?>"
/></td></tr>
<tr><td>Password</td><td><input type="password" name="Password" id="Password"
value="<?php if(isset($_GET['password'])){echo $_GET['password']; } ?>"
/></td></tr>
<tr><td>Jenis Kelamin</td><td><?php
if(isset($_GET['JK'])=='Laki-laki')
{
echo "<input type='radio' name='JK' value='Laki-laki' checked/>Laki-laki";
echo "<input type='radio' name='JK' value='Perempuan'/>Perempuan";
}

elseif (isset($_GET['JK'])=='Perempuan')
{
echo "<input type='radio' name='JK' value='Laki-laki' />Laki-laki";
echo "<input type='radio' name='JK' value='Perempuan' checked/>Perempuan";
}
else
{
echo "<input type='radio' name='JK' value='Laki-laki' />Laki-laki";
echo "<input type='radio' name='JK' value='Perempuan' />Perempuan";
}
?></td></tr>
<tr><td>Agama</td><td>
<select name="agama" id="agama">
<option value="">[P i l i h]</option>
<option value="Islam" <?php if ($_GET['agama']=='Islam'){ echo "selected";} ?>>Islam</option>
<option value="Katholik" <?php if ($_GET['agama']=='Katholik'){ echo "selected";} ?
>>Katholik</option>
<option value="Protestan" <?php if ($_GET['agama']=='Protestan'){ echo "selected";} ?
>>Protestan</option>
<option value="Hindu" <?php if ($_GET['agama']=='Hindu'){ echo "selected";} ?>>Hindu</option>
<option value="Budha" <?php if ($_GET['agama']=='Budha'){ echo "selected";} ?
>>Budha</option>
<option value="Lainnya" <?php if ($_GET['agama']=='Lainnya'){ echo "selected";} ?
>>Lainnya</option>
</select></td></tr>
<tr><td>Hobby</td><td><input type="checkbox" name="musik" value="Musik"
<?if(strpos($_GET['hobby'],'Musik')!==false){echo 'checked';}?>
/>Musik
<input type="checkbox" name="membaca" value="Membaca"
<?if(strpos($_GET['hobby'],'Membaca')!==false){echo 'checked';}?>
/>Membaca
<input type="checkbox" name="komputer" value="Komputer"
<?if(strpos($_GET['hobby'],'Komputer')!==false){echo 'checked';}?>
/>Komputer</td></tr>
<tr><td>Kritik</td><td><textarea wrap="OFF" rows="5" cols="20" name="kritik">
<?php if(isset($_GET['kritik'])){echo trim($_GET['kritik']); } ?>
</textarea></td></tr>
<tr><td><input type="submit" name="save" id="save" value="Save"/></td></tr>
</table>
</form>
<?php
$conn= mysqli_connect("localhost","root","","pelatihan");
$npm = $_POST['src_npm'];
$nama = $_POST['src_nama'];
$pass = $_POST['src_pass'];
$jk = $_POST['src_jk'];
$agama = $_POST['src_agama'];
$hobby = $_POST['src_hobby'];
$kritik = $_POST['src_kritik'];
$where ='';

if(isset($npm))
{ $where = 'NPM like "%'.$npm.'%"';}
if(isset($nama))
{ if ($where=='')
{ $where = ' NAMA like "%'.$nama.'%"';}
else
{ $where = $where.' AND NAMA like "%'.$nama.'%"';}
}
if(isset($pass))
{ if ($where=='')
{ $where = ' PASSWD like "%'.$pass.'%"';}
else
{ $where = $where.' AND PASSWD like "%'.$pass.'%"';}
}
if(isset($jk))
{ if ($where=='')
{ $where = ' JENIS_KELAMIN like "%'.$jk.'%"';}
else
{ $where = $where.' AND JENIS_KELAMIN like "%'.$jk.'%"';}
}
if(isset($agama))
{ if ($where=='')
{ $where = ' AGAMA like "%'.$agama.'%"';}
else
{ $where = $where.' AND AGAMA like "%'.$agama.'%"';}
}
if(isset($hobby))
{ if ($where=='')
{ $where = ' HOBBY like "%'.$hobby.'%"';}
else
{ $where = $where.' AND HOBBY like "%'.$hobby.'%"';}
}
if(isset($kritik))
{ if ($where=='')
{ $where = ' KRITIK like "%'.$kritik.'%"';}
else
{ $where = $where.' AND KRITIK like "%'.$kritik.'%"';}
}
if ($where !='')
{$where = ' Where '.$where;};
$query ="select * from peserta ".$where;
$result=mysqli_query($conn,$query);
$nomor = 1;
echo "<table border=1>";
echo "<form action='crud.php' method='POST'>";
echo "<tr><td></td>";
echo "<td><input type='textbox' id='src_npm' name='src_npm'/></td>";

echo "<td><input type='textbox' id='src_nama' name='src_nama'/></td>";


echo "<td><input type='textbox' id='src_pass' name='src_pass'/></td>";
echo "<td><input type='textbox' id='src_jk' name='src_jk'/></td>";
echo "<td><input type='textbox' id='src_agama' name='src_agama'/></td>";
echo "<td><input type='textbox' id='src_hobby' name='src_hobby'/></td>";
echo "<td><input type='textbox' id='src_kritik' name='src_kritik'/></td>";
echo "<td><input type='submit' value='Search'/></td>";
echo "<td><input type='submit' value='Refresh'/></td></tr></form>";
echo "<tr><td align='center'>NOMOR</td><td align='center'>NPM</td>";
echo "<td align='center'>NAMA</td><td align='center'>PASSWORD</td>";
echo "<td align='center'>JENIS KELAMIN</td><td align='center'>AGAMA</td>";
echo "<td align='center'>HOBBY</td><td align='center'>KRITIK</td>";
echo "<td align='center'>DELETE</td>";
echo "<td align='center'>EDIT</td>";
echo "</tr>";
$nomor=$nomor+1;
while($row=mysqli_fetch_array($result))
{ echo "<tr><td>".$nomor."</td>";
echo "<td>".$row['NPM']."</td>";
echo "<td>".$row['NAMA']."</td>";
echo "<td>".$row['PASSWD']."</td>";
echo "<td>".$row['JENIS_KELAMIN']."</td>";
echo "<td>".$row['AGAMA']."</td>";
echo "<td>".$row['HOBBY']."</td>";
echo "<td>".$row['KRITIK']."</td>";
echo "<td><a href='delete.php?npm=$row[0]'>Delete</a></td>";
echo
"<td><a
href='crud.php?
npm=$row[0]&nama=$row[1]&password=$row[2]&JK=$row[3]&agama=$row[4]&hobby=$row[5]&kritik
=".trim($row[6])."'>Edit</a></td>";
echo "</tr>";
$nomor++;
}
echo "</TABLE>";
?>
Hasilnya setalah running

Kemudian pada password kita isi 4 dan tekan search maka akan tampil sbb

Jadi yang memliki password angka 4 yang dikeluarkan


Refresh untuk mengembalikan ke semula
Jika kita isi npm 2 dan agama ISLAM maka tampil sbb

Dan kita tekan search maka akan tampil sbb:


Artinya yang memiliki npm ada angka 2 dan agama islam

7.