Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Materi Pembelajaran Mulok (Web Desain) - Walid Umar, S.SI

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

Materi Pembelajaran Mulok (Web Desain) – Walid Umar, S.

SI

SQL (Structure Query Language)


C:\Users\walidumar>cd /

C:\>cd xampp/mysql/bin

C:\xampp\mysql\bin>mysql -u root -p
Enter password:

mysql> show databases;


+--------------------+
| Database |
+--------------------+
| information_schema |
| cdcol |
| mysql |
| performance_schema |
| phpmyadmin |
| test |
| webauth |
+--------------------+
7 rows in set (3.26 sec)

mysql> create database sekolah;


Query OK, 1 row affected (0.36 sec)

mysql> show databases;


+--------------------+
| Database |
+--------------------+
| information_schema |
| cdcol |
| mysql |
| performance_schema |
| phpmyadmin |
| sekolah |
| test |
| webauth |
+--------------------+
8 rows in set (0.00 sec)

mysql> drop database sekolah;


Query OK, 0 rows affected (0.64 sec)

mysql> show databases;


+--------------------+
| Database |
+--------------------+
| information_schema |
| cdcol |
| mysql |
| performance_schema |
| phpmyadmin |
| test |
| webauth |
+--------------------+
7 rows in set (0.00 sec)

mysql> create database sekolah;


Query OK, 1 row affected (0.02 sec)

mysql> use sekolah;


Database changed

mysql> create table siswa(nisn int(10), nama varchar(30), umur int(3));


Query OK, 0 rows affected (0.42 sec)

mysql> desc siswa;


+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| nisn | int(10) | YES | | NULL | |
| nama | varchar(30) | YES | | NULL | |
| umur | int(3) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.14 sec)

mysql> show tables;


+-------------------+
| Tables_in_sekolah |
+-------------------+
| siswa |
+-------------------+
1 row in set (0.00 sec)

mysql> alter table siswa


-> add alamat varchar(30);
Query OK, 0 rows affected (0.42 sec)
Records: 0 Duplicates: 0 Warnings: 0
Materi Pembelajaran Mulok (Web Desain) – Walid Umar, S.SI

mysql> desc siswa;


+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| nisn | int(10) | YES | | NULL | |
| nama | varchar(30) | YES | | NULL | |
| umur | int(3) | YES | | NULL | |
| alamat | varchar(30) | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
4 rows in set (0.02 sec)

mysql> alter table siswa


-> add tempatlahir varchar(20)
-> after umur;
Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> desc siswa;


+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| nisn | int(10) | YES | | NULL | |
| nama | varchar(30) | YES | | NULL | |
| umur | int(3) | YES | | NULL | |
| tempatlahir | varchar(20) | YES | | NULL | |
| alamat | varchar(30) | YES | | NULL | |
+-------------+-------------+------+-----+---------+-------+
5 rows in set (0.03 sec)

mysql> alter table siswa add tgllahir DATE;


