Difference between revisions of "PHP: Membuat Guest Book Sederhana"
Jump to navigation
Jump to search
<form id="form1" name="form1" method="post" action="addguestbook.php">
</form>
Onnowpurbo (talk | contribs) |
Onnowpurbo (talk | contribs) |
||
Line 72: | Line 72: | ||
</tr> | </tr> | ||
</table> | </table> | ||
+ | |||
+ | |||
+ | ==addguestbook.php== | ||
+ | |||
+ | <?php | ||
+ | $host="localhost"; // Host name | ||
+ | $username=""; // Mysql username | ||
+ | $password=""; // Mysql password | ||
+ | $db_name="test"; // Database name | ||
+ | $tbl_name="guestbook"; // Table name | ||
+ | |||
+ | // 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"); | ||
+ | |||
+ | $datetime=date("y-m-d h:i:s"); //date time | ||
+ | |||
+ | $sql="INSERT INTO $tbl_name(name, email, comment, datetime)VALUES('$name', '$email', '$comment', '$datetime')"; | ||
+ | $result=mysql_query($sql); | ||
+ | |||
+ | //check if query successful | ||
+ | if($result){ | ||
+ | echo "Successful"; | ||
+ | echo "<BR>"; | ||
+ | |||
+ | // link to view guestbook page | ||
+ | echo "<a href='viewguestbook.php'>View guestbook</a>"; | ||
+ | } | ||
+ | |||
+ | else { | ||
+ | echo "ERROR"; | ||
+ | } | ||
+ | |||
+ | mysql_close(); | ||
+ | ?> |
Revision as of 13:21, 9 May 2018
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
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
Buku Tamu |
|
<a href="viewguestbook.php">Lihat Buku Tamu</a> |
addguestbook.php
<?php $host="localhost"; // Host name $username=""; // Mysql username $password=""; // Mysql password $db_name="test"; // Database name $tbl_name="guestbook"; // Table name // 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"); $datetime=date("y-m-d h:i:s"); //date time $sql="INSERT INTO $tbl_name(name, email, comment, datetime)VALUES('$name', '$email', '$comment', '$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(); ?>