Tag Archives: image

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>

Add Custom Column for Posts and Custom Post Types in backend

functions.php

Default for all avalable post types.


add_image_size('featured_preview', 55, 55, true);
// GET FEATURED IMAGE
function ST4_get_featured_image($post_ID) {
$post_thumbnail_id = get_post_thumbnail_id($post_ID);
if ($post_thumbnail_id) {
$post_thumbnail_img = wp_get_attachment_image_src($post_thumbnail_id, 'featured_preview');
return $post_thumbnail_img[0];
}
}
// ADD NEW COLUMN
function ST4_columns_head($defaults) {
$defaults['featured_image'] = 'Featured Image';
return $defaults;
}

// SHOW THE FEATURED IMAGE
function ST4_columns_content($column_name, $post_ID) {
if ($column_name == 'featured_image') {
$post_featured_image = ST4_get_featured_image($post_ID);
if ($post_featured_image) {
echo '<img src="' . $post_featured_image . '" />';
}
}
}
add_filter('manage_posts_columns', 'ST4_columns_head');
add_action('manage_posts_custom_column', 'ST4_columns_content', 10, 2);

If you want Create Custom Column for your  requirement so use this code (Use for Specific Post).


add_filter('manage_edit-portfolios_columns', 'add_new_portfolios_columns');
function add_new_portfolios_columns() {
$new_columns['cb'] = '<input type="checkbox" />';
$new_columns['title'] = _x('Name', 'column name');
$new_columns['featured_image'] = __('Images');
$new_columns['taxonomy-portfolio-categories'] = __('Portfolio Categories');
$new_columns['taxonomy-portfolio-tag'] = __('Portfolio Tag');
$new_columns['date'] = _x('Date', 'column name');
return $new_columns;
}
/*Featured Images*/
// GET FEATURED IMAGE
function portfolio_get_featured_image($post_ID) {
$post_thumbnail_id = get_post_thumbnail_id($post_ID);
if ($post_thumbnail_id) {
$post_thumbnail_img = wp_get_attachment_image_src($post_thumbnail_id, 'featured_preview');
return $post_thumbnail_img[0];
}
}
// SHOW THE FEATURED IMAGE
function portfolio_columns_content($column_name, $post_ID) {
if ($column_name == 'featured_image') {
$post_featured_image = portfolio_get_featured_image($post_ID);
if ($post_featured_image) {
echo '<img src="' . $post_featured_image . '" />';
}
}
}
// About WordPress Hooks
add_action('manage_posts_custom_column', 'portfolio_columns_content', 10, 2);

Image auto crop with defined size

Functions.php

// Crop image for home page posts

add_image_size('legal-thumbnail-image', 420, 247, true);

Display


<?php $legal_image = wp_get_attachment_image_src(get_post_thumbnail_id(get_the_ID()), 'legal-thumbnail-image', true); ?>
<a href="<?php the_permalink(); ?>"><img class="img-responsive" src="<?php echo esc_url($legal_image[0]); ?>" width="<?php echo $legal_image[1]; ?>" height="<?php echo $legal_image[2]; ?>" alt="<?php the_title(); ?>" /></a>

Upload image in php

Image Insert, Update, Delete with allow image extension and save image with rename using time();

Image saved in upload folder and when you delete record, image is also delete from upload directory.

form.php

<form method=”post” enctype=”multipart/form-data”>
id
<input type=”text” name=”id”>
name
<input type=”text” name=”name”>
image
<input type=”file” name=”image”>
<br>
<input type=”submit” name=”submit” value=”submit”>
</form>
<?php

$cn=mysql_connect(“localhost”,”root”,””);
$db=mysql_select_db(“image”,$cn);

if(isset($_POST[‘submit’]))
{
$id = $_POST[‘id’];
$name = $_POST[‘name’];

$define_name = “profile_”.time();

$image = $define_name.”.jpg”;

$allowedExts = array(“gif”, “jpeg”, “jpg”, “png”);
if (($_FILES[“image”][“type”] == “image/gif”)
|| ($_FILES[“image”][“type”] == “image/jpeg”)
|| ($_FILES[“image”][“type”] == “image/png”)
|| ($_FILES[“image”][“type”] == “image/pjpeg”))

{
move_uploaded_file($_FILES[‘image’][‘tmp_name’],”upload/”.$define_name.”.jpg”);
echo “inserted”;
$q=’INSERT INTO `img`(`id`,`name`,`image`) VALUES (“‘.$id.'”,”‘.$name.'”,”‘.$image.'”)’;
$r=mysql_query($q);
}
else
{
echo ‘alert(“wrong file extension!”)’;
}
}
include “display.php”;
?>

display.php

<?php
$cn=mysql_connect(“localhost”,”root”,””);
$db=mysql_select_db(“image”,$cn);
$q=’SELECT * FROM `img`’;
$r=mysql_query($q);
?>
<table border=”1″>
<tr>
<td>id</td>
<td>name</td>
<td>image</td>
</tr>
<?php
while($row= mysql_fetch_array($r))
{ ?>
<tr>
<td><?php echo $row[‘id’]; ?></td>
<td><?php echo $row[‘name’]; ?></td>
<td><img src=”upload/<?php echo $row[‘image’]; ?>” width=50 height=50></td>
<td><a href=”edit.php?editID=<?php echo $row[‘id’]; ?>”>edit</a></td>
<td><a href=”delete.php?deleteID=<?php echo $row[‘id’]; ?>&image=<?php echo $row[‘image’]; ?>”>delete</a></td>
</tr>
<?php
}
?>
</table>

editt.php

<?php
$cn=mysql_connect(“localhost”,”root”,””);
$db=mysql_select_db(“image”,$cn);
if(isset($_REQUEST[‘editID’]))
{
$id = $_REQUEST[‘editID’];
$q = mysql_query(“SELECT * FROM img WHERE id = ‘$id'”)or die(mysql_error());
$r = mysql_fetch_array($q);
}

?>

<form method=”post” enctype=”multipart/form-data”>
id
<input type=”text” name=”id” value=”<?php echo $r[‘id’]; ?>”>
name
<input type=”text” name=”name” value=”<?php echo $r[‘name’]; ?>”>
image
<input type=”file” name=”image” value=”<?php echo $r[‘image’]; ?>”>
<img src=”upload/<?php echo $r[‘image’]; ?>” width=50 height=50> <br>
<a href=”form.php”>display</a>
<input type=”submit” name=”update” value=”update”>
<input type=”hidden” value=”<?php echo $r[‘image’]; ?>” name=”himage” />
</form>
<?php
if(isset($_POST[“update”]))
{
$id=$_POST[‘id’];
$name=$_POST[‘name’];
$image=$_FILES[‘image’][‘name’];
$oldimage = $_REQUEST[‘himage’];
unlink(‘upload/’.$oldimage);
move_uploaded_file($_FILES[‘image’][‘tmp_name’],”upload/”.$_FILES[‘image’][‘name’]);
$q=’UPDATE `img` SET `name`=”‘.$name.'”, `image`=”‘.$image.'” WHERE `id`=”‘.$id.'” ‘;
$r=mysql_query($q) or die (mysql_error());
echo “updated”;
header(“location:form.php”);
}
?>

delete.php

<?php

$cn=mysql_connect(“localhost”,”root”,””);
$db=mysql_select_db(“image”,$cn);

if(isset($_REQUEST[‘deleteID’]))
{
$deleteID=$_REQUEST[‘deleteID’];
$image=$_REQUEST[‘image’];

$q=’DELETE FROM `img` WHERE `id` = “‘.$deleteID.'”‘;
$r=mysql_query($q) or die (mysql_error());

unlink(“upload/”.$image);

echo “deleted”;
header(‘location:form.php’);
}
?>

database.sql

— Database: `image`

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


— Table structure for table `img`

CREATE TABLE IF NOT EXISTS `img` (
`id` int(50) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`image` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=110 ;


— Dumping data for table `img`

INSERT INTO `img` (`id`, `name`, `image`) VALUES
(24, ‘sss’, ‘1.jpg’);

Image optimizer

https://compressor.io/

http://tools.dynamicdrive.com/imageoptimizer/index.php

https://tinypng.com/

Add Featured image option in backend

Past in functions.php

add_theme_support( ‘post-thumbnails’ );

%d bloggers like this: