In this case, we use if - else branching. The basic form of the statement is as follows:
if (kondisi) {
statement-jika-kondisi-true;
} else {
statement-jika-kondisi-false;
}
Condition is a parameter that can consist of variables, values or operators that will be checked for truth/condition (TRUE or FALSE). If the condition is TRUE, then the statement in the if block will be executed, conversely if the condition is FALSE then the statement in the else block will be executed.
Before trying the program code below, I first assume that:
- You have installed wampserver
- You have understood how the "Action", "GET Method" and "$_REQUEST" Attributes work
First, we create the form first, I will give an example of the code below, please save it with the file name "input_nilai.php"
<html>
<!--
*bundet.com
*Wawan Beneran
*PHP Form Hitung Nilai Mahasiswa
-->
<head>
<title>Belajar PHP </title>
</head>
<body>
<h2>PHP Hitung Nilai Mahasiswa</h2>
<style type="text/css">
#mhs{
//margin: 10px;
position: absolute;
left: 60px;
}
</style>
<form action ="output_nilai.php" method="POST">
Nama :
<input id="mhs" type="text" name="nama"><br>
Tugas:
<input id="mhs" type="text" name="tugas"><br>
UTS:
<input id="mhs" type="text" name="uts"><br>
UAS:
<input id="mhs" type="text" name="uas"><br><br>
<input id="mhs" type="submit" value="Hasil">
</form>
</body>
</html>
Meanwhile, use the code below to calculate and display the final value, save it with the file name "output_nilai.php"
<?php
/**
*gatewan.com
*Wawan Beneran
*PHP Kalkulasi Nilai Mahasiswa
*/
$nama=$_REQUEST["nama"];
$tugas=$_REQUEST["tugas"];
$uts=$_REQUEST["uts"];
$uas=$_REQUEST["uas"];
$hasil=($tugas*0.3)+($uas*0.4)+($uts*0.3);
echo("Nilai ");
echo($nama);
echo(" Adalah:");
echo("<br>");
if ($hasil>80)
echo("Hasil : <b>A</b>");
elseif ($hasil>70)
echo("Hasil : <b>B</b>");
elseif ($hasil>60)
echo("Hasil : <b>C</b>");
elseif ($hasil>40)
echo("Hasil : <b>D</b>");
else
echo("Hasil : <b>E</b>");
?>
As usual, put the php files in the www folder (Wampserver directory). Next, please run via localhost, like this demo:
PHP CRUD Example Guest Data
CRUD stands for Create Read Update Delete, a general concept for processing information system data, technically studied in the discipline of DML (Data Manipulation Language).
The Create and Read methods have been studied in the previous discussion: Creating a Guest Book . So this material will only continue and develop our previous work.
WorkFlow:
- Adding SIGN UP button in index.php
- Create a Registration form
- Creating a Registration Button engine
- Create a page to display the number of Members
- Create an Edit Information form
- Creating an Edit Information Button engine
- Creating a Delete Button engine.
I use the term "engine" as an illustration to make it easier to understand the script, so that the difference between static script and dynamic script is clearer .
- Static script (as GUI/CLOTHES and no interaction with database)
- Dynamic script (as an engine that interacts with the database).
Perhaps there are terms that are still unfamiliar to you, whether it is attributes, elements, instructions, classes, etc. So please access the DBMS DICTIONARY, PHP DICTIONARY, or HTML DICTIONARY, which we have provided specifically, to make it easier to follow the material that we have presented.
1. Add SIGN UP button in index.php
Please open the previous material and edit the index.php file to add a sign up button:
<html>
<!--
*bundet.com
*Wawan Beneran
*GUI ENTRY DATA BUKU TAMU
-->
<head>
<title>Buku Tamu</title>
</head>
<body>
<h2>Contact</h2>
<a href="daftar.php"><button>Sign UP</button></a> <!--Tambahkan Tombol Berikut -->
<hr size="1">
<form name="tamu" method="post" action="simpan.php">
<a> Nama <input style="margin: 5px 5px 5px 20px;" type="text" name="name"></a><br>
Email <input style="margin: 5px 5px 5px 24px;" type="text" name="email"><br>
Alamat <input style="margin: 5px 5px 5px 14px;" type="text" name="address"><br>
Kota <input style="margin: 5px 5px 5px 26px;" type="text" name="city"><br>
Pesan <br><textarea style="width: 210px; margin-bottom: 10px;" name="msg"></textarea><br>
<input type="submit" name="submit" value="Send"> <input type="reset" name="reset" value="Reset">
</form>
</body>
</html>
2. Create a registration form
I will give an example like the following code:
<html>
<!--
*bundet.com
*Wawan Beneran
*GUI/FORM REGISTRASI MEMBER
-->
<head>
<title>Registrasi</title>
</head>
<body>
<h2>Registrasi</h2>
<hr size="1">
<form name="tamu" method="post" action="input_member.php">
Nama <input style="margin: 5px 5px 5px 40px;" type="text" name="name"><br>
Username <input style="margin: 5px 5px 5px 16px;" type="text" name="username"><br>
Email <input style="margin: 5px 5px 5px 44px;" type="text" name="email"><br>
Password <input style="margin: 5px 5px 5px 16px;" type="password" name="password"><br>
<input type="submit" name="submit" value="Submit"> <input type="reset" name="reset" value="Reset">
</form>
</body>
</html>
Save the code with the file name "daftar.php" and place it in the Guestbook folder that we created in the previous discussion.
3. Create Registration Button engine
I will give an example of the following code, and in it I will also add a hyperlink to return to the Registration Form or to the page to see the Member list.
<html>
<!--
*bundet.com
*Wawan Beneran
*Engine Registrasi(Untuk Action Tombol "Submit")
-->
<head>
<title>Contact</title>
</head>
<body>
<h1>REGISTRASI</h1>
<a href="daftar.php">Kembali ke Registrasi</a>
<br>
<h2>MEMBER</h2>
<a href="anggota.php">Lihat Anggota</a>
<hr size=1>
<?php
include ("connect.php");
$nama=$_POST['name'];
$username=$_POST['username'];
$email=$_POST['email'];
$password=$_POST['password'];
//sql entry data pada tabel
$sql = "INSERT INTO anggota (nama, username, email, password)
VALUES ('$nama','$username','$email','$password')";
if ($conn->query($sql) === TRUE) {
echo "Selamat Bergabung!";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
</body>
</html>
Save the script with the file name "input_member.php" and place it in the GuestBook folder.
4. Create a page to display the number of Members
This script is also the result of the adoption of the previous discussion in the file muncul.php, only a little modification and improvisation to display the data, such as packaged in the form of a table defined with the tag <table> where the data/column is defined with the tag <td> while the row is defined with the tag <tr>. (Normal HTML, old material, right?)
<html>
<!--
*bundet.com
*Wawan Beneran
*Engine Daftar Anggota (Untuk Hiperlink "Lihat Anggota")
-->
<head>
<title>Daftar Anggota</title>
</head>
<body>
<h1>Daftar Anggota</h1>
<a href="daftar.php">Kembali ke Registrasi</a>
<br>
<hr size=1>
<?php
include ("connect.php");
// sql menampilkan record
$sql = "SELECT id, nama, username, email, password FROM anggota";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data setiap baris
echo '<table border="1" width="700">';
echo '<tr>';
echo '<td><center>ID</td>';
echo '<td><center>Nama</td>';
echo '<td><center>Username</td>';
echo '<td><center>Email</td>';
echo '<td><center>Password</td>';
echo '<td><center>Action</td>';
echo '</tr>';
while($row = $result->fetch_assoc()) {
echo '<tr>';
echo '<td><center>'.$row["id"].'</td>';
echo '<td><center>'.$row["nama"].'</td>';
echo '<td><center>'.$row["username"].'</td>';
echo '<td><center>'.$row["email"].'</td>';
echo '<td><center>'.$row["password"].'</td>';
echo '<td><center><a href="delete_member.php?id='.$row['id'].'&action=delete">Delete</a>';
echo ' | <a href="edit_member.php?id='.$row['id'].'&action=edit_member.php">Edit</a></center></td>';
echo '</tr>';
}
echo '</table>';
} else {
echo "0 results";
}
$conn->close()
?>
</body>
</html>
Save the script with the file name "anggota.php" and place it in the Guestbook folder.
5. Create an Edit Information form
In this script I also embed the Member List, so that I don't have to bother going back and forth to check.
<html>
<title>:::EDITDATA:::</title>
<!--
*bundet.com
*Wawan Beneran
*GUI/FORM Edit Informasi (Untuk Hiperlink Edit di Daftar Anggota)
-->
<?php
include ("anggota.php");/*saya sertakan juga tampilan Daftar Anggota,
Agar tidak repot bolak-balik hanya untuk melihat hasilnya.*/
?>
<body>
<h2><p>::EDIT DATA::</h2></p>
<form method="post" action="update_member.php">
<table border="0">
<tr>
<td>id</td>
<td>:</td>
<td><input type="text" name="id">
</tr>
<tr>
<td>Nama</td>
<td>:</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>Username</td>
<td>:</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>Email</td>
<td>:</td>
<td><input type="text" name="email"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input type="password" name="password"></td>
</tr>
</table>
<br>
<table border="0">
<tr>
<td><input type="submit" value="Save"></td>
<td><input type="reset" value="Reset"></td>
</tr>
</table></form></body></html>
Save the script with the file name "edit_member.php" and place it in the Guestbook folder.
6. Create an Edit Information Button engine
In this script, I also embed the Member list, of course after the procedure is fulfilled, therefore I include it in the statement block.
<?php
/*
*bundet.com
*Wawan Beneran
*ENGINE UPDATE (Untuk Action Tombol "Save" di From Update)
*/
include ("connect.php");
$id=$_POST['id'];
$nama=$_POST['name'];
$username=$_POST['username'];
$email=$_POST['email'];
$password=$_POST['password'];
// sql entry data pada tabel
$sql = "UPDATE anggota set nama='$nama', username='$username', email='$email', password='$password' where id='$id'";
if ($conn->query($sql) === TRUE) {
echo "Data Telah Diupdate!";
include("anggota.php");
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
?>
Save the script with the file name "edit_memeber.php" and place it in the Guestbook folder.
7. Create the Delete Button engine
In order to see the results immediately, I also embedded the Member List in the following script statement block.
<?php
/*
*bundet.com
*Wawan Beneran
*ENGINE DELETE (Untuk action hiperlink "Delete" di Daftar Anggota)
*/
include ("connect.php");
$sql = "DELETE from anggota where id=$_REQUEST[id]";
if ($conn->query($sql) === TRUE) {
echo "Data Telah Dihapus!";
include("anggota.php");
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
?>
Demo:
Creating a Guestbook With PHP and MySQL
Create a "GuestBook" folder in the directory: wampserver/wamp/www
Create a connection to the database, we just adopt the code in the previous discussion:
Create a connection to the database, we just adopt the code in the previous discussion:
<?php
$hostmysql = "localhost";
$username = "root";
$password = "";
$database = "bukutamu";
$conn = mysqli_connect ($hostmysql, $username, $password, $database);
if ($conn){
echo "<b> Koneksi Berhasil </b>";
}
else{
die ("<b> Koneksi Gagal </b>");
}
?>
Edit the database destination, save it with the file name "connect.php", place it in the Guestbook folder. Create a GUI DATA ENTRY, I will give an example of the following code:
<html>
<!--
*gatewan.com
*Wawan Beneran
*GUI ENTRY DATA BUKU TAMU
-->
<head>
<title>Buku Tamu</title>
</head>
<body>
<h2>Contact</h2>
<hr size="1">
<form name="tamu" method="post" action="simpan.php">
Nama <input style="margin: 5px 5px 5px 20px;" type="text" name="name"><br>
Email <input style="margin: 5px 5px 5px 24px;" type="text" name="email"><br>
Alamat <input style="margin: 5px 5px 5px 14px;" type="text" name="address"><br>
Kota <input style="margin: 5px 5px 5px 26px;" type="text" name="city"><br>
Pesan <br><textarea style="width: 210px; margin-bottom: 10px;" name="msg"></textarea><br>
<input type="submit" name="submit" value="Send"> <input type="reset" name="reset" value="Reset">
</form>
</body>
</html>
Simpan code dengan nama file "index.php" dan letakan di folder BukuTamu.
6. Membuat Engine-nya menggunakan PHP dan query MySQL, saya contohkan code berikut:
<html>
<!--
*gatewan.com
*Wawan Beneran
*ENGINE BUKU TAMU (Untuk Action Tombol "SEND")
-->
<head>
<title>Contact</title>
</head>
<body>
<h1>BUKU TAMU</h1>
<a href="index.php"> Kembali ke Buku Tamu</a>
<br>
<h2>BUKU TAMU</h2>
<a href="tampil.php"> Lihat Buku Tamu</a>
<hr size=1>
<?php
include ("connect.php");
$nama=$_POST['name'];
$email=$_POST['email'];
$alamat=$_POST['address'];
$kota=$_POST['city'];
$pesan=$_POST['msg'];
// sql entry data pada tabel
$sql = "INSERT INTO tamu (name, email, address, city, msg)
VALUES ('$nama','$email','$alamat','$kota','$pesan')";
if ($conn->query($sql) === TRUE) {
echo "Pesan telah terkirim!";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
</body>
</html>
Simpan code dengan nama file "simpan.php" dan letakan di folder BukuTamu.
7. Membuat RECORD DISPLAY, saya contohkan code berikut:
<html>
<!--
*gatewan.com
*Wawan Beneran
*RECORD DISPLAY (Untuk menampilkan seluruh data di Database)
-->
<head>
<title>Contact</title>
</head>
<body>
<h1>BUKU TAMU</h1>
<a href="index.php"> Kembali ke Buku Tamu</a>
<br>
<h2>BUKU TAMU</h2>
<a href="tampil.php"> Lihat Buku Tamu</a>
<hr size=1>
<?php
include ("connect.php");
// sql menampilkan record
$sql = "SELECT id, name, email, address, city, msg FROM tamu";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data setiap baris
while($row = $result->fetch_assoc()) {
echo "<br>".
"id: ". $row["id"]. "<br>".
"Name: ". $row["name"]. "<br>".
"Email: ". $row["email"]. "<br>".
"Alamat: ". $row["address"]. "<br>".
"Kota: ". $row["city"]. "<br>".
"Pesan: ". $row["msg"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close()
?>
</body>
</html>