add,edit,delete source code in php


Simple php code untuk add,delete dan view data…silahkan copy untuk yang memerlukan source code-nya

Langkah pertama adalah buat database terlebih dahulu di phpmyadmin….nama terserah anda dan harus berakhiran .sql

— phpMyAdmin SQL Dump
— version 3.2.1
http://www.phpmyadmin.net

— Host: localhost
— Generation Time: Oct 10, 2011 at 05:33 AM
— Server version: 5.1.37
— PHP Version: 5.3.0

SET SQL_MODE=”NO_AUTO_VALUE_ON_ZERO”;

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;


— Database: `books`

— ——————————————————–


— Table structure for table `books`

CREATE TABLE IF NOT EXISTS `books` (
`BookID` int(11) NOT NULL AUTO_INCREMENT,
`Title` varchar(150) NOT NULL,
`Author` varchar(150) NOT NULL,
`PublisherName` varchar(150) NOT NULL,
`CopyrightYear` year(4) NOT NULL,
PRIMARY KEY (`BookID`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=9 ;


— Dumping data for table `books`

INSERT INTO `books` (`BookID`, `Title`, `Author`, `PublisherName`, `CopyrightYear`) VALUES
(1, ‘Algebra’, ‘Almel’, ‘Chun Sa’, 2007),
(2, ‘Algebra’, ‘Almel’, ‘Chun Sa’, 2007),
(3, ‘Algebra’, ‘Almel’, ‘Chun Sa’, 2007),
(4, ‘Algebra’, ‘Almel’, ‘Chun Sa’, 2007),
(5, ‘Algebra’, ‘Almel’, ‘Chun Sa’, 2007),
(6, ‘Algebra’, ‘Almel’, ‘Chun Sa’, 2007),
(7, ‘Algebra’, ‘Almel’, ‘Chun Sa’, 2007),
(8, ‘Algebra’, ‘Almel’, ‘Chun Sa’, 2007);

Lalu add file koneksi-nya db.php

<?php      $conn = mysql_connect(‘localhost’, ‘root’, ”);     if (!$conn)    {     die(‘Could not connect: ‘ . mysql_error());    }    mysql_select_db(“Books”, $conn);?>

buat file index.php

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”&gt;
<html xmlns=”http://www.w3.org/1999/xhtml”&gt;
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″ />
<title>Books</title>
</head>

<body>
<form method=”post”>
<table>

<tr>
<td>Title:</td>
<td><input type=”text” name=”title” /></td>
</tr>
<tr>
<td>Author</td>
<td><input type=”text” name=”author” /></td>
</tr>
<tr>
<td>Publisher Name</td>
<td><input type=”text” name=”name” /></td>
</tr>
<tr>
<td>Copyright Year</td>
<td><input type=”text” name=”copy” /></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input type=”submit” name=”submit” value=”add” /></td>
</tr>
</table>
<?php
if (isset($_POST[‘submit’]))
{
include ‘db.php’;

$title=$_POST[‘title’] ;
$author= $_POST[‘author’] ;
$name=$_POST[‘name’] ;
$copy=$_POST[‘copy’] ;

mysql_query(“INSERT INTO `books`(Title,Author,PublisherName,CopyrightYear)
VALUES (‘$title’,’$author’,’$name’,’$copy’)”);

}
?>
</form>
<table border=”1″>

<?php
include(“db.php”);

$result=mysql_query(“SELECT * FROM books”);

while($test = mysql_fetch_array($result))
{
$id = $test[‘BookID’];
echo “<tr align=’center’>”;
echo”<td><font color=’black’>” .$test[‘BookID’].”</font></td>”;
echo”<td><font color=’black’>” .$test[‘Title’].”</font></td>”;
echo”<td><font color=’black’>”. $test[‘Author’]. “</font></td>”;
echo”<td><font color=’black’>”. $test[‘PublisherName’]. “</font></td>”;
echo”<td><font color=’black’>”. $test[‘CopyrightYear’]. “</font></td>”;
echo”<td> <a href =’view.php?BookID=$id’>Edit</a>”;
echo”<td> <a href =’del.php?BookID=$id’><center>Delete</center></a>”;

echo “</tr>”;
}
mysql_close($conn);
?>
</table>

</body>
</html>

file view.php

<?php
require(“db.php”);
$id =$_REQUEST[‘BookID’];

$result = mysql_query(“SELECT * FROM books WHERE BookID  = ‘$id'”);
$test = mysql_fetch_array($result);
if (!$result)
{
die(“Error: Data not found..”);
}
$Title=$test[‘Title’] ;
$Author= $test[‘Author’] ;
$PublisherName=$test[‘PublisherName’] ;
$CopyrightYear=$test[‘CopyrightYear’] ;

if(isset($_POST[‘save’]))
{
$title_save = $_POST[‘title’];
$author_save = $_POST[‘author’];
$name_save = $_POST[‘name’];
$copy_save = $_POST[‘copy’];

mysql_query(“UPDATE books SET Title =’$title_save’, Author =’$author_save’,
PublisherName =’$name_save’,CopyrightYear =’$copy_save’ WHERE BookID = ‘$id'”)
or die(mysql_error());
echo “Saved!”;

header(“Location: index.php”);
}
mysql_close($conn);
?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”&gt;
<html xmlns=”http://www.w3.org/1999/xhtml”&gt;
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″ />
<title>Untitled Document</title>
</head>

<body>
<form method=”post”>
<table>
<tr>
<td>Title:</td>
<td><input type=”text” name=”title” value=”<?php echo $Title ?>”/></td>
</tr>
<tr>
<td>Author</td>
<td><input type=”text” name=”author” value=”<?php echo $Author ?>”/></td>
</tr>
<tr>
<td>Publisher Name</td>
<td><input type=”text” name=”name” value=”<?php echo $PublisherName ?>”/></td>
</tr>
<tr>
<td>Copyright Year</td>
<td><input type=”text” name=”copy” value=”<?php echo $CopyrightYear ?>”/></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input type=”submit” name=”save” value=”save” /></td>
</tr>
</table>

</body>
</html>

untuk delete data buat file del.php

<?php
include(“db.php”);

$id =$_REQUEST[‘BookID’];

// sending query
mysql_query(“DELETE FROM books WHERE BookID = ‘$id'”)
or die(mysql_error());

header(“Location: index.php”);
?>

Satu Tanggapan

  1. For example any individual has on average 316 connections on these social sites.
    Though it might often seem difficult to ask
    for recommendations, you must consider a give and take policy.

    Use any media mentions to your advantage in your summary sections as well as having a complete media kit
    on your profile using the Box.

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

%d blogger menyukai ini: