Insert Update Delete Search (Radio, Checkbox, Imge) with Pagination in PHP

Create database

CREATE DATABASE IF NOT EXISTS `test` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci;

Create table

CREATE TABLE IF NOT EXISTS `tbl1` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(11) NOT NULL,
  `number` varchar(11) NOT NULL,
  `pass` varchar(11) NOT NULL,
  `cpass` varchar(11) NOT NULL,
  `city` varchar(11) NOT NULL,
  `gender` varchar(11) NOT NULL,
  `job` varchar(50) NOT NULL,
  `image` varchar(50) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=132 ;

Create index.php

<form name="f1" method="post" enctype="multipart/form-data">
  name
  <input type="text" name="name" id="name">
  <br>
  Number
  <input type="text" name="number" id="number">
  <br>
  password
  <input type="text" name="pass" id="pass">
  <br>
  conform pass
  <input type="text" name="cpass" id="cpass">
  <br>
  city
  <select name="city" id="city">
    <option></option>
    <option value="junagadh">junagaadh</option>
    <option value="keshod">Keshod</option>
  </select>
  <br>
  Gender
  <input type="radio" name="gender" value="male" id="gender" />
  Male
  <input type="radio" value="female" name="gender" id="gender" />
  Female<br>
  job
  <input type="checkbox" name="job[]" value="developer" id="developer" />
  Developer
  <input type="checkbox" name="job[]" value="designer" id="designer" />
  Designer<br>
  <input type="file" name="image" id="image" />
  <input type="submit" name="insert" value="insert">
  <br>
</form>
<?php

$cn = mysql_connect("localhost","root","");
$db = mysql_select_db("test",$cn);

if(isset($_POST['insert']))
{
	$name = $_POST["name"];
	$number = $_POST["number"];
	$pass = $_POST["pass"];
	$cpass = $_POST["cpass"];
	$city = $_POST["city"];
	@$gender = $_POST["gender"];
	@$job = implode(",",$_POST["job"]);
	
	$image = $_FILES['image']['name'];
	$define_name = "image_".microtime().".jpg";
	move_uploaded_file($_FILES['image']['tmp_name'],"upload/".$define_name);
	
	$q = "INSERT INTo `tbl1` (`name`, `number`, `pass`, `cpass`, `city`, `gender`, `job`, `image`)
		  VALUES ('".$name."', '".$number."', '".$pass."', '".$cpass."', '".$city."', '".$gender."', '".$job."', '".$define_name."')";

	$r = mysql_query($q);
}
echo '<a href="display.php">Display Records</a>';
?>

Create display.php


<form method="post" action="search_all.php">
  Search
  <input type="text" name="search_all" />
  <input type="submit" name="submit" value="submit" />
</form>
<?php
	$cn = mysql_connect("localhost","root","");
	$db = mysql_select_db("test",$cn);
	
	$q = "SELECT * FROM `tbl1`";
	$r = mysql_query($q);
	 
		$row2 = mysql_num_rows($r);
		$limit = 2;
		$page_devide = ceil($row2/$limit);
		if(!isset($_GET['page']))
		{
			header("location:display.php?page=1"); 
		}
		else
		{
			$page = $_GET['page'];
		}
			$start = ($page-1)*$limit;
			$q2 = "select * from tbl1 limit $start,$limit";
			$r2 = mysql_query($q2);
	?>
<table border="1">
  <tr>
    <td>name</td>
    <td>number</td>
    <td>pass</td>
    <td>cpass</td>
    <td>city</td>
    <td>gender</td>
    <td>job</td>
    <td>Image</td>
    <td>Edit</td>
    <td>Delete</td>
  </tr>
  <?php while($row = mysql_fetch_array($r2)) { ?>
  <tr>
    <td><?php echo $row['name']; ?></td>
    <td><?php echo $row['number']; ?></td>
    <td><?php echo $row['pass']; ?></td>
    <td><?php echo $row['cpass']; ?></td>
    <td><?php echo $row['city']; ?></td>
    <td><?php echo $row['gender']; ?></td>
    <td><?php echo $row['job']; ?></td>
    <td><img src="upload/<?php echo $row['image']; ?>" width="50" height="50"></td>
    <td><a href="edit.php?hid=<?php echo $row['id']; ?>">Edit</a></td>
    <td><a href="delete.php?hid=<?php echo $row['id']; ?>&image=<?php echo $row['image']; ?>">Delete</a></td>
  </tr>
  <?php } ?>
  <?php 
	
		for($i=1;$i<=$page_devide;$i++)
		{
			?>
  <a href="display.php?page=<?php echo $i; ?>"><?php echo $i;?></a>
  <?php	
		}
	?>
</table>

Create delete.php

<?php
$cn = mysql_connect("localhost","root","");
$db = mysql_select_db("test",$cn);
$id = $_REQUEST['hid'];
$image = $_REQUEST['image'];
$q="DELETE FROM tbl1 WHERE id=".$id; 
$r=mysql_query($q) or die(mysql_query());
unlink("upload/".$image);
header("location:display.php");
?>

Create edit.php

<?php

$cn = mysql_connect("localhost","root","");
$db = mysql_select_db("test",$cn);