Query OK, 0 rows affected (0.21 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> desc siswa;


+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| nisn | int(10) | YES | | NULL | |
| nama | varchar(30) | YES | | NULL | |
| umur | int(3) | YES | | NULL | |
| tempatlahir | varchar(20) | YES | | NULL | |
| alamat | varchar(30) | YES | | NULL | |
| tgllahir | date | YES | | NULL | |
+-------------+-------------+------+-----+---------+-------+
6 rows in set (0.02 sec)

mysql> alter table siswa


-> drop tgllahir;
Query OK, 0 rows affected (0.10 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> desc siswa;


+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| nisn | int(10) | YES | | NULL | |
| nama | varchar(30) | YES | | NULL | |
| umur | int(3) | YES | | NULL | |
| tempatlahir | varchar(20) | YES | | NULL | |
| alamat | varchar(30) | YES | | NULL | |
+-------------+-------------+------+-----+---------+-------+
6 rows in set (0.05 sec)

mysql> alter table siswa


-> add tgllahir date
-> after tempatlahir;
Query OK, 0 rows affected (0.20 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> desc siswa;


+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| nisn | int(10) | YES | | NULL | |
| nama | varchar(30) | YES | | NULL | |
| umur | int(3) | YES | | NULL | |
| tempatlahir | varchar(20) | YES | | NULL | |
| tgllahir | date | YES | | NULL | |
| alamat | varchar(30) | YES | | NULL | |
+-------------+-------------+------+-----+---------+-------+
6 rows in set (0.05 sec)
Materi Pembelajaran Mulok (Web Desain) – Walid Umar, S.SI

mysql> alter table siswa


-> change nama
-> nama_lengkap varchar(40);
Query OK, 0 rows affected (0.45 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> desc siswa;


+--------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+-------+
| nisn | int(10) | YES | | NULL | |
| nama_lengkap | varchar(40) | YES | | NULL | |
| umur | int(3) | YES | | NULL | |
| tempatlahir | varchar(20) | YES | | NULL | |
| tgllahir | date | YES | | NULL | |
| alamat | varchar(30) | YES | | NULL | |
+--------------+-------------+------+-----+---------+-------+
6 rows in set (0.13 sec)

mysql> insert into siswa


-> values
-> ('1001','Krisdewanto','19','Gorontalo','19960816','Liluwo');
Query OK, 1 row affected (0.03 sec)

mysql> insert into siswa


-> values
-> ('1002','Ferlianto Saramadi','20','Gorontalo','19960716','Pulubala'),
-> ('1003','Priya Anggraini','17','Gorontalo','19981206','Awara');
Query OK, 2 rows affected (0.05 sec)
Records: 2 Duplicates: 0 Warnings: 0

mysql> select * from siswa;


+------+--------------------+------+-------------+------------+----------+
| nisn | nama_lengkap | umur | tempatlahir | tgllahir | alamat |
+------+--------------------+------+-------------+------------+----------+
| 1001 | Krisdewanto | 19 | Gorontalo | 1996-08-16 | Liluwo |
| 1002 | Ferlianto Saramadi | 20 | Gorontalo | 1996-07-16 | Pulubala |
| 1003 | Priya Anggraini | 17 | Gorontalo | 1998-12-06 | Awara |
+------+--------------------+------+-------------+------------+----------+
3 rows in set (0.00 sec)

mysql> update siswa


-> set tempatlahir = 'Mongondow'
-> where nisn ='1002';
Query OK, 1 row affected (0.05 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> select * from siswa;


+------+--------------------+------+-------------+------------+----------+
| nisn | nama_lengkap | umur | tempatlahir | tgllahir | alamat |
+------+--------------------+------+-------------+------------+----------+
| 1001 | Krisdewanto | 19 | Gorontalo | 1996-08-16 | Liluwo |
| 1002 | Ferlianto Saramadi | 20 | Mongondow | 1996-07-16 | Pulubala |
| 1003 | Priya Anggraini | 17 | Gorontalo | 1998-12-06 | Awara |
+------+--------------------+------+-------------+------------+----------+
3 rows in set (0.02 sec)

mysql> update siswa


-> set umur='18', nama_lengkap='Priya Anggrayni'
-> where nisn='1003';
Query OK, 1 row affected (0.03 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> select * from siswa;


+------+--------------------+------+-------------+------------+----------+
| nisn | nama_lengkap | umur | tempatlahir | tgllahir | alamat |
+------+--------------------+------+-------------+------------+----------+
| 1001 | Krisdewanto | 19 | Gorontalo | 1996-08-16 | Liluwo |
| 1002 | Ferlianto Saramadi | 20 | Mongondow | 1996-07-16 | Pulubala |
| 1003 | Priya Anggrayni | 18 | Gorontalo | 1998-12-06 | Awara |
+------+--------------------+------+-------------+------------+----------+
3 rows in set (0.00 sec)

mysql> delete from siswa


-> where nisn='1001';
Query OK, 1 row affected (0.05 sec)

mysql> select * from siswa;


+------+--------------------+------+-------------+------------+----------+
| nisn | nama_lengkap | umur | tempatlahir | tgllahir | alamat |
+------+--------------------+------+-------------+------------+----------+
| 1002 | Ferlianto Saramadi | 20 | Mongondow | 1996-07-16 | Pulubala |
| 1003 | Priya Anggrayni | 18 | Gorontalo | 1998-12-06 | Awara |
+------+--------------------+------+-------------+------------+----------+
2 rows in set (0.00 sec)

mysql> delete from siswa


-> where umur >=17;
Query OK, 2 rows affected (0.06 sec)

mysql> select * from siswa;


Empty set (0.00 sec)
Materi Pembelajaran Mulok (Web Desain) – Walid Umar, S.SI

mysql> select * from siswa;


+------+-------------------+--------+------+-------------+------------+-----------+
| nisn | nama_lengkap | gender | umur | tempatlahir | tgllahir | alamat |
+------+-------------------+--------+------+-------------+------------+-----------+
| 1001 | Nadiyanti Paris | Wanita | 20 | Gorontalo | 1997-07-08 | Gorontalo |
| 1002 | Merlin Daud | Wanita | 20 | Gorontalo | 1998-07-06 | Gorontalo |
| 1003 | Rifka R. Pina | Wanita | 20 | Jawa Barat | 1997-06-08 | Gorontalo |
| 1004 | Rin Yusria Amrain | Wanita | 20 | Gorontalo | 1998-12-12 | Kabila |
| 1005 | Nur Afni Teu | Wanita | 20 | Gorontalo | 1998-10-10 | Awara |
| 1006 | Simon Olii | Pria | 22 | Gorontalo | 1995-04-06 | Pulubala |
| 1007 | Jerianto Launuha | Pria | 22 | Gorontalo | 1995-06-06 | Gorontalo |
+------+-------------------+--------+------+-------------+------------+-----------+
7 rows in set (0.00 sec)

mysql> select * from siswa


-> where gender='Pria';
+------+------------------+--------+------+-------------+------------+-----------+
| nisn | nama_lengkap | gender | umur | tempatlahir | tgllahir | alamat |
+------+------------------+--------+------+-------------+------------+-----------+
| 1006 | Simon Olii | Pria | 22 | Gorontalo | 1995-04-06 | Pulubala |
| 1007 | Jerianto Launuha | Pria | 22 | Gorontalo | 1995-06-06 | Gorontalo |
+------+------------------+--------+------+-------------+------------+-----------+
2 rows in set (0.05 sec)

mysql> select * from siswa where umur <=20;


+------+-------------------+--------+------+-------------+------------+-----------+
| nisn | nama_lengkap | gender | umur | tempatlahir | tgllahir | alamat |
+------+-------------------+--------+------+-------------+------------+-----------+
| 1001 | Nadiyanti Paris | Wanita | 20 | Gorontalo | 1997-07-08 | Gorontalo |
| 1002 | Merlin Daud | Wanita | 20 | Gorontalo | 1998-07-06 | Gorontalo |
| 1003 | Rifka R. Pina | Wanita | 20 | Jawa Barat | 1997-06-08 | Gorontalo |
| 1004 | Rin Yusria Amrain | Wanita | 20 | Gorontalo | 1998-12-12 | Kabila |
| 1005 | Nur Afni Teu | Wanita | 20 | Gorontalo | 1998-10-10 | Awara |
+------+-------------------+--------+------+-------------+------------+-----------+
5 rows in set (0.00 sec)

mysql> select * from siswa


-> where nama_lengkap like 'Ri%';
+------+-------------------+--------+------+-------------+------------+-----------+
| nisn | nama_lengkap | gender | umur | tempatlahir | tgllahir | alamat |
+------+-------------------+--------+------+-------------+------------+-----------+
| 1003 | Rifka R. Pina | Wanita | 20 | Jawa Barat | 1997-06-08 | Gorontalo |
| 1004 | Rin Yusria Amrain | Wanita | 20 | Gorontalo | 1998-12-12 | Kabila |
+------+-------------------+--------+------+-------------+------------+-----------+
2 rows in set (0.06 sec)

mysql> select * from siswa


-> where umur !=20;
+------+------------------+--------+------+-------------+------------+-----------+
| nisn | nama_lengkap | gender | umur | tempatlahir | tgllahir | alamat |
+------+------------------+--------+------+-------------+------------+-----------+
| 1006 | Simon Olii | Pria | 22 | Gorontalo | 1995-04-06 | Pulubala |
| 1007 | Jerianto Launuha | Pria | 22 | Gorontalo | 1995-06-06 | Gorontalo |
+------+------------------+--------+------+-------------+------------+-----------+
2 rows in set (0.02 sec)

mysql> select * from siswa


-> where nama_lengkap like '%a';
+------+------------------+--------+------+-------------+------------+-----------+
| nisn | nama_lengkap | gender | umur | tempatlahir | tgllahir | alamat |
+------+------------------+--------+------+-------------+------------+-----------+
| 1003 | Rifka R. Pina | Wanita | 20 | Jawa Barat | 1997-06-08 | Gorontalo |
| 1007 | Jerianto Launuha | Pria | 22 | Gorontalo | 1995-06-06 | Gorontalo |
+------+------------------+--------+------+-------------+------------+-----------+
2 rows in set (0.00 sec)

mysql> select * from siswa


-> where nama_lengkap like '%ii%';
+------+--------------+--------+------+-------------+------------+----------+
| nisn | nama_lengkap | gender | umur | tempatlahir | tgllahir | alamat |
+------+--------------+--------+------+-------------+------------+----------+
| 1006 | Simon Olii | Pria | 22 | Gorontalo | 1995-04-06 | Pulubala |
+------+--------------+--------+------+-------------+------------+----------+
1 row in set (0.00 sec)

mysql> select nama_lengkap from siswa


-> where umur >=20 AND tempatlahir !='Gorontalo';
+---------------+
| nama_lengkap |
+---------------+
| Rifka R. Pina |
+---------------+
1 row in set (0.02 sec)
Materi Pembelajaran Mulok (Web Desain) – Walid Umar, S.SI

mysql> select nama_lengkap, tempatlahir, tgllahir from siswa;


+-------------------+-------------+------------+
| nama_lengkap | tempatlahir | tgllahir |
+-------------------+-------------+------------+
| Nadiyanti Paris | Gorontalo | 1997-07-08 |
| Merlin Daud | Gorontalo | 1998-07-06 |
| Rifka R. Pina | Jawa Barat | 1997-06-08 |
| Rin Yusria Amrain | Gorontalo | 1998-12-12 |
| Nur Afni Teu | Gorontalo | 1998-10-10 |
| Simon Olii | Gorontalo | 1995-04-06 |
| Jerianto Launuha | Gorontalo | 1995-06-06 |
+-------------------+-------------+------------+
7 rows in set (0.00 sec)

mysql> select nama_lengkap from siswa


-> where tempatlahir like '%lo' OR gender='Pria';
+-------------------+
| nama_lengkap |
+-------------------+
| Nadiyanti Paris |
| Merlin Daud |
| Rin Yusria Amrain |
| Nur Afni Teu |
| Simon Olii |
| Jerianto Launuha |
+-------------------+
6 rows in set (0.00 sec)
Materi Pembelajaran Mulok (Web Desain) – Walid Umar, S.SI

PHP
Struktur Dasar PHP - Komentar

<?php
//Struktur Dasar PHP
?>

Struktur Dasar PHP – Komentar multibaris

<?php
/*
Ini struktur komentar
lebih
dari
satu
baris
*/
?>

Menampilkan Teks String

<?php
echo ‘Yo Man’;
?>

HTML Inner PHP

<?php
echo '<b>ini kolaborasi </b><font color="red">HTML</font>
<font color=blue>& PHP</font>';
?>

Variabel 1

<?php
$jurusan='Teknik Komputer & Jaringan';
echo 'Saya Jurusan '.$jurusan;
?>

Variabel 2

<?php
$nama='Walid umar';
echo 'Halo, '.$nama.'<br>Salam Kenal!';
?>

PHP Operator

Operator Aritmatika

Operator Deskripsi Contoh Hasil

+ Penjumlahan x=2 4
x+2
- Pengurangan x=2 3
5-x
* Perkalian x=4 20
x*5
/ Pembagian 15/5 3
5/2 2.5
% Modulus (Sisa Bagi) 5%2 1
10%8 2
10%2 0
++ Peningkatan x=5 x=6
x++
-- Penurunan x=5 x=4
x--
Materi Pembelajaran Mulok (Web Desain) – Walid Umar, S.SI

Operator Perbandingan

Operator Deskripsi Contoh

== Sama dengan 5==8 false


!= Tidak sama dengan 5!=8 true
<> Tidak sama dengan 5<>8 true
> Lebih dari 5>8 false
< Kurang dari 5<8 true
>= Lebih dari sama dengan 5>=8 false
<= Kurang dari sama dengan 5<=8 true

Operator Logika

Operator Deskripsi Contoh


&& DAN x=6
y=3
(x < 10 && y > 1) adalah true
|| ATAU x=6
y=3
(x==5 || y==5) adalah false
! TIDAK x=6
y=3!
(x==y) adalah true

Operasi Aritmatika Variabel

<?php
$a=5;
$b=3;
$kali=$a*$b;
echo 'hasil kali = '.$kali;
echo '<br>';
$kurang=$a-$b;
echo 'hasil kurang = '.$kurang;
echo '<br>';
$jumlah=$a+$b;
echo 'hasil jumlah = '.$jumlah;
echo '<br>';
$bagi=$a/$b;
echo 'hasil jumlah = '.$bagi;
echo '<br>';

?>

Operasi Aritmatika Variabel

<?php
echo '<h3>Rumus Menghitung Luas Segitiga</h3><br>';
$panjang=10;
$lebar=30;
$luas=$panjang*$lebar;
echo 'Luas Segitiga :: '.$panjang.' x '.$lebar.'='.$luas;
?>

Operator Logika & Perbandingan IF-ELSE

<?php
$nilai = 75;

if ($nilai >= 75){


echo "Selamat! anda Lulus Ujian";
}else{
echo "Maaf ! anda harus remedial";
}

?>
Materi Pembelajaran Mulok (Web Desain) – Walid Umar, S.SI

Operator Logika & Perbandingan IF-ELSE

<?php
$nilai = 75;

if ($nilai > 75){


echo "Selamat! anda Lulus Ujian";
}else{
echo "Maaf ! anda harus remedial";
}

?>

Operator Logika & Perbandingan if,elseif-else

<?php
$nilai = 85;
if ($nilai >= 98){
echo "Nilai anda : A";
}else if ($nilai >=85){
echo "Nilai anda : B";
}else if ($nilai >=75){
echo "Nilai anda : C";
}else if ($nilai >=60){
echo "Nilai anda : D";
}else{
echo "Anda Harus Mengulang '_' ";
}

?>

Join String

<?php
$text1="Halo Kawan,...! ";
$text2="Salam kenal...";
echo $text1 . " " . $text2;
?>

Menghitung panjang String

<?php
//mengukur panjang string
echo "Hitung panjang String > SMK Negeri 4 Gorontalo<br> Adalah : ";
echo strlen("SMK Negeri 4 Gorontalo");
?>

Menghitung panjang String (variabel)

<?php
//mengukur panjang string dalam variabel
$sekolah = "SMK Negeri 4 Gorontalo";
echo strlen($sekolah);
?>

Memposisikan letak value dalam string

<?php
//menentukkan lokasi / posisi karakter dari sebuah string
$data = 'Sekolah Cyber di Gorontalo adalah SMK 4 Gorontalo';
echo strpos($data,'Gorontalo');
?>

Perulangan (looping) - while

<?php
$i=1;
echo "belajar berhitung dari 1 - 10 : <br>";
while($i<=10)
{
echo "Angka ". $i . " adalah ". $i . "<br>";
$i++;
}
?>

Perulangan (looping) - for

<?php
echo "belajar berhitung -> looping-for";

for ($i=0; $i<20; $i++){


echo "Angka : ". $i;
echo "<br>";
}

?>
Materi Pembelajaran Mulok (Web Desain) – Walid Umar, S.SI

Perulangan (looping) – for

<?php
echo "belajar berhitung -> looping-for <br>";
for ($i=1; $i<=20; $i++){
echo $i."...";
}
?>

Pengenalan Fungsi PHP - function

<?php
function kali ($a, $b){
echo 'Perkalian Bilangan<br>';
echo 'bilangan A : '.$a.'<br>';
echo 'bilangan B : '.$b.'<br>';
$kali = $a * $b;
echo 'adalah sama dengan > ';
return $kali;
}
echo kali(5,3);
?>

HTML Form dan method POST

<html><body>
<form action="latihan1.php" method="post">
<table border="0">
<tr><td>Nama</td><td>:</td><td><input type="text" name="nama"></td></tr>
<tr><td>Jurusan</td><td>:</td><td><input type="jurusan" name="jurusan"></td></tr>
<tr><td><input type="submit" value="Masuk"></td></tr>
</table></form>
<?php
$nama = $_POST['nama'];
$jurusan = $_POST['jurusan'];
echo "Selamat datang ".$nama."<br>";
echo $jurusan . " is the best !"
?>
</body>
</html>

Operasi Penjumlahan 2 bilangan dengan method POST

<html>
<body>
<form method="POST" action="latihan1.php">
A = <input type ="text" name="bil1"/>
<input type=”text” name=”operator” />
B = <input type ="text" name="bil2"/><br>
<input type="submit" value="Jumlahkan">
</form>
<?php
$bilanganA = $_POST['bil1'];
$bilanganB = $_POST['bil2'];
$hasil = $bilanganA + $bilanganB;
echo "Hasil Jumlah : ".$hasil;
?>
</body>
</html>

Operasi Penjumlahan 2 bilangan dengan method GET

<html>
<body>
<form method="GET" action="latihan1.php">
A = <input type ="text" name="bil1"/>
<input type=”text” name=”operator” />
B = <input type ="text" name="bil2"/><br>
<input type="submit" value="Jumlahkan">
</form>
<?php
$bilanganA = $_GET['bil1'];
$bilanganB = $_GET['bil2'];
$hasil = $bilanganA + $bilanganB;
echo "Hasil Jumlah : ".$hasil;
?>
</body>
</html>
Materi Pembelajaran Mulok (Web Desain) – Walid Umar, S.SI

Fungsi koneksi php ke mysql

<?php
//skrip koneksi ke database mysql
$koneksi = mysql_connect("localhost","root","");
if (!$koneksi){
die('Tidak bisa koneksi ke database ' . mysql_error());
}else{
echo "berhasil terkoneksi ke database!";
}
?>

Menampilkan data query sql dengan metode perulangan (while) ke halaman web

<?php
$koneksi = mysql_connect("localhost","root","");
if (!$koneksi){
die('Tidak bisa koneksi ke database ' . mysql_error());
}else{
// echo "berhasil terkoneksi ke database!";
}
mysql_select_db("sekolah", $koneksi);
$query = mysql_query("select * from siswa");
echo "<center><b>Data Siswa TKJ SMK Negeri 4 Gorontalo</b>
<table border=1 cellpadding=5>
<tr><td>NISN</td><td>Nama</td><td>Gender</td><td>Umur</td>
<td>Tempat Lahir</td><td>Tanggal Lahir</td><td>Tanggal Lahir</td>
</tr>";
while ($baris = mysql_fetch_array($query)){
echo "<tr>";
echo "<td>". $baris['nisn']."</br>";
echo "<td>". $baris['nama_lengkap']."</br>";
echo "<td>". $baris['gender']."</br>";
echo "<td>". $baris['umur']."</br>";
echo "<td>". $baris['tempatlahir']."</br>";
echo "<td>". $baris['tgllahir']."</br>";
echo "<td>". $baris['alamat']."</br>";
echo "</tr>";
}
echo "</table></center>";
?>
</body>
</html>

Menginput data query sql dengan tag form html

<html><body>
<form action="latihan2.php" method="post">
<table border=0><tr><td>NISN</td><td>:</td><td><input type="text" name="nisn"></td></tr>
<tr><td>Nama Siswa</td><td>:</td><td><input type="text" name="nama_lengkap"></td></tr>
<tr><td>Gender</td><td>:</td><td><input type="text" name="gender"></td></tr>
<tr><td>Umur</td><td>:</td><td><input type="text" name="umur"></td></tr>
<tr><td>Tempat Lahir</td><td>:</td><td><input type="text" name="tempatlahir"></td></tr>
<tr><td>Tanggal Lahir</td><td>:</td><td><input type="text" name="tgllahir"></td></tr>
<tr><td>Alamat</td><td>:</td><td><input type="text" name="alamat"></td></tr>
<tr><td><input type="submit" value="Save!"></td></tr>
</table></form>
<?php
$koneksi = mysql_connect("localhost","root","");
if (!$koneksi)
{
die('Gagal Terhubung : ' . mysql_error());
}
mysql_select_db("sekolah", $koneksi);
$sql="INSERT INTO siswa
VALUES('$_POST[nisn]','$_POST[nama_lengkap]','$_POST[gender]','$_POST[umur]','$_POST[tempatlahir]
','$_POST[tgllahir]','$_POST[alamat]')";
if (!mysql_query($sql,$koneksi))
{
die('Gagal dieksekusi: ' . mysql_error());
}
echo "Data tersimpan!";
?>
</body>
</html>
Materi Pembelajaran Mulok (Web Desain) – Walid Umar, S.SI

Latihan (create, read, update, delete)

Struktur Tabel
CREATE TABLE `siswa` (
`siswa_id` int(11) NOT NULL auto_increment,
`siswa_nis` int(20) NOT NULL,
`siswa_nama` varchar(50) NOT NULL,
`siswa_kelas` varchar(10) NOT NULL,
`siswa_jurusan` varchar(30) NOT NULL,
PRIMARY KEY (`siswa_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=7 ;

Koneksi ke mysql (koneksi.php)


<?php
$host = "localhost";
$user = "root";
$pass = "root";
$name = "tugas ";

$koneksi = mysql_connect($host, $user, $pass) or die("Koneksi ke database gagal!");


mysql_select_db($name, $koneksi) or die("Tidak ada database yang dipilih!");
?>

Tampilkan Data (tampil.html>


<!DOCTYPE html>
<html><head><title>Tampilkan Data </title></head><body>
<p><a href="index.php">Beranda</a> / <a href="tambah.php">Tambah Data</a></p>
<h3>Data Siswa</h3>
<table cellpadding="5" cellspacing="0" border="1">
<tr bgcolor="#CCCCCC"><th>No.</th><th>NIS</th><th>Nama Lengkap</th><th>Kelas</th>
<th>Jurusan</th><th>Opsi</th></tr>
<?php
include('koneksi.php');
$query = mysql_query("SELECT * FROM siswa ORDER BY siswa_nis DESC") or die(mysql_error());
if(mysql_num_rows($query) == 0){ //ini artinya jika data hasil query di atas kosong
echo '<tr><td colspan="6">Tidak ada data!</td></tr>';
}else{
$no = 1;
while($data = mysql_fetch_assoc($query)){ echo '<tr>';
echo '<td>'.$no.'</td>'; //menampilkan nomor urut
echo '<td>'.$data['siswa_nis'].'</td>'; echo '<td>'.$data['siswa_nama'].'</td>'; //menampilkan
data nama lengkap dari database
echo '<td>'.$data['siswa_kelas'].'</td>'; //menampilkan data kelas dari database
echo '<td>'.$data['siswa_jurusan'].'</td>'; //menampilkan data jurusan dari database
echo '<td><a href="edit.php?id='.$data['siswa_id'].'">Edit</a> / <a
href="hapus.php?id='.$data['siswa_id'].'" onclick="return confirm(\'Yakin?\')">Hapus</a></td>';
echo '</tr>';
$no++;
}}
?>
</table></body></html>

Form Tambah (tambah.php)


<!DOCTYPE html>
<html><head><title>Input Data</title></head>
<body><h2>Simple CRUD</h2>
<p><a href="index.php">Beranda</a> / <a href="tambah.php">Tambah Data</a></p>
<h3>Tambah Data Siswa</h3>
<form action="tambah-proses.php" method="post">
<table cellpadding="3" cellspacing="0">
<tr><td>NIS</td><td>:</td><td><input type="text" name="nis" required></td>
</tr><tr><td>Nama Lengkap</td><td>:</td>
<td><input type="text" name="nama" size="30" required></td></tr>
<tr><td>Kelas</td><td>:</td><td><select name="kelas" required>
<option value="">Pilih Kelas</option><option value="X">X</option>
<option value="XI">XI</option><option value="XII">XII</option></select>
</td></tr><tr><td>Jurusan</td><td>:</td><td>
<select name="jurusan" required><option value="">Pilih Jurusan</option>
<option value="Teknik Komputer dan Jaringan">Teknik Komputer dan Jaringan</option>
<option value="Akuntansi">Akuntansi</option>
<option value="Kria Tekstil">Kria Tekstil</option>
<option value="Kria Kayu">Kria Kayu</option></select>
</td></tr><tr><td>&nbsp;</td><td></td><td>
<input type="submit" name="tambah" value="Tambah"></td></tr>
</table></form></body></html>
Materi Pembelajaran Mulok (Web Desain) – Walid Umar, S.SI

Proses Tambah (tambah-proses.php)


<?php
if(isset($_POST['tambah'])){
include('koneksi.php');
$nis = $_POST['nis'];
$nama = $_POST['nama'];
$kelas = $_POST['kelas'];
$jurusan = $_POST['jurusan'];
$input = mysql_query
("INSERT INTO siswa VALUES(NULL, '$nis', '$nama', '$kelas', '$jurusan')") or die(mysql_error());
if($input){
echo 'Data berhasil di tambahkan! ';
echo '<a href="tambah.php">Kembali</a>';
}else{
echo 'Gagal menambahkan data! ';
echo '<a href="tambah.php">Kembali</a>';
}
}else{
echo '<script>window.history.back()</script>';
}
?>

Edit Data (edit-data.php)


<!DOCTYPE html><html><head><title>Edi Data </title></head>
<body>
<p><a href="index.php">Beranda</a> / <a href="tambah.php">Tambah Data</a></p>
<h3>Edit Data Siswa</h3>
<?php
include('koneksi.php');
//membuat variabel $id yg nilainya adalah dari URL GET id -> edit.php?id=siswa_id
$id = $_GET['id'];
$show = mysql_query("SELECT * FROM siswa WHERE siswa_id='$id'");
if(mysql_num_rows($show) == 0){
echo '<script>window.history.back()</script>';
}else{
$data = mysql_fetch_assoc($show);}?>
<form action="edit-proses.php" method="post">
<input type="hidden" name="id" value="<?php echo $id; ?>"><table cellpadding="3"
cellspacing="0"><tr><td>NIS</td><td>:</td>
<td><input type="text" name="nis" value="<?php echo $data['siswa_nis']; ?>" required></td></tr>
<tr><td>Nama Lengkap</td><td>:</td>
<td><input type="text" name="nama" size="30" value="<?php echo $data['siswa_nama']; ?>"
required></td></tr>
<tr><td>Kelas</td><td>:</td><td>
<select name="kelas" required>
<option value="">Pilih Kelas</option>
<option value="X" <?php if($data['siswa_kelas'] == 'X'){ echo 'selected'; } ?>>X</option> <option
value="XI" <?php if($data['siswa_kelas'] == 'XI'){ echo 'selected'; } ?>>XI</option> <option
value="XII" <?php if($data['siswa_kelas'] == 'XII'){ echo 'selected'; } ?>>XII</option></select>
</td>
</tr>
<tr>
<td>Jurusan</td>
<td>:</td>
<td>
<select name="jurusan" required>
<option value="">Pilih Jurusan</option>
<option value="Teknik Komputer dan Jaringan" <?php if($data['siswa_jurusan'] == 'Teknik Komputer
dan Jaringan'){ echo 'selected'; } ?>>Teknik Komputer dan Jaringan</option>
<option value="Akuntansi" <?php if($data['siswa_jurusan'] == 'Akuntansi'){ echo 'selected'; }
?>>Akuntansi</option>
<option value="Kria Tekstil" <?php if($data['siswa_jurusan'] == 'Kria Tekstil'){ echo 'selected';
} ?>>Kria Tekstil</option>
<option value="Kria Kayu" <?php if($data['siswa_jurusan'] == 'Kria Kayu'){ echo 'selected'; }
?>>Kria Kayu</option>
</select>
</td></tr><tr><td>&nbsp;</td><td></td><td><input type="submit" name="simpan"
value="Simpan"></td></tr></table></form></body>
</html>
Materi Pembelajaran Mulok (Web Desain) – Walid Umar, S.SI

Proses Edit (edit-proses.php)


<?php
if(isset($_POST['simpan'])){
include('koneksi.php');
$id = $_POST['id'];
$nis = $_POST['nis'];
$nama = $_POST['nama'];
$kelas = $_POST['kelas'];
$jurusan = $_POST['jurusan'];
$update = mysql_query("UPDATE siswa SET siswa_nis='$nis', siswa_nama='$nama',
siswa_kelas='$kelas', siswa_jurusan='$jurusan' WHERE siswa_id='$id'") or
die(mysql_error());
if($update){
echo 'Data berhasil di simpan! ';
echo '<a href="edit.php?id='.$id.'">Kembali</a>';
}else{
echo 'Gagal menyimpan data! ';
echo '<a href="edit.php?id='.$id.'">Kembali</a>';
}
}else{ //jika tidak terdeteksi tombol simpan di klik
echo '<script>window.history.back()</script>';
}
?>

Hapus Data (hapus.php)

<?php

if(isset($_GET['id'])){
include('koneksi.php');
$id = $_GET['id'];
$cek = mysql_query("SELECT siswa_id FROM siswa WHERE siswa_id='$id'") or die(mysql_error());
if(mysql_num_rows($cek) == 0){
echo '<script>window.history.back()</script>';
}else{
$del = mysql_query("DELETE FROM siswa WHERE siswa_id='$id'");
if($del){
echo 'Data siswa berhasil di hapus! '; //Pesan jika proses hapus berhasil
echo '<a href="index.php">Kembali</a>'; //membuat Link untuk kembali ke halaman beranda
}else{
echo 'Gagal menghapus data! '; //Pesan jika proses hapus gagal
echo '<a href="index.php">Kembali</a>'; //membuat Link untuk kembali ke halaman beranda
}}}else{
echo '<script>window.history.back()</script>';
}
?>

You might also like