lu bisa coba contoh script ini:
SQLCREATE TABLE IF NOT EXISTS `cpanel` (
`id` int(11) NOT NULL auto_increment,
`userName` varchar(255) NOT NULL default '',
`passWord` varchar(255) NOT NULL default '',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;
INSERT INTO `cpanel` (`id`, `userName`, `passWord`) VALUES
(1, 'admin', '202cb962ac59075b964b07152d234b70');
CREATE TABLE IF NOT EXISTS `manage_data` (
`id` int(11) NOT NULL auto_increment,
`title` varchar(255) NOT NULL default '',
`post` text NOT NULL,
`time` datetime NOT NULL default '0000-00-00 00:00:00',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
INSERT INTO `manage_data` (`id`, `title`, `post`, `time`) VALUES
(1, 'The Secrets to Keywords - Expanding Your List', 'The Primary objective of a business website is to attract potential buyers and convert them into paying customers. Relevant keywords allow you to fulfil this primary objective. They help you attract relevant traffic that want to buy the types of products you have to offer, however, the more relevant keywords that you rank well for, the more business you will do. By effectively expanding your keyword lists, you expand the potential market for your business.', '2008-11-10 23:32:54');
config.php<?
/*
==============================================
Nama File: config.php
Fungsi: basic configuration
Author: Hairul Azami - webmaster@dremi.info
==============================================
*/
define("_SERVER", "localhost");
define("_USER", "root");
define("_PASS", "123");
define("_DB", "ujicoba");
define("_TBL_DATA", "manage_data");
define("_TBL_USER", "cpanel");
define("_ABSOLUTE_PATH", "/ujicoba/update-delete-insert-data-db/");
?>
functions.php<?
/*
================================================
Nama File: functions.php
Fungsi: functions producer for global actions
Author: Hairul Azami - webmaster@dremi.info
================================================
*/
//require
require_once("config.php");
//connection1
function DB_CON1()
{
global $db_link1;
@$db_link1 = mysql_connect(""._SERVER."",""._USER."",""._PASS."") or die("Miss Connection #1 $php_errormsg");
if ($db_link1) @mysql_select_db(""._DB."") or die ("Warning: Unable to Select Database #1, Please Check Database Name");
return $db_link1;
}
function DB_CLOSE1()
{
global $db_link1;
@$db_link1 = mysql_close(""._SERVER."",""._USER."",""._PASS."");
return $db_link1;
}
//formLogin
function formLogin()
{
?>
<script language="javascript">
function blockEnter() {
if (event.keyCode ==13) {
return event.keyCode = 9;
}
return true;
}
</script>
<div id="FormBox">
<form id="login" name="login" method="post" action="">
<label>Username:</label>
<input name="usern" type="text" class="input" onKeyDown="return blockEnter()" size="34" maxlength="50"/>
<label>Password :</label>
<input name="passw" type="password" class="input" size="34" maxlength="50"/>
<div class="spacer"> </div>
<input name="submit" type="submit" class="submit" value="" />
</form>
</div>
<div class="spacer"> </div>
Begin your fun post with login before ...<br />
© 2008 dremi.info
<script language="JavaScript1.2">document.login.usern.focus();</script>
<?
}
//formInsert
function formInsert()
{
?>
<script language="javascript">
function blockEnter() {
if (event.keyCode ==13) {
return event.keyCode = 9;
}
return true;
}
</script>
<div id="FormBox">
<form id="Insert" name="Insert" method="post" action="">
<h1>Insert Data</h1>
<label>Title:</label>
<input name="title" type="text" class="input" onKeyDown="return blockEnter()" size="34" maxlength="50"/>
<label>Post:</label>
<textarea name="post" class="input" cols="38" rows="5"></textarea>
<div class="spacer"> </div>
<input name="submit" type="submit" class="submit" value="" />
</form>
</div>
<script language="JavaScript1.2">document.login.usern.focus();</script>
<?
}
//formUpdate
function formUpdate($id)
{
$ROW=mysql_fetch_array(mysql_query("SELECT * FROM "._TBL_DATA."
WHERE id = $id"));
?>
<script language="javascript">
function blockEnter() {
if (event.keyCode ==13) {
return event.keyCode = 9;
}
return true;
}
</script>
<div id="FormBox">
<form id="Update" name="Update" method="post" action="">
<h1>Update Data</h1>
<label>Title:</label>
<input name="title" type="text" class="input" onKeyDown="return blockEnter()" size="34" maxlength="50" value="<? echo $ROW['title']; ?>"/>
<label>Post:</label>
<textarea name="post" class="input" cols="38" rows="5"><? echo $ROW['post']; ?></textarea>
<div class="spacer"> </div>
<input name="submit" type="submit" class="submit" value="" />
</form>
</div>
<script language="JavaScript1.2">document.login.usern.focus();</script>
<?
}
//login user akses
function login($usern,$passw)
{
DB_CON1();
global $row;
$pass_enc=md5($passw);
$row=mysql_fetch_array(mysql_query("SELECT * FROM "._TBL_USER."
WHERE userName='".$usern."' and passWord='".$pass_enc."'"));
return $row;
}
//redirect halaman
function redirect($delay,$goto,$msg)
{
echo"<strong>$msg</strong><br>Redirect progress..<br>Please Stand By.. <meta http-equiv=\"refresh\" content=\"$delay;URL=$goto\" />";
}
?>
index.php<?
session_start();
include_once("config.php");
include_once("functions.php");
DB_CON1();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>[Demo Script] Update, Delete, Insert Data pada Table Database</title>
<style type="text/css">
/* CSS Web 2.0 Builder by dr.emi */
body,td,th { font-family: Arial; font-size: 11px; color: #000000; }
body { background-color: #FFFFFF; margin: 10px 10px 10px 10px; }
#BlueSky { background-color: #F2F9FF; padding: 30px; margin-bottom:5px; width:350px; border:1px solid #91CBF7; }
#FormBox { margin:0px;padding:0px; }
#FormBox h1 { color:#333399; font-size:13px;margin-bottom:10px;padding:0px; }
#FormBox p { color:#777777; font-size:10px;margin-bottom:20px;padding:0px; }
#FormBox label { margin-bottom:10px; color:#555555;display:block;font-weight:bold; font-size:12px; }
#FormBox .input { margin-bottom:20px; border: 1px solid #cdcdcd; padding: 0.2em; background: #FFFFFF url("images/field.gif") repeat-x 0 1px; color:#666666; }
#FormBox .input:hover { margin-bottom:20px; border: 1px solid #FF0000; padding: 0.2em; background: #FFFFFF url("images/field.gif") repeat-x 0 1px; color:#666666; }
#FormBox .submit { width:117px; height:40px; border:none; background: #FFFFFF url("images/button.gif") no-repeat 0 0; }
#FormBox .spacer { margin-bottom:10px; }
a:link {
color: #0099FF;
}
a:visited {
color: #0099FF;
}
a:hover {
color: #009900;
}
a:active {
color: #0099FF;
}
</style></head>
<body>
<h2>[Demo Script] Update, Delete, Insert Data pada Table Database</h2>
<?
if(isset($LogOut))
{
unset($usern);
unset($passw);
unset($inTime);
unset($getIP);
session_destroy();
}
if(!session_is_registered('usern'))
{
if(isset($_POST['submit']))
{
login($_POST['usern'], $_POST['passw']);
if($row)
{
$usern = $_POST['usern'];
$passw = $_POST['passw'];
$inTime = date("d-M-Y H:i:s");
$getIP = $_SERVER['REMOTE_ADDR'];
$pass_enc = md5($_POST['passw']);
session_register('usern');
session_register('passw');
session_register('inTime');
session_register('getIP');
SetCookie("logged-in", "at", time()+600);
header("location:"._ABSOLUTE_PATH);
break;
}
else
{
header("location:"._ABSOLUTE_PATH."?".rand()."");
}
}
formLogin();
die();
}
else
{
?>
<h2>Pilih Menu Navigasi</h2>
<p>
<a href="?">Home</a> |
<a href="?Insert">Insert</a> |
<a href="?LogOut">Logout { <? echo $_SESSION['usern']; ?> } </a>
</p>
<?
if(isset($Insert))
{
if(isset($_POST['submit']))
{
if(!empty($_POST['title']) && !empty($_POST['post']))
{
if(mysql_query("INSERT INTO "._TBL_DATA."
(title, post, time)
VALUES
('".trim($_POST['title'])."', '".trim($_POST['post'])."',
'".date("Y-m-d H:i:s")."')"))
{
redirect(3, _ABSOLUTE_PATH, "Sukses !");
}
else
{
redirect(3, _ABSOLUTE_PATH, "Error !");
}
}
}
formInsert();
}
elseif(isset($Update) && !empty($_GET['id']))
{
if(isset($_POST['submit']))
{
if(!empty($_POST['title']) && !empty($_POST['post']))
{
if(mysql_query("UPDATE "._TBL_DATA."
SET title='".trim($_POST['title'])."', post='".trim($_POST['post'])."'
WHERE id = '$_GET[id]'"))
{
redirect(3, _ABSOLUTE_PATH, "Sukses !");
}
else
{
redirect(3, _ABSOLUTE_PATH, "Error !");
}
}
}
formUpdate($_GET['id']);
}
elseif(isset($Delete) && !empty($_GET['id']))
{
if(mysql_query("DELETE FROM "._TBL_DATA."
WHERE id = '$_GET[id]'"))
{
redirect(3, _ABSOLUTE_PATH, "Sukses !");
}
else
{
redirect(3, _ABSOLUTE_PATH, "Error !");
}
}
##START YOUR LIST DATA
$QRY=mysql_query("SELECT * FROM "._TBL_DATA."");
$i=0;
?>
<h3>Data Post</h3>
<?
while($ROW=mysql_fetch_array($QRY))
{
$i++;
?>
<h3>No. <? echo $i; ?></h3>
<strong>On: <? echo $ROW['time']; ?></strong>
<br />
<strong>Title: <? echo $ROW['title']; ?></strong>
<br />
<strong>Post: </strong><br />
<? echo $ROW['post']; ?><br /><br />
<a href="?Update&id=<? echo $ROW[id]; ?>">Edit</a> |
<a href="javascript:;" onclick="cf=confirm('Are you Sure to remove post number <? echo $i; ?> ???');if(cf)window.location='?Delete&id=<? echo $ROW[id]; ?>';return false;">
Delete</a>
<hr style="color:#EFEFEF; width:100%; float:left; height:0.5px;" />
<?
}
}
?>
</body>
</html>
<?
DB_CLOSE1();
?>
download disni filenya :
