UPLOAD AND UPDATE IMAGE IS OPTIONAL
<?php
include('db.php');
$id=$_GET['id'];
$message='';
//$id = (isset($_GET['id']) ? $_GET['id'] : '');
if (isset($_POST['update']))
{
$fileName='';
$first=$_POST['first'];
$last=$_POST['last'];
$email=$_POST['email'];
$mobile=$_POST['mobile'];
$gender=$_POST['gender'];
$hobbies=$_POST['hobbies'];
$arr=implode(",",$hobbies);
$message=$_POST['Message'];
//$file = $_FILES['file'];
date_default_timezone_set('Asia/Kolkata');
$created_date= date('Y-m-d h:i:s');
$upload_path = 'uploads';
$new_image_name="";
$new_image_name=$_FILES["file"]["name"];
$allowed = array('png' ,'jpg','jpeg');
$ext = pathinfo($new_image_name, PATHINFO_EXTENSION);
move_uploaded_file($_FILES['file']['tmp_name'], "uploads/".$new_image_name);
$actual_link = "http://$_SERVER[HTTP_HOST]";
$actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$folder_path = dirname($actual_link);
if(!empty($new_image_name)){
$image= $folder_path.'/uploads'.'/'.$new_image_name;
//echo $fileName;
//die;
}
else{
$select= "SELECT image FROM forms WHERE id='".$id."'";
//echo $select;
//die;
$query=mysqli_query($connection,$select);
$num=mysqli_num_rows($query);
if($num > 0)
{
while($row=mysqli_fetch_array($query))
{
$image=$row['image'];
//echo $fileName;
//die;
}
}
}
$sql = "UPDATE forms SET first_name='$first',last_name='$last',email='$email',mobile='$mobile',gender='$gender',hobbies='$arr',message='$message',image='$image',created_date='$created_date' WHERE id='".$id."'";
$query=mysqli_query($connection,$sql);
if($query)
{
header('location:select.php');
exit;
}
}
?>
<form class=form action="" method="post" enctype="multipart/form-data">
<div class="mesage">
<?php if(!empty($message)) { echo $message; } ?>
</div>
<?php $select1= "SELECT * FROM forms WHERE id='".$id."'";
//echo $select1;
//die;
$result1 = mysqli_query($connection,$select1);
$num1=mysqli_num_rows($result1);
//echo $num1;
//die;
if($num1){
$row1 = mysqli_fetch_array($result1);
?>
<head><h2>Update From</h2></head>
<form class=form action="" method="post" enctype="multipart/form-data">
<p>First Name:<input type="text" name="first" placeholder="First Name" value= <?php echo $row1['first_name'];?>>
Last Name:<input type="text" name="last" placeholder="Last Name" value= <?php echo $row1['last_name'];?>></p>
<p>Email:<input type="email" name="email" placeholder="Email" value= <?php echo $row1['email'];?>>
Mobile:<input type="tele" name="mobile" placeholder="Mobile" value= <?php echo $row1['mobile'];?>></p>
<p>
Gender:
Male<input type="radio" name="gender" value="Male" <?php if($row1['gender']=="Male"){echo "checked";}?>/>
Female<input <?php if($row1['gender']=="Female"){echo "checked";}?> type="radio" name="gender" value="Female"/>
</p>
<p>Hobbies:
<?php
$chkbox=$row1['hobbies'];
$arr=explode(",",$chkbox);
//print_r($arr);
//die;
?>
Singing <input <?php if(in_array("Singing",$arr)){echo "checked";}?> type="checkbox" name="hobbies[]" value="Singing" />
Dancing <input <?php if(in_array("Dancing",$arr)){echo "checked";}?> type="checkbox" name="hobbies[]" value="Dancing" />
Art <input <?php if(in_array("Art",$arr)){echo "checked";}?> type="checkbox" name="hobbies[]" value="Art" />
cricket <input <?php if(in_array("Cricket",$arr)){echo "checked";}?> type="checkbox" name="hobbies[]" value="Cricket" /><br>
</p>
<p>
<textarea name="Message" rows="5" cols="40" placeholder="Enter Message">
<?php echo $row1['message'];?>
</textarea>
</p>
<p>
<td><img height=70px width=40px src="<?php echo $row1['image'];?>" />
<input type ="file" name="file"><br>
</td>
</p>
<p>
<input type="submit" name="update" value="Update" >
</p>
<?php } ?>
</form>
No comments:
Post a Comment