Sie sind auf Seite 1von 14

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=$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="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++;
}
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="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 "<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="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 "<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=$r
ow[5]&kritik=$row[6]'>Edit</a></td>";
echo "</tr>";
$nomor++;
}
echo "</TABLE>";
?>
Kemudian menampilkan data yang diklik ke form :
<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 ($_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=$row[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
(nama,npm,passwd,jenis_kelamin,agama,hobby,kritik)
VALUES('".$nama."','".$npm."','".$passwd."','".$JK."','"
.$agama."','".$hobby."','".$kritik."')";
}

INTO

peserta

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

7.