if(isset($_REQUEST['hid']))
{
	$id = $_REQUEST['hid'];
	$q = "SELECT * FROM `tbl1` WHERE `id` = '".$id."'";
	$r = mysql_query($q);
}
	while($row = mysql_fetch_array($r))
	{
		$id = $row['id'];
		$name = $row['name'];
		$number = $row['number'];
		$pass = $row['pass'];
		$cpass = $row['cpass'];
		$city = $row['city'];
		$gender = $row['gender'];
		$job = explode(",",$row['job']);
		$image = $row['image']; 
	}
?>

<form name="f1" method="post" enctype="multipart/form-data">
  <input type="hidden" name="id" value="<?php echo $id; ?>">
  name
  <input type="text" name="name" id="name" value="<?php echo $name;  ?>">
  <br>
  Number
  <input type="text" name="number" id="number" value="<?php echo $number;  ?>">
  <br>
  password
  <input type="text" name="pass" id="pass" value="<?php echo $pass;  ?>">
  <br>
  conform pass
  <input type="text" name="cpass" id="cpass" value="<?php echo $cpass;  ?>">
  <br>
  city
  <select name="city" id="city">
    <option></option>
    <option value="junagadh" <?php if($city == "junagadh") { ?> selected="selected">
    <?php } ?>
    junagaadh</option>
    <option value="keshod" <?php if($city == "keshod"){ ?> selected="selected" <?php } ?>>Keshod</option>
  </select>
  <br>
  Gender
  <input type="radio" name="gender" value="male" id="gender" <?php if($gender == "male"){ ?> checked="checked" <?php } ?> />
  Male
  <input type="radio" value="female" name="gender" id="gender" <?php if($gender == "female") { ?> checked="checked" <?php } ?> />
  Female<br>
  job
  <input type="checkbox" name="job[]" value="developer" id="developer" <?php if(in_array("developer",$job)){ ?> checked="checked" <?php } ?> />
  Developer
  <input type="checkbox" name="job[]" value="designer" id="designer" <?php if(in_array("designer",$job)){ ?> checked="checked" <?php } ?> />
  Designer<br>
  <input type="file" name="image" id="image" />
  <img src="upload/<?php echo $image; ?>" width="50" height="50"><br>
  <input type="submit" name="edit" value="edit">
  <br>
  <input type="hidden" name="old_image" value="<?php echo $image; ?>" />
</form>
<?php

if(isset($_POST['edit']))
{
		$id = $_POST['id'];
		$name = $_POST['name'];
		$number = $_POST['number'];
		$pass = $_POST['pass'];
		$cpass = $_POST['cpass'];
		$city = $_POST['city'];
		@$gender = $_POST['gender'];
		@$job = implode(",",($_POST['job']));
		
		$image = $_FILES['image']['name'];
		$define_name = "image_".microtime().".jpg";
		
		$old_image = $_REQUEST['old_image'];
		unlink("upload/".$old_image);
		
		move_uploaded_file($_FILES['image']['tmp_name'],"upload/".$define_name);
		
		$q = "UPDATE `tbl1` SET `name` = '".$name."', `number` = '".$number."', `pass` = '".$pass."',   	
			 `cpass` = '".$cpass."', `city` = '".$city."', `gender` = '".$gender."', `job` = '".$job."',
			 `image` = '".$define_name."' WHERE `id` = '".$id."'";
			 
		
		$r = mysql_query($q)or die(mysql_query());

		header("location:display.php");
}

?>

Create search_all.php

<?php

$cn = mysql_connect("localhost","root","");
$db = mysql_select_db("test",$cn);

$q = "SELECT * 
FROM  `tbl1` 
WHERE  `name` LIKE  '%".$_POST['search_all']."%'
OR  `number` LIKE  '%".$_POST['search_all']."%'
OR  `pass` LIKE  '%".$_POST['search_all']."%'
OR  `cpass` LIKE  '%".$_POST['search_all']."%'
OR  `city` LIKE  '%".$_POST['search_all']."%'
OR  `gender` LIKE  '%".$_POST['search_all']."%'
OR  `job` LIKE  '%".$_POST['search_all']."%'
OR  `image` LIKE  '%".$_POST['search_all']."%'";

$r = mysql_query($q);
?>

<table border="1">
  <tr>
    <td>name</td>
    <td>number</td>
    <td>pass</td>
    <td>cpass</td>
    <td>city</td>
    <td>gender</td>
    <td>job</td>
    <td>Image</td>
    <td>Edit</td>
    <td>Delete</td>
  </tr>
  <?php while($row = mysql_fetch_array($r)) { ?>
  <tr>
    <td><?php echo $row['name']; ?></td>
    <td><?php echo $row['number']; ?></td>
    <td><?php echo $row['pass']; ?></td>
    <td><?php echo $row['cpass']; ?></td>
    <td><?php echo $row['city']; ?></td>
    <td><?php echo $row['gender']; ?></td>
    <td><?php echo $row['job']; ?></td>
    <td><img src="upload/<?php echo $row['image']; ?>" width="50" height="50"></td>
    <td><a href="edit.php?hid=<?php echo $row['id']; ?>">Edit</a></td>
    <td><a href="delete.php?hid=<?php echo $row['id']; ?>&image=<?php echo $row['image']; ?>">Delete</a></td>
  </tr>
  <?php } ?>
</table>

Advertisements

Tagged: , , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: