Pages

Translate

Friday, August 31, 2012

Php Script For Add And Update Function


hi... .lama dh x bukak vf ni... kali ni sy akan meletakkan artikel yang mengandungi php script utk fungsi add dan update... .

1)create conn.php file

<?php
$db_name="tuto";// Database name
$connection=mysql_connect("localhost","root","" or die("I Couldn't connect" ;
$db=mysql_select_db($db_name,$connection) or die("I Couldn't select your database" ;
$profil="profil"; // Table name
?>

2)create form.php file

<!-- form.php - Update And Add Record Form File -->
<html>
<head>
<title>Tuto</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<table align="center" bgcolor="#FFFFFF">
<tr>
<td> </td>
</tr>
<tr>
<td> <table align="center" cellpadding="2" cellspacing="2">
<tr>
<td> </td>
</tr>
<tr>
<td>
<?
$mode=$_GET["mode"];
if($mode=="add" {
?>
<form name="form1" method="post" action="formsubmit.php?mode=add">
<table width="500" border="1" align="center" cellpadding="2" cellspacing="2">
<tr>
<td><strong>Tambah Profil Baru </strong></td>
<td> </td>
</tr>
<tr>
<td>Nama Penuh </td>
<td><input name="namapenuh" type="text" id="namapenuh"></td>
</tr>
<tr>
<td>Nama Panggilan</td>
<td><input name="namapanggilan" type="text" id="namapanggilan"></td>
</tr>
<tr>
<td><input type="submit" name="Submit" value="Simpan Data"></td>
<td> </td>
</tr>
</table>
</form>
<?
} else {
include("conn.php" ;
$id=$_GET["id"];
$sql="select id,namapenuh,namapanggilan from $profil where id='$id'";

$result=mysql_query($sql,$connection) or die(mysql_error());
while($row=mysql_fetch_array($result)) {
$id=$row['id'];
$namapenuh=$row['namapenuh'];
$namapanggilan=$row['namapanggilan'];
}
?>
<form name="form1" method="post" action="formsubmit.php?mode=update">
<table border="1" align="center" cellpadding="2" cellspacing="2">
<tr>
<td><strong>Update Profil </strong></td>
<td><input type="hidden" name="id" value="<? echo $id; ?>">
</td>
</tr>
<tr>
<td>Nama Penuh </td>
<td><input name="namapenuh" type="text" id="namapenuh" value="<? echo $namapenuh; ?>"></td>
</tr>
<tr>
<td>Nama Panggilan </td>
<td><input name="namapanggilan" type="text" id="namapanggilan" value="<? echo $namapanggilan; ?>"></td>
</tr>
<tr>
<td><input type="submit" name="Submit" value="Update Data"></td>
<td> </td>
</tr>
</table>
</form>

<?

}
?>

</td>
</tr>
<tr>
<td> </td>
</tr>
</table></td>
</tr>
</table>
</body>
</html>

3)create formsubmit.php file

<!-- formsubmit.php - Update and submit new record file -->
<?php
include("conn.php" ;
$mode=$_GET["mode"];
if($mode=="add" {
$namapenuh=$_POST["namapenuh"];
$namapanggilan=$_POST["namapanggilan"];
$sql="insert into $profil(namapenuh,namapanggilan) values('$namapenuh','$namapanggilan')";
$result=mysql_query($sql,$connection) or die(mysql_error());
header("location: index.php" ;

} elseif($mode=="update" {
$namapenuh=$_POST["namapenuh"];
$namapanggilan=$_POST["namapanggilan"];
$id=$_POST["id"];
$sql="update $profil set namapenuh='$namapenuh',namapanggilan='$namapanggilan' where id='$id'";
//echo $sql;
$result=mysql_query($sql,$connection) or die(mysql_error());
header("location: index.php" ;
}

?>

4) create index.php file

<!--index.php - The main Index File -->
<html>
<head>
<title>Tuto</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<table>
<tr>
<td>
<form action="" method="post" name="" >
<table border="1" align="center">
<tr>
<td colspan="3" align="center"><a href="form.php?mode=add">Tambah Profil Baru </a></td>
</tr>
<tr>

<td><strong>Nama Penuh</strong></td>
<td><strong>Nama Panggilan</strong></td>
<td><strong>Update</strong></td>

</tr>
<?
include("conn.php" ;
$sql="select id,namapenuh,namapanggilan from $profil order by namapenuh";
$result=mysql_query($sql,$connection) or die(mysql_error());
while($row=mysql_fetch_array($result)) {
?>
<tr>

<td><? echo $row['namapenuh']; ?></td>
<td><? echo $row['namapanggilan']; ?></td>
<td><a href="<? echo "form.php?id=".$row['id']."&mode=update"; ?>">Update</a></td>

</tr>
<? } ?>

</table>
</form></td>
</tr>
</table>
</body>
</html>

5)semua script diatas memerlukan satu database dan diharap pandai2 la create database dan table untuk simple function ni ek... ..gud luck  

No comments:

Post a Comment