Wednesday 22 August 2018

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>

Saturday 18 August 2018

UPDATE

6. update.php

<?php
include('db.php');
$id=$_GET['id'];
//$id = (isset($_GET['id']) ? $_GET['id'] : '');
if (isset($_POST['update']))
{
//$arr=implode(",",$hobbies);
$first=$_POST['first'];
//echo $first;
$last=$_POST['last'];
//echo $last;
$email=$_POST['email'];
//echo $email;
$mobile=$_POST['mobile'];
//echo $mobile;
$gender=$_POST['gender'];
//echo $gender;
$hobbies=$_POST['hobbies'];
$arr=implode(",",$hobbies);
//echo $arr;
$message=$_POST['Message'];
$sql = "UPDATE forms SET first_name='$first',last_name='$last',email='$email',mobile='$mobile',gender='$gender',hobbies='$arr',message='$message' WHERE id='".$id."'";
//echo $sql;
//die;
$qeury=mysqli_query($connection,$sql);
if($qeury){
header('location:select.php');
exit;
}
else
{
echo 'not update';
}
}


$select= "SELECT * FROM forms WHERE id='".$id."'";
$result = mysqli_query($connection,$select);
while($row = mysqli_fetch_array($result)){


?>

<form action="" method="post" enctype="multipart/form-data">
<p>First Name:<input type="text" name="first" placeholder="First Name" value= <?php echo $row['first_name'];?>>
Last Name:<input type="text" name="last" placeholder="Last Name" value= <?php echo $row['last_name'];?>></p>
<p>Email:<input type="email" name="email" placeholder="Email" value= <?php echo $row['email'];?>>
Mobile:<input type="tele" name="mobile" placeholder="Mobile" value= <?php echo $row['mobile'];?>></p

<p>
Gender: 
Male<input type="radio" name="gender" value="Male" <?php if($row['gender']=="Male"){echo "checked";}?>/>

Female<input <?php if($row['gender']=="Female"){echo "checked";}?> type="radio" name="gender" value="Female"/>
</p>

<p>Hobbies:
<?php 
$chkbox=$row['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 $row['message'];?>
</textarea>

<input type="submit" name="update" value="Update" >
</p>
<?php } ?>
</form>


Friday 17 August 2018

FORM INSERT SELECT AND DELETE

1. Form.php  
<form action="insert.php" method="post" enctype="multipart/form-data">
<p>First Name:<input type="text" name="first" placeholder="First Name">
Last Name:<input type="text" name="last" placeholder="Last Name"></p>
<p>Email:<input type="email" name="email" placeholder="Email">
Mobile:<input type="tele" name="mobile" placeholder="Mobile"></p>
<p>
Gender:
Male<input type="radio" name="gender" value="Male" >
Female<input type="radio" name="gender" value="Female" >
</p>
<p>Hobbies:
Singing <input type="checkbox" name="hobbies[]" value="Singing">
Dancing <input type="checkbox" name="hobbies[]" value="Dancing">
Art <input type="checkbox" name="hobbies[]" value="Art">
cricket <input type="checkbox" name="hobbies[]" value="Cricket"><br>
</p>
<p>
<textarea name="Message" rows="5" cols="40" placeholder="Enter Message">
</textarea>
<!__<input type ="file" name="upload"><!__<br>
<input type="submit" name="submit" value="Submit" >
</p>
<br><a href="select.php" type="submit" name="logout">Show</a>
</form>
2. db.php
<?php
$servername="localhost";
$username="root";
$password="123456789";
$db_name="database_name";//your database Name
$connection=mysqli_connect($servername,$username,$password,$db_name);
// Check connection
if (!$connection) {
    die("Connection failed: " . mysqli_connect_error());
}
//echo "Connected successfully";
?>
3. insert.php
<?php
require "db.php";//for database connection
if (isset($_POST['submit'])){ $first=$_POST['first'];
$last=$_POST['last'];
$email=$_POST['email'];
$mobile=$_POST['mobile'];
$gender=$_POST['gender'];
$hobbies=$_POST['hobbies'];
$arr=implode(",",$hobbies);
$message=$_POST['Message'];
  //******************************INSERT QUERY START HERE******************************
  $query="INSERT INTO forms(first_name,last_name,email,mobile,gender,hobbies,message)
  VALUES('$first','$last','$email','$mobile','$gender','$arr','$message')";
  $insert=mysqli_query($connection,$query);
  if (!$insert==0){
  echo "Record inserted Successfully";
  }
  else
  {
  echo "validations Failed";
  }
}
?>
4.  select.php
<table align="center" >
<table style="width:900px;"border="5" >
<tr><th colspan="9"><h1>Records</h1></th></tr>
<tr align="center">
<td><h3>ID</h3></td>
<td><h3>FIRST NAME</h3></td>
<td><h3>LAST NAME</h3></td>
<td><h3>EMAIL</h3></td>
<td><h3>MOBILE</h3></td>
<td><h3>GENDER</h3></td>
<td><h3>HOBBIES</h3></td>
<td><h3>MESSAGE</h3></td>
<td><h3>ACTION</h3></td>
</tr>
<?php
//require "db.php";
//if(isset($_POST['submit'])){
include('db.php');
$select= "SELECT * FROM forms";
$result = mysqli_query($connection,$select);
while($row = mysqli_fetch_array($result)){
?> <tr>
<td><?php echo $row['id'];?></td>
<td><?php echo $row['first_name'];?></td>
<td><?php echo $row['last_name'];?></td>
<td><?php echo $row['email'];?></td>
<td><?php echo $row['mobile'];?></td>
<td><?php echo $row['gender'];?></td>
<td><?php echo $row['hobbies'];?></td>
<td><?php echo $row['message'];?></td>
<td><a href='delete.php?id=<?php echo $row['id']; ?>'>Delete</a></td>
</tr> <?php } ?>
</table>
5. delete.php
<table align="center" >
<table style="width:900px;"border="5" >
<tr><th colspan="9"><h1>Records</h1></th></tr>
<tr align="center">
<td><h3>ID</h3></td>
<td><h3>FIRST NAME</h3></td>
<td><h3>LAST NAME</h3></td>
<td><h3>EMAIL</h3></td>
<td><h3>MOBILE</h3></td>
<td><h3>GENDER</h3></td>
<td><h3>HOBBIES</h3></td>
<td><h3>MESSAGE</h3></td>
<td><h3>ACTION</h3></td>
</tr>
<?php
//require "db.php";
//if(isset($_POST['submit'])){
include('db.php');
$select= "SELECT * FROM forms";
$result = mysqli_query($connection,$select);
while($row = mysqli_fetch_array($result)){
?> <tr>
<td><?php echo $row['id'];?></td>
<td><?php echo $row['first_name'];?></td>
<td><?php echo $row['last_name'];?></td>
<td><?php echo $row['email'];?></td>
<td><?php echo $row['mobile'];?></td>
<td><?php echo $row['gender'];?></td>
<td><?php echo $row['hobbies'];?></td>
<td><?php echo $row['message'];?></td>
<td><a href='delete.php?id=<?php echo $row['id']; ?>'>Delete</a></td>
</tr> <?php } ?>
</table>


Thursday 16 August 2018

Error: Fatal error: Cannot redeclare exp() in PHP

Error: Fatal error: Cannot redeclare exp() in 

The message says Cannot redeclare and that means that the function already exists.
exp() is a built-in function in php.


You can do it like this
function my_exp($value1,$value2) {
  return $value1 ** $value2;
}
echo my_exp(2,8);//prints 256;
Or you can use the built-in function pow()
echo pow(2,8);//prints 256