php example edit delete update

############ add.php #########

<?php
include('dbcon.php');

if (isset($_POST['submit'])){

$Title=$_POST['Title'];
$Author=$_POST['Author'];
$PublisherName=$_POST['PublisherName'];
$CopyrightYear=$_POST['CopyrightYear'];



mysql_query("insert into books (Title,Author,PublisherName,CopyrightYear)
            values('$Title','$Author','$PublisherName','$CopyrightYear')");
            header('location:index.php');
}
?>

########## delete.php #########

<?php
include('dbcon.php');
$id=$_GET['id'];

mysql_query("delete from books where BookID='$id'");
header('location:index.php');

?>

########### edit.php #########

<?php
include 'dbcon.php';

$id=$_GET['id'];



?>
<html>
<head>
<title>PHP PRACTICE</title>
</head>
<body>
<form method="post">
<table>
<?php $books_query=mysql_query("select * from books where BookID='$id'");
$books_rows=mysql_fetch_array($books_query);
?>
<tr><td>Title:</td><td><input type="text" name="Title" value="<?php echo $books_rows['Title'];  ?>"></td></tr>
<tr><td>Author:</td><td><input type="text" name="Author" value="<?php echo $books_rows['Author'];  ?>"></td></tr>
<tr><td>PublisherName:</td><td><input type="text" name="PublisherName" value="<?php echo $books_rows['PublisherName'];  ?>"></td></tr>
<tr><td>CopyrightYear:</td><td><input type="text" name="CopyrightYear" value="<?php echo $books_rows['CopyrightYear']; ?>" ></td></tr>
<tr><td></td><td><input type="submit" name="submit" value="save"></td></tr>
</table>
</form>
</body>
</html>
<?php

if (isset($_POST['submit'])){

$Title=$_POST['Title'];
$Author=$_POST['Author'];
$PublisherName=$_POST['PublisherName'];
$CopyrightYear=$_POST['CopyrightYear'];

mysql_query("update books set Title='$Title',Author='$Author',PublisherName='$PublisherName',CopyrightYear='$CopyrightYear' where BookID='$id'");
@header('location:index.php');

}
?>

############# index.php #############

<?php
include('dbcon.php');
?>
<html>
<head>
<title>PHP PRACTICE</title>
</head>
<body>
<form method="post" action="add.php">
<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>PublisherName:</td><td><input type="text" name="PublisherName"></td></tr>
<tr><td>CopyrightYear:</td><td><input type="text" name="CopyrightYear"></td></tr>
<tr><td></td><td><input type="submit" name="submit" value="add"></td></tr>
</table>
</form>

</br>
</br>
<table border="1">
<?php
$books_query=mysql_query("select * from books");
while($books_rows=mysql_fetch_array($books_query)){
?>
<tr>
<td><?php echo $books_rows['BookID'] ; ?></td>
<td><?php echo $books_rows['Title'] ; ?></td>
<td><?php echo $books_rows['Author'] ; ?></td>
<td><?php echo $books_rows['PublisherName'] ; ?></td>
<td><?php echo $books_rows['CopyrightYear'] ; ?></td>
<td><a href="delete.php<?php echo '?id='.$books_rows['BookID']; ?>">delete</a></td>
<td><a href="edit.php<?php echo '?id='.$books_rows['BookID']; ?>">Edit</a></td>
</tr>
<?php }?>
</table>
</body>
</html>

############## dbcon.php ########

<?php
mysql_select_db('books',mysql_connect('localhost','root',''));
?>

###################### and here dumpind  data for above example   ###############

-- phpMyAdmin SQL Dump
-- version 3.2.0.1
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Nov 09, 2014 at 06:52 AM
-- Server version: 5.1.36
-- PHP Version: 5.3.0

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";

--
-- 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=17 ;

--
-- Dumping data for table `books`
--

INSERT INTO `books` (`BookID`, `Title`, `Author`, `PublisherName`, `CopyrightYear`) VALUES
(12, 'af', 'asd', 'sda', 2005),
(15, 'php for beginner', 's kumar', 's chand', 2014),
(14, 'kamal', 'vikas', 'asdf', 2016);


#####################
#####################
for any query regarding for begginer drop me message on whatsapp  9026413903  or on nimbuzz  sabhajeetkumar1

Comments