Last active
March 14, 2018 04:53
-
-
Save auriza/aba559515b329c3e4d62d4c6d8591af7 to your computer and use it in GitHub Desktop.
Contoh aplikasi web PHP dengan PostgreSQL dan SQLite3
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
<?php | |
/* | |
CREATE TABLE bukutamu ( | |
id SERIAL PRIMARY KEY, | |
nama VARCHAR(50) NOT NULL, | |
email VARCHAR(50) NOT NULL, | |
pesan TEXT NOT NULL | |
); | |
*/ | |
pg_connect("host=localhost dbname=student user=student password=student") | |
or exit("CONNECT ERROR"); | |
if ($_POST['tambah'] === "Tambah") { | |
$nama = $_POST['nama']; | |
$email = $_POST['email']; | |
$pesan = $_POST['pesan']; | |
$ok = pg_query_params("INSERT INTO bukutamu (nama, email, pesan) VALUES ($1, $2, $3)", array($nama, $email, $pesan)); | |
if (!$ok) echo "<pre>".pg_last_error()."</pre>"; | |
} | |
if ($_POST['update'] === "Update") { | |
$id = $_POST['id']; | |
$nama = $_POST['nama']; | |
$email = $_POST['email']; | |
$pesan = $_POST['pesan']; | |
$ok = pg_query_params("UPDATE bukutamu SET nama=$1, email=$2, pesan=$3 WHERE id=$4", array($nama, $email, $pesan, $id)); | |
if (!$ok) echo "<pre>".pg_last_error()."</pre>"; | |
} | |
if ($_POST['hapus'] === "Hapus") { | |
$id = $_POST['id']; | |
$ok = pg_query_params("DELETE FROM bukutamu WHERE id=$1", array($id)); | |
if (!$ok) echo "<pre>".pg_last_error()."</pre>"; | |
} | |
if ($_POST['edit'] === "Edit") { | |
$id = $_POST['id']; | |
$result = pg_query_params("SELECT * FROM bukutamu WHERE id=$1", array($id)); | |
if (!$result) | |
echo "<pre>".pg_last_error()."</pre>"; | |
else | |
$row = pg_fetch_array($result); | |
} | |
?> | |
<h2>Form Pesan</h2> | |
<form method="post" action=""> | |
<input type="hidden" name="id" value="<?php echo $row['id']; ?>"> | |
<p>Nama: <br><input type="text" name="nama" value="<?php echo $row['nama']; ?>" required></p> | |
<p>Email:<br><input type="email" name="email" value="<?php echo $row['email']; ?>" required></p> | |
<p>Pesan:<br><textarea required name="pesan" ><?php echo $row['pesan']; ?></textarea></p> | |
<p> | |
<?php if ($_POST['edit'] === "Edit") { ?> | |
<input type="submit" name="update" value="Update"> | |
<?php } else { ?> | |
<input type="submit" name="tambah" value="Tambah"> | |
<?php } ?> | |
</p> | |
</form> | |
<h2>Daftar Pesan</h2> | |
<?php | |
$result = pg_query("SELECT * FROM bukutamu ORDER BY id DESC"); | |
if (!$result) | |
echo "<pre>".pg_last_error()."</pre>"; | |
else { | |
while ($row = pg_fetch_array($result)) { | |
$id = $row['id']; | |
$nama = htmlspecialchars($row['nama'], ENT_QUOTES); | |
$email = htmlspecialchars($row['email'], ENT_QUOTES); | |
$pesan = htmlspecialchars($row['pesan'], ENT_QUOTES); | |
?> | |
<article> | |
<i>"<?php echo $pesan; ?>"</i><br> | |
– <a href="mailto:<?php echo $email; ?>"><?php echo $nama; ?></a> | |
<form method="post" action=""> | |
<input type="hidden" name="id" value="<?php echo $id; ?>"> | |
<input type="submit" name="edit" value="Edit"> | |
<input type="submit" name="hapus" value="Hapus"> | |
</form> | |
</article> | |
<?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
<?php | |
/* | |
$ sqlite3 bukutamu.db | |
CREATE TABLE bukutamu ( | |
id INTEGER PRIMARY KEY, | |
nama VARCHAR(50) NOT NULL, | |
email VARCHAR(50) NOT NULL, | |
pesan TEXT NOT NULL | |
); | |
$ chmod o+w bukutamu.db | |
$ chmod o+w $(pwd) | |
*/ | |
$db = new SQLite3("bukutamu.db"); | |
if ($_POST['tambah'] === "Tambah") { | |
$nama = $db->escapeString($_POST['nama']); | |
$email = $db->escapeString($_POST['email']); | |
$pesan = $db->escapeString($_POST['pesan']); | |
$ok = $db->exec("INSERT INTO bukutamu (nama, email, pesan) VALUES ('$nama', '$email', '$pesan')"); | |
if (!$ok) echo $db->lastErrorMsg(); | |
} | |
if ($_POST['update'] === "Update") { | |
$id = $db->escapeString($_POST['id']); | |
$nama = $db->escapeString($_POST['nama']); | |
$email = $db->escapeString($_POST['email']); | |
$pesan = $db->escapeString($_POST['pesan']); | |
$ok = $db->exec("UPDATE bukutamu SET nama='$nama', email='$email', pesan='$pesan' WHERE id=$id"); | |
if (!$ok) echo $db->lastErrorMsg(); | |
} | |
if ($_POST['hapus'] === "Hapus") { | |
$id = $db->escapeString($_POST['id']); | |
$ok = $db->exec("DELETE FROM bukutamu WHERE id=$id"); | |
if (!$ok) echo $db->lastErrorMsg(); | |
} | |
if ($_POST['edit'] === "Edit") { | |
$id = $db->escapeString($_POST['id']); | |
$result = $db->query("SELECT * FROM bukutamu WHERE id=$id"); | |
if (!$result) | |
echo $db->lastErrorMsg(); | |
else | |
$row = $result->fetchArray(); | |
} | |
?> | |
<h2>Form Pesan</h2> | |
<form method="post" action=""> | |
<input type="hidden" name="id" value="<?php echo $row['id']; ?>"> | |
<p>Nama: <br><input type="text" name="nama" value="<?php echo $row['nama']; ?>" required></p> | |
<p>Email:<br><input type="email" name="email" value="<?php echo $row['email']; ?>" required></p> | |
<p>Pesan:<br><textarea required name="pesan" ><?php echo $row['pesan']; ?></textarea></p> | |
<p> | |
<?php if ($_POST['edit'] === "Edit") { ?> | |
<input type="submit" name="update" value="Update"> | |
<?php } else { ?> | |
<input type="submit" name="tambah" value="Tambah"> | |
<?php } ?> | |
</p> | |
</form> | |
<h2>Daftar Pesan</h2> | |
<?php | |
$result = $db->query("SELECT * FROM bukutamu ORDER BY id DESC"); | |
if (!$result) | |
echo $db->lastErrorMsg(); | |
else { | |
while ($row = $result->fetchArray()) { | |
$id = $row['id']; | |
$nama = htmlspecialchars($row['nama'], ENT_QUOTES); | |
$email = htmlspecialchars($row['email'], ENT_QUOTES); | |
$pesan = htmlspecialchars($row['pesan'], ENT_QUOTES); | |
?> | |
<article> | |
<i>"<?php echo $pesan; ?>"</i><br> | |
– <a href="mailto:<?php echo $email; ?>"><?php echo $nama; ?></a> | |
<form method="post" action=""> | |
<input type="hidden" name="id" value="<?php echo $id; ?>"> | |
<input type="submit" name="edit" value="Edit"> | |
<input type="submit" name="hapus" value="Hapus"> | |
</form> | |
</article> | |
<?php | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment