Difference between revisions of "PHP: Membuat Guest Book Sederhana"

From OnnoWiki
Jump to navigation Jump to search
 
(4 intermediate revisions by the same user not shown)
Line 11: Line 11:
  
  
==Membuat tabel guestbook==
+
==Membuat tabel guestbook (jangan lupa view source)==
  
 
  mysql -u root -p123456
 
  mysql -u root -p123456
Line 27: Line 27:
 
  PRIMARY KEY (`id`)
 
  PRIMARY KEY (`id`)
 
  ) DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
 
  ) DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
 
  
 
==guestbook.php==
 
==guestbook.php==
  
 
+
  <!DOCTYPE HTML>
<table width="400" border="0" align="center" cellpadding="3" cellspacing="0">
+
  <html>
<tr>
+
  <body>
<td><strong>Buku Tamu </strong></td>
+
  <form action="addguestbook.php" method="post">
</tr>
+
  Nama: <input type="text" name="name"><br>
  </table>
+
  Email: <input type="text" name="email"><br>
  <table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
+
  Komentar: <input type="text" name="comment"><br>
  <tr>
+
  <input type="submit">
  <form id="form1" name="form1" method="post" action="addguestbook.php">
 
<td>
 
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
 
  <tr>
 
<td width="117">Nama</td>
 
<td width="14">:</td>
 
<td width="357"><input name="name" type="text" id="name" size="40" /></td>
 
</tr>
 
<tr>
 
  <td>Email</td>
 
<td>:</td>
 
<td><input name="email" type="text" id="email" size="40" /></td>
 
</tr>
 
<tr>
 
  <td valign="top">Komentar</td>
 
<td valign="top">:</td>
 
<td><textarea name="comment" cols="40" rows="3" id="comment"></textarea></td>
 
</tr>
 
<tr>
 
<td>&nbsp;</td>
 
<td>&nbsp;</td>
 
  <td><input type="submit" name="Submit" value="Submit" /> <input type="reset" name="Submit2" value="Reset" /></td>
 
</tr>
 
</table>
 
</td>
 
 
  </form>
 
  </form>
  </tr>
+
  </body>
  </table>
+
  </html>
<table width="400" border="0" align="center" cellpadding="3" cellspacing="0">
 
<tr>
 
<td><strong><a href="viewguestbook.php">Lihat Buku Tamu</a> </strong></td>
 
</tr>
 
</table>
 
 
 
  
 
==addguestbook.php==
 
==addguestbook.php==
Line 78: Line 46:
 
  <?php
 
  <?php
 
  $host="localhost"; // Host name
 
  $host="localhost"; // Host name
  $username=""; // Mysql username
+
  $username="root"; // Mysql username
  $password=""; // Mysql password
+
  $password="123456"; // Mysql password
 
  $db_name="test"; // Database name
 
  $db_name="test"; // Database name
 
  $tbl_name="guestbook"; // Table name
 
  $tbl_name="guestbook"; // Table name
 +
 +
$datetime=date("y-m-d h:i:s"); //date time
 +
$name1=$_POST["name"];
 +
$email1=$_POST["email"];
 +
$comment1=$_POST["comment"];
 
   
 
   
 
  // Connect to server and select database.
 
  // Connect to server and select database.
Line 87: Line 60:
 
  mysql_select_db("$db_name")or die("cannot select DB");
 
  mysql_select_db("$db_name")or die("cannot select DB");
 
   
 
   
$datetime=date("y-m-d h:i:s"); //date time
+
  $sql="INSERT INTO $tbl_name(name, email, comment, datetime)VALUES('$name1', '$email1', '$comment1', '$datetime')";
 
  $sql="INSERT INTO $tbl_name(name, email, comment, datetime)VALUES('$name', '$email', '$comment', '$datetime')";
 
 
  $result=mysql_query($sql);
 
  $result=mysql_query($sql);
 
   
 
   
Line 107: Line 78:
 
  mysql_close();
 
  mysql_close();
 
  ?>
 
  ?>
 +
 +
==viewguestbook.php==
 +
 +
<?php
 +
$servername = "localhost";
 +
$username = "root";
 +
$password = "123456";
 +
$dbname = "test";
 +
 +
// Create connection
 +
$conn = new mysqli($servername, $username, $password, $dbname);
 +
// Check connection
 +
if ($conn->connect_error) {
 +
    die("Connection failed: " . $conn->connect_error);
 +
}
 +
 +
$sql = "SELECT id, name, email, comment, datetime FROM guestbook";
 +
$result = $conn->query($sql);
 +
 +
if ($result->num_rows > 0) {
 +
    // output data of each row
 +
    while($row = $result->fetch_assoc()) {
 +
        echo "id: " . $row["id"]. " - Name: " . $row["name"]. " " . $row["email"]. " " . $row["comment"]. " " . $row["datetime"]. "<br>";
 +
    }
 +
} else {
 +
    echo "0 results";
 +
}
 +
$conn->close();
 +
?>
 +
 +
 +
 +
==Pranala Menarik==
 +
 +
* [[Arduino: RFID RC522]]

Latest revision as of 06:32, 1 July 2019

Sumber:

http://www.phpeasystep.com/phptu/15.html
http://www.phpeasystep.com/workshopview.php?id=16


Yang perlu dibuat

  • Table "guestbook" in database "test".
  • guestbook.php.
  • addguestbook. php.
  • viewguestbook.php


Membuat tabel guestbook (jangan lupa view source)

mysql -u root -p123456
create database test;
use test;
grant ALL on root.* to test@localhost;
CREATE TABLE `guestbook` (
`id` int(4) NOT NULL auto_increment,
`name` varchar(65) NOT NULL default ,
`email` varchar(65) NOT NULL default ,
`comment` longtext NOT NULL,
`datetime` varchar(65) NOT NULL default ,
PRIMARY KEY (`id`)
) DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

guestbook.php

<!DOCTYPE HTML>
<html>
<body>
<form action="addguestbook.php" method="post">
Nama: <input type="text" name="name">
Email: <input type="text" name="email">
Komentar: <input type="text" name="comment">
<input type="submit"> </form> </body> </html>

addguestbook.php

<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="123456"; // Mysql password
$db_name="test"; // Database name
$tbl_name="guestbook"; // Table name

$datetime=date("y-m-d h:i:s"); //date time
$name1=$_POST["name"];
$email1=$_POST["email"];
$comment1=$_POST["comment"];

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect server ");
mysql_select_db("$db_name")or die("cannot select DB");

$sql="INSERT INTO $tbl_name(name, email, comment, datetime)VALUES('$name1', '$email1', '$comment1', '$datetime')";
$result=mysql_query($sql);

//check if query successful
if($result){
echo "Successful";
echo "
"; // link to view guestbook page echo "<a href='viewguestbook.php'>View guestbook</a>"; } else { echo "ERROR"; } mysql_close(); ?>

viewguestbook.php

<?php
$servername = "localhost";
$username = "root";
$password = "123456";
$dbname = "test";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT id, name, email, comment, datetime FROM guestbook";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["id"]. " - Name: " . $row["name"]. " " . $row["email"]. " " . $row["comment"]. " " . $row["datetime"]. "
"; } } else { echo "0 results"; } $conn->close(); ?>


Pranala Menarik