INDEX.PHP
<html>
<body>
<div style="border:1px solid;">
<form action="add.php" method="post">
Fullname <input type="text" name="name"><br>
Age <input type="text" name="age"><br>
<input type="submit" value="Submit">
<input type="reset" value="Reset">
</form>
</div>
</body>
</html>
ADD.PHP
<?php
mysql_connect("localhost","root","") or die (mysql_error());
echo "sulod";
mysql_select_db("datamex") or die (mysql_error());
echo "abre na ";
$name= ($_POST['name']);
$age = ($_POST['age']);
mysql_query("insert users set name='".$name."', age='".$age."'");
echo "<script>alert('Record successfuly saved.');window.location.href='paragview.php';</script>";
?>
VIEW.PHP
<?php
$konek = mysql_connect("localhost","root","") or die("Cannot connect to server");
mysql_select_db("datamex",$konek) or die("Cannot connect to the database");
$query = mysql_query("select * from users order by id asc");
?>
<table border="1">
<tr>
<td>Name</td>
<td>Age</td>
<td>Action</td>
</tr>
<?php
if(mysql_num_rows($query)>0){
while($row= mysql_fetch_array($query)){ ?>
<tr>
<td><?=$row['name']?></td>
<td><?=$row['age']?></td>
<td><a href="update.php?id=<?=$row['id']?>">[Edit]</a> <a href="delete.php?id=<?=$row['id']?>">[Delete]</a></td>
</tr>
<?php
}
}
else{
echo "no record";
}
?>
<tr><td colspan="3"><a href="index.php" >[Add new record]</a>
</td></tr>
</table>
EDIT.PHP
<?php
$konek = mysql_connect("localhost","root","") or die("Cannot connect to server");
mysql_select_db("datamex",$konek) or die("Cannot connect to the database");
$name= ($_POST['name'])?$_POST['name']:'';
$age= ($_POST['age'])?$_POST['age']:'';
$id = ($_POST['id'])?$_POST['id']:'';
if($name=="" || $age==""){
echo "<script>alert('Complete all field');history.back();</script>";
}else{
mysql_query("update users set name='".$name."', age='".$age."' where id='".$id."'");
echo "<script>alert('Record successfuly updated.');window.location.href='view.php';</script>";
}
?>
UPDATE.PHP
<?php
$konek = mysql_connect("localhost","root","") or die("Cannot connect to server");
mysql_select_db("datamex",$konek) or die("Cannot connect to the database");
$query = mysql_query("select * from users where id='".$_GET['id']."'");
$row = mysql_fetch_array($query);
?>
<html>
<body>
<div style="border:1px solid;">
<form action="edit.php" method="post">
<input type='hidden' name="id" value="<?=($_GET['id'])?$_GET['id']:''?>">
Fullname <input type="text" name="name" value="<?=$row['name']?>"><br>
Age <input type="text" name="age" value="<?=$row['age']?>"><br>
<input type="submit" value="Update">
<input type="reset" value="Reset">
</form>
<a href="index.php" >[Back to home]</a>
</div>
</body>
</html>
DELETE.PHP
<?php
$konek = mysql_connect("localhost","root","") or die("Cannot connect to server");
mysql_select_db("datamex",$konek) or die("Cannot connect to the database");
$id = $_GET['id'];
mysql_query("delete from users where id='".$id."'");
echo "<script>alert('Record successfuly updated.');window.location.href='view.php';</script>";
?>
Home
»
phpexamples
»
SIMPLE PHP ADD, VIEW, EDIT, UPDATE, DELETE
SIMPLE PHP ADD, VIEW, EDIT, UPDATE, DELETE
Labels:
phpexamples
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment