Monthly Archives: November 2014

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’);

Advertisements

Insert Update Delete Search in php

index.php

<form name=”f1″ method=”post” action=”query.php”>
<table width=”305″ height=”198″ border=”1″>
<tr>
<td width=”112″>User Id</td>
<td width=”177″><input type=”text” id=”uid” name=”uid”></td>
</tr>
<tr>
<td width=”112″>Username</td>
<td width=”177″><input type=”text” id=”uname” name=”uname”></td>
</tr>
<tr>
<td>Password</td>
<td><input type=”password” id=”pass” name=”pass”></td>
</tr>
<td height=”45″ colspan=”3″ align=”center”><input type=”submit” value=”Insert” name=”insert”>
<input type=”submit” value=”Update” name=”update”>
<input type=”submit” value=”Delete” name=”delete”>
<input type=”submit” value=”Display All” name=”displayall”>
<a href=”search.php”>Search</a></td>
</tr>
</table>
</form>

Query.php

<?php
$cn=mysql_connect(“localhost:3306″,”root”,””);
mysql_select_db(“iuds”,$cn);

// ________________________I N S E R T__________________________________________

if(isset($_POST[“insert”]))
{
$uid=$_POST[‘uid’];
$uname=$_POST[‘uname’];
$pass=$_POST[‘pass’];

$q=”INSERT INTO udata values(‘$uid’,’$uname’,’$pass’)”;

mysql_query($q);
echo “INSERTed successfully”;

}

// _______________________________U P D A T E________________________________

if(isset($_POST[“update”]))
{
$uid=$_POST[“uid”];
$uname=$_POST[“uname”];
$pass=$_POST[“pass”];

$q=”UPDATE udata SET uname=’$uname’,pass=’$pass’ WHERE uid=’$uid'”;
mysql_query($q);
echo “UPDATEd successfully”;
}
// ________________________D E L A T E__________________________

if(isset($_POST[“delete”]))
{
$uid=$_POST[‘uid’];
$uname=$_POST[‘uname’];
$pass=$_POST[‘pass’];

$q=”DELETE FROM udata WHERE uid=”.$uid;
mysql_query($q);
echo “DELETEd successfully”;
}
// ___________________D I S P L A Y_________________________________

if(isset($_POST[‘displayall’]))
{
$uid=$_POST[‘uid’];
$uname=$_POST[‘uname’];
$pass=$_POST[‘pass’];

$q=”select * FROM udata”;
$r=mysql_query($q);

echo “<table border=1>”;
echo “<tr><td>User Id</td><td>User name</td><td>Password</td></tr>”;
while($row=mysql_fetch_array($r))
{
echo “<tr>
<td>$row[0]</td>
<td>$row[1]</td>
<td>$row[2]</td>
<td><a href=index.php?uid=$uid[0]>back</a></td>
</tr>”;
}
echo “</table>”;
}

// ____________________________S E A R C H D I S P L A Y____________________

$uid=””;
$uname=””;
$pass=””;
if(isset($_GET[‘uid’]))
{
$r=mysql_query(“select * from udata where uid=”.$_GET[‘uid’]);
while($row=mysql_fetch_array($r))
{
$no=$row[0];
$name=$row[1];
$marks1=$row[2];
}
}

?>

Search.php

<form method=”post”>
<table>
<tr>
<td>Search User By Id</td>
<td><input type=”text” name=”uid”></td>
<td><input type=”submit” value=”search” name=”search”></td>
</tr>
</table>
</form>
<?php

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

if(isset($_POST[“search”]))
{
$uid=$_POST[‘uid’];
$r=mysql_query(“select * from udata where uid=’$uid'”);
echo $r;

echo “<table border=1>”;
echo “<tr><td>User Id</td><td>User name</td><td>Password</td></tr>”;
while($row=mysql_fetch_array($r))
{
echo”<tr>
<td>$row[0]</td>
<td>$row[1]</td>
<td>$row[2]</td>
<td><a href=search.php?uid=$row[0]>select</a></td>
</tr>”;
}
}
echo “</table>”;
?>

All types of form validation



<form name="f1" onsubmit="return vali()">
  name
  <input type="text" name="fname" id="fname">
  <br>
  Email
  <input type="text" name="email" id="email">
  <br>
  Number
  <input type="text" name="number" id="number">
  <br>
  Strong pass
  <input type="text" name="strongpass" id="strongpass">
  <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>
  </select>
  <br>
  Gender
  <input type="radio" name="gender" id="gender" />
  Male
  <input type="radio" name="gender" id="gender" />
  Female<br>
  Post
  <input type="checkbox" name="developer" id="developer" />
  Developer
  <input type="checkbox" name="designer" id="designer" />
  Designer<br>
  <input type="submit" name="submit">
  <script language="javascript">
	
	function vali()
	{
		var x = document.forms["f1"]["fname"].value;
		if(x == null || x == "")
		{
			alert("name");
			fname.focus();
			return false;
		}
		
		var x = document.forms["f1"]["email"].value;
		if(!x.match(/^[a-zA-Z0-9._]+@[a-zA-Z0-9]+\.[a-zA-Z]{2,4}$/))
		{
			alert("email");
			email.focus();
			return false;
		}
		var x = document.forms["f1"]["number"].value;
		if(!x.match(/^[0-9]{10}$/))
		{
			alert("number");
			number.focus();
			return false;
		}
		var x = document.forms["f1"]["strongpass"].value;
		if(!x.match(/^[0-9A-Za-z._-]{6,10}$/))
		{
			alert("pass musst be 10 digit");
			strongpass.focus();
			return false;
		}
		var x = document.forms["f1"]["pass"].value;
		if(x == null || x == "")
		{
			alert("password");
			pass.focus();
			return false;
		}
		var x = document.forms["f1"]["pass"].value;
		var y = document.forms["f1"]["cpass"].value;
		if(x != y) // (!(x==y)) also use this
		{
			alert("pass not match");
			cpass.focus();
			return false;
		}
		var x = document.forms["f1"]["city"].value;
		if(x == null || x == "")
		{
			alert("city");
			city.focus();
			return false;
		}
		if(document.f1.gender[0].checked == false && gender[1].checked == false)
		{
			alert("gensder");
			gender[0].focus();
			return false
		}
		if(document.f1.developer.checked == false && designer.checked == false)
		{
			alert("job");
			developer.focus();
			return false;
		}
		
		alert("Thank you for submitting data");
		window.location.href="validation.php";
		
		return true;
	}
	
</script>
</form>



Image optimizer

https://compressor.io/

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

https://tinypng.com/

Chane placeholder, lable, button name using jquery

Create new file like custom.js

jQuery(document).ready(function(e) {
jQuery(“#reply-title”).text(“New text”); // change html text
jQuery(“.comment-form-author #author”).attr(“placeholder”,”Name”); // change placeholder name
jQuery(“.form-submit #submit”).attr(“value”,”Send”); // change button value
});

Add custom media field

Paste in functions.php

Field displaying in Edit media

/* custom media fild */
add_filter( ‘attachment_fields_to_edit’,  ‘attachment_fields_edit_hfp’, null, 2 );
add_filter( ‘attachment_fields_to_save’,  ‘attachment_fields_save_hfp’, null, 2 );
add_action( ‘admin_init’, ‘hfp_columns’);

function attachment_fields_edit_hfp( $form_fields, $post ) {

$hfp_prevention = (bool) get_post_meta($post->ID, ‘_myhfp_prevention’, true);
$checked = ($hfp_prevention) ? ‘checked’ : ”;
$form_fields[‘hfp’] = array(
‘label’ => ”,
‘input’ => ‘html’,
‘html’ => “<label><strong>Show for visitors</strong></label><br/><input type=’checkbox’ {$checked} name=’attachments[{$post->ID}][hfp_prevention]’ id=’attachments[{$post->ID}][hfp_prevention]’ />Show for visitors?”,
‘value’ => $hfp_prevention,
‘helps’ => ”
);
return $form_fields;

}
function attachment_fields_save_hfp($post, $attachment) {

if (isset($attachment[‘hfp_prevention’]))
$hfp_prevention = ($attachment[‘hfp_prevention’] == ‘on’) ? 1 : 0;
else $hfp_prevention=0;
update_post_meta($post[‘ID’], ‘_myhfp_prevention’, $hfp_prevention);

return $post;

}

function hfp_column( $cols ) {

$cols[“hfp”] = “Show for visitors”;
return $cols;

}

function hfp_column_value( $column_name, $id ) {

$meta = wp_get_attachment_metadata($id);
$hfp=get_post_meta($id, ‘_myhfp_prevention’, true);
if ($hfp==1) echo “Yes”;

}

function hfp_column_sortable( $cols ) {

$cols[“hfp”] = “hfp”;
return $cols;

}
function hfp_columns() {

add_filter( ‘manage_media_columns’, ‘hfp_column’ );
add_action( ‘manage_media_custom_column’, ‘hfp_column_value’, 10, 2 );
add_filter( ‘manage_upload_sortable_columns’, ‘hfp_column_sortable’ );

}

%d bloggers like this: