php beginer usually make two form for Editing and inputing data to mysql, one form for input and other one for editing, It doesn't need two form to input and edit your data in php, we just need one form for both process. We can manipulate variable form to do it both.
1. let make a table in mysql's database (database name it up to you)
see the mysql syntax here :
2. make a php file
here is the source
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
//connection variable
$server = "localhost";
$user = "root";
$password = "";
$koneksi = mysql_connect ($server, $user, $password) or die ("koneksi gagal");
$db = "company";
//----------------
if(isset($_GET['idemp']))
{
$idemp =$_GET['idemp'];
$datasek = mysql_db_query($db, "select * from employes where id= $idemp", $koneksi);
list($myidemp, $myemp, $myadd)= mysql_fetch_row($datasek);
$button = "update";
}else{
$button = "save";
}
if(!isset($_POST['save']) && !isset($_POST['update']))
{
?>
<h1>Employes</h1>
<br>
<form method="post" action="<?php echo $PHP_SELF ?>" name="myfrm">
<table>
<tr><td>Employe Name</td><td>:</td><td><input type="text" name="empname" size="35" value="<?php echo $myemp ?>"></td></tr>
<tr><td>Employe Address</td><td>:</td><td><textarea name="address" style="border-bottom-color:#99FF00"><?php echo $myadd ?></textarea></td></tr>
<tr>
<td colspan="3" align="center">
<input type="submit" name="<?php echo $button ?>" value="<?php echo $button ?>">
<input type="reset" value="Batal"></td>
</tr>
</table>
</form>
<?php
$show = mysql_db_query($db,"select * from employes",$koneksi);
//$myrow = mysql_num_rows($show);
//if($myrow>0){
?>
<table>
<tr><td>Name</td><td>Address</td><td>Edit</td></tr>
<?php
while(list($id, $empname, $empaddress)=mysql_fetch_row($show)){
?>
<tr><td><?php echo $empname ?></td><td><?php echo $empaddress ?></td><td><a href="inputedit.php?idemp=<?php echo $id ?>">Edit</a></td></tr>
<?php
}
?>
</table>
<?php
//}
}
else
{
$empname = $_POST['empname'];
$address = $_POST['address'];
if(isset($_POST['save'])){
$query = "insert into employes values(null,
'$empname',
'$address')";
$rquery = mysql_db_query($db, $query, $koneksi) or die("error Query");
echo "Data Saved";
}elseif(isset($_POST['update'])){
$qupdate = "update employes set emp_name = '".addslashes($empname)."', emp_address = '".addslashes($address)."' Where id = $idemp";
$hupdate = mysql_db_query($db, $qupdate, $koneksi);
echo "Data Updated";
}
?>
<br>
<a href="inputedit.php">Back</a>
<?php
}
?>
</body>
</html>
You can download this source
here
Judul : Input and Edit your Data in one form
Deskripsi : php beginer usually make two form for Editing and inputing data to mysql, one form for input and other one for editing, It doesn't nee...