PHP Crud Tutorials

  • Step1:go to this link and download 
https://getbootstrap.com/docs/4.1/getting-started/download/ 
  • Step2:Firet you create2 database table 1table name(customers) and second table name is(city)
  • Step3:dbconnection.php
 <?php

$connect = mysql_connect ("localhost","root","root") or die (mysql_error());
$mydb = mysql_select_db ("saad") or die (mysql_error());

?>
  • Step4:create.php
<?php
require 'dbconnect.php';
?>
<!DOCTYPE html>
<html>
<head>
    <title>Create</title>
    <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
    <script type="js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="span10 offset1">
    <div class="row">
    <h3>Create User</h3>
    </div>

    <form class="form-horizontal" action="savedata.php" method="post" enctype="multipart/form-data">
        <div class="control-group">
        <label class="control-label"><b>Name</b></label>
        <div class="controls">
            <input type="text" name="name" placeholder="Name" /required>
        </div>
        </div>

        <div class="control-group">
        <label class="control-label"><b>Email</b></label>
        <div class="controls">
            <input type="email" name="email" placeholder="Email*" /required>
            </div>
        </div>

        <div class="control-group">
        <label class="control-label"><b>Mobile</b></label>
        <div class="controls">
            <input type="text" name="mobile" placeholder="Mobile*" /required>
            </div>
        </div>

        <div class="control-group">
        <label class="control-label"><b>Gender</b></label>
        <div class="controls">
            <input type="radio" name="gender" value="Male" />Male
            <br>
            <input type="radio" name="gender" value="Female"/>Female
            </div>
        </div>

 <div class="control-group">
 <label class="control-label"><b>City</b></label>
 <div class="controls">
 <select name="city">
  <?php
$sql="SELECT * FROM cities";
$run=mysql_query($sql);
while ($row=mysql_fetch_array($run)) {
?>

 <option> <?php echo $row['city_name'];?> </option>

<?php
}

?>
 </select>
</div>
</div>

      <div class="control-group">
          <label class="control-label"><b>Date of brth</b></label>
          <div class="controls">
          <input size="10" type="Date" name="age" placeholder="yyyy-mm-dd" value="2017-09-07">
          </div>
      </div>

        <tr>
    <div cspan10 lass="control-group">
   
    <td><label class="control-label"> Profile </label></td>
     <td>`
    <input class="input-group" type="file" name="uploadedimage" accept="image/*">
   
     </td>
      </tr>

      <div class="form-actions">
          <button type="submit"  name="save" class="btn btn-success">Save</button>
          <a href="index.php" class="btn">back</a>
      </div>

    </form>
    </div>

</div>

</body>
</html>
  • Step5:savedata.php
  <?php
include("dbconnect.php");


$name   = mysql_real_escape_string($_POST['name']);
$email  = mysql_real_escape_string($_POST['email']);
$mobile = mysql_real_escape_string($_POST['mobile']);
$gender = mysql_real_escape_string($_POST['gender']);
$city   = mysql_real_escape_string($_POST['city']);

    function GetImageExtension($imagetype)
     {
       if(empty($imagetype)) return false;
       switch($imagetype)
       {
           case 'image/bmp': return '.bmp';
           case 'image/gif': return '.gif';
           case 'image/jpeg': return '.jpg';
           case 'image/png': return '.png';
           default: return false;
       }
     }
    
if (!empty($_FILES["uploadedimage"]["name"])) {

  $file_name=$_FILES["uploadedimage"]["name"];
  $temp_name=$_FILES["uploadedimage"]["tmp_name"];
  $imgtype  =$_FILES["uploadedimage"]["type"];
  $ext= GetImageExtension($imgtype);
  $imagename=date("d-m-Y")."-".time().$ext;
  $target_path = "image/".$imagename;
  $dateOfBirth = $_POST["age"];
  $today = date("Y-m-d");
  $diff = date_diff(date_create($dateOfBirth), date_create($today));
 $age = $diff->format('%y');


if(move_uploaded_file($temp_name, $target_path)) {
$query= "INSERT INTO customers(name,email,mobile,gender,city,age,image,submission_date) VALUES('".$name."','".$email."','".$mobile."','".$gender."','".$city."','".$age."','".$target_path."',  '".date("Y-m-d")."')";
$run=mysql_query($query) or die(mysql_error());


}
}

?>

<script type="text/javascript">
window.location = "index.php";
</script>

  • Step6:index.php
 <!DOCTYPE html>
<html>
<head>
    <title>Crud</title>
    <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
    <script type="js/bootstrap.min.js"></script>
</head>
<body>
<div class="container-fluid">
    <div class="row">
        <h1>CRUD GRID</h1>
    </div>
    <div class="row">
  <div class="col-md-6">     
    <a style="" href="create.php" class="btn btn-success">ADD USERS</a>
</div>
<div class="col-md-6">
<form class="form-horizontal" action="index.php" method="GET" align='right' enctype="multipart/form-data">

    <input type="text" name="search" placeholder="Search">
    <input title="search" class="btn btn-warning" type="submit" name="sub" value="Search">
   

</form>
</div>

<?php

include 'dbconnect.php';
if (isset($_GET['sub'])) {
    $search=$_GET['search'];

    $query = "SELECT * FROM customers where id='$search' OR name='$search'";

    $run = mysql_query($query) or die(mysql_error());
    while ($row=mysql_fetch_array($run)) {

        $id = $row['id'];
        $name = $row['name'];
        $email= $row['email'];
        $mobile= $row['mobile'];
        $gender=$row['gender'];
        $submission_date= $row['submission_date'];
        $city = $row['city']; 
        $age = $row['age'];
        $image= $row['image'];

    }
?>

<table class="table- table-striped table-bordered" width="950">
<thead>
    <tr>

         <th> NO              </th>
         <th> Name            </th>
         <th> Email Address   </th>
         <th> Mobile          </th>
         <th> Gender          </th>
         <th> Submission date </th>
         <th> City            </th>
         <th> Age             </th>
         <th> Image           </th>
  
       
    </tr>
</thead>
<tbody>
    <tr>

        <td align="center"> <?php echo $id; ?>    </td>
        <td align="center"> <?php echo $name; ?>  </td>
        <td align="center"> <?php echo $email; ?> </td>
        <td align="center"> <?php echo $mobile; ?></td>
        <td align="center"> <?php echo $gender; ?></td>
        <td align="center"> <?php echo $submission_date; ?></td>
        <td align="center"> <?php echo $city; ?> </td>
        <td align="center"> <?php echo $age; ?> </td>
        <td align="center"> <?php  echo  '<img style="width:80px; height:80px;" src="'.$image.'" />';?></td>
    </tr>
    </tbody>
    <tr>
        <td align="center" ><a class="btn btn-info" align="center" href="index.php">Ok </a></td>
    </tr>

    
</table>
<hr>

<?php } ?>
    <table class="table table-striped table-bordered">

<thead>
<tr class="btn-info">
    <th> No              </th>
    <th> Name            </th>
    <th> Email Address   </th>
    <th> Mobile          </th>
    <th> Gender          </th>
    <th> City            </th>
    <th> Age             </th>
    <th> Image           </th>
    <th> Submission Date </th>
   
   
    <th> Read        </th>
    <th> Edit        </th>
    <th> Delete      </th>
</tr>
</thead>


<tbody>
   
    <?php
      include 'dbconnect.php';
  
      $sql = "SELECT * FROM customers  ORDER BY id DESC";
      $run =mysql_query($sql);
      while($row=mysql_fetch_array($run)){
     ?>
      <tr>
      <td> <?=$row['id'];?>     </td>
      <td> <?=$row['name'];?>   </td>
      <td> <?=$row['email'];?>  </td>
      <td> <?=$row['mobile'];?> </td>
      <td> <?=$row['gender'];?> </td>
      <td> <?=$row['city'];?>   </td>
      <td> <?=$row['age'];?>    </td>
      <td><img style="width:50px;" src="<?=$row['image'];?>" /></td>
      <td> <?=$row['submission_date'];?> </td>


      <td>
      <a class="btn" href="read.php?id=<?=$row['id'];?>">Read</a>
      </td>
      <td>
      <a class="btn btn-info" href="update.php?id=<?=$row['id'];?>">Edit</a>
      </td>
      <td>
      <a class="btn btn-danger" href="delete.php?id=<?=$row['id'];?>">Delete</a>
      </td>
      </tr>
    
     <?php
       }        
    ?>               
</tbody>

</table>

<div class="container-fluid">
<div class="row">
    <a href="/saad/city/" class="btn btn-info">ALL CITY</a>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
  • Step7:update.php
<?php
   include 'dbconnect.php';
  
   $id=$_GET['id'];
  
   $query="SELECT * FROM customers Where id='$id'";
   $run=mysql_query($query);
   $check=mysql_fetch_array($run);

if(isset($_POST['done'])){
$name   = mysql_real_escape_string($_POST['name']);
$email  = mysql_real_escape_string($_POST['email']);
$mobile = mysql_real_escape_string($_POST['mobile']);
$gender = mysql_real_escape_string($_POST['gender']);
$city   = mysql_real_escape_string($_POST['city']);
$age    = mysql_real_escape_string($_POST['age']);




if(empty($_FILES["uploadedimage"]["name"])) {

  $query=mysql_query ("UPDATE customers SET name='$name',email='$email',mobile='$mobile',gender='$gender',city='$city',age='$age' WHERE id='$id'") or die(mysql_error());
 }
 header("location: index.php");
}


function GetImageExtension($imagetype)
  {
if(empty($imagetype)) return false;
switch($imagetype)
{
  case 'image/bmp': return '.bmp';
  case 'image/gif': return '.gif';
  case 'image/jpeg': return '.jpg';
  case 'image/png': return '.png';
  default: return false;
}
  }
  
if (!empty($_FILES["uploadedimage"]["name"])) {

  $file_name=$_FILES["uploadedimage"]["name"];
  $temp_name=$_FILES["uploadedimage"]["tmp_name"];
  $imgtype  =$_FILES["uploadedimage"]["type"];
  $ext= GetImageExtension($imgtype);
  $imagename=date("d-m-Y")."-".time().$ext;
  $target_path = "image/".$imagename;


 
if(move_uploaded_file($temp_name, $target_path)) {

 $query_upload=mysql_query ("UPDATE customers SET name='$name',email='$email',mobile='$mobile',gender='$gender' ,   city='$city', age='$age', image='$target_path' WHERE id='$id'") or die(mysql_error());


  mysql_query($query_upload) or die("error in $query_upload == ----> ".mysql_error());

 
}else{

   exit("Error While uploading image on the server");

}

}

 
?>

<!DOCTYPE html>
<html>
<head>
    <title>Edit</title>
    <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
    <script type="js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="span10 offset1">
<div class="row">
<h3>Edit Customer</h3>
</div>

<form class="form-horizontal" action="update.php?id=<?php echo $id ?>" method="post" enctype="multipart/form-data">
    <div class="control-group">
    <label class="control-label"><b>Name</b></label>
    <div class="controls">
    <input type="text" name="name" placeholder="Name" value="<?php echo $check['name'];?>" required/>
    </div>
    </div>

    <div class="control-group">
    <label class="control-label"><b>Email Address</b></label>
    <div class="controls">
    <input type="text" name="email" placeholder="Email Address" value="<?php echo $check['email'];?>" required/>
    </div>
    </div>

    <div class="control-group">
    <label class="control-label"><b>Mobile</b></label>
    <div class="controls">
    <input type="text" name="mobile" placeholder="Mobile" value="<?php echo $check['mobile'];?>" required/>
    </div>
    </div>

 <div class="control-group">
 <label class="control-label"><b>Gender</b></label>
 <div class="controls">
 <input type="radio" name="gender" value="Male"<?php if($check['gender']=="Male"){echo "checked";}?>>Male
 <br>
 <input type="radio" name="gender" value="Female"<?php if($check['gender']=="Female"){echo "checked";}?>>Female
 </div>
 </div>

 <div class="control-group">
 <label class="control-label"><b>City</b></label>
 <div class="controls">
 <select name="city">
<?php
//full loop crud
  $cities=array("Karachi","Quetta","Lahore","Islamabad","Lahore","Rawalpindi","Karachi","Rawalpindi","Quetta","Lahore","Tando Allahyar","Chakwal","Raiwind");

  foreach($cities as $city)
  {
    if ($city == $check['city']) {$showDefault = 'selected="selected"';} else {$showDefault = "";}
?>
    <option value="<?=$city;?>"<?=$showDefault;?>><?=$city;?></option>
    <?php
  }
?>

</select>
</div>
</div>


<div class="control-group">
<label class="control-label"><b>Age</b></label>
<div class="controls">
<select name="age">
<option value>Year</option>
<?php

for ($i = 0; $i <=102; $i++)
{
if ($i == $check['age']) {$showDefault = 'selected="selected"';} else {$showDefault = "";}
?>
<option value="<?=$i;?>"<?=$showDefault;?>><?=$i;?></option>
<?php
}
?>
</select>
</div>
</div>

<tr>

 <td><label class="control-label"> </label></td>
  <td>
  <p><?php echo '<img style=width:80px; height:80px; src="'.$check['image'].'">' ;?></p>
 <p><input style="padding:0px 160px;" class="input-group" type="file" name="uploadedimage" accept="image/*"></p>

</td>
</tr>


<div class="form-actions">
<button class="btn btn-info" type="submit" name="done">Save</button>
<a class="btn" href="index.php">back</a>
</div>
</form>
</div>
</div>

</body>
</html> 

  • Step7:read.php
 <?php
require 'dbconnect.php';

  
    $id = null;
    if ( !empty($_GET['id'])) {
        $id = $_REQUEST['id'];
    }
   
    if ( null==$id ) {
        header("Location: index.php");
    }else{
        $query="SELECT * FROM customers where id='$id'";
        $run=mysql_query($query);
        $data=mysql_fetch_array($run);
    }


?>

<!DOCTYPE html>
<html>
<head>
    <title>Read</title>
    <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
    <script type="js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="span10 offset1">
<div class="row">
    <h3>Read Customers </h3>
    </div>
    <div class="form-horizontal">
    <div class="control-group">
    <label class="control-label"><b>Name</b></label>
    <div class="controls">
    <label class="checkbox">
    <?php echo $data['name']; ?>
    </label>
    </div>
    </div>

    <div class="control-group">
    <label class="control-label"><b>Email Address</b></label>
    <div class="controls">
    <label class="checkbox">
        <?php echo $data['email']; ?>
    </label>
    </div>
    </div>

    <div class="control-group">
    <label class="control-label"><b>Mobile</b></label>
    <div class="controls">
    <label class="checkbox">
        <?php echo $data['mobile']; ?>
    </label>
    </div>
    </div>

    <div class="control-group">
    <label class="control-label"><b>Gender</b></label>
    <div class="controls">
    <label class="checkbox">
        <?php echo $data['gender']; ?>
    </label>
    </div>
    </div>

    <div class="control-group">
    <label class="control-label"><b>City</b></label>
    <div class="controls">
    <label class="checkbox">
        <?php echo $data['city']; ?>
    </label>
    </div>
    </div>

    <div class="control-group">
    <label class="control-label"><b>Age</b></label>
    <div class="controls">
    <label class="checkbox">
        <?php echo $data['age']; ?>
    </label>
    </div>
    </div>

    <div class="control-group">
    <label class="control-label"><b>Image</b></label>
    <div class="controls">
    <label class="checkbox">
        <?php echo '<img style=height:50px; width:50px; src="'.$data['image'].'">';?>
    </label>
    </div>
    </div>

    <div class="control-group">
    <label class="control-label"><b>Submission Date</b></label>
    <div class="controls">
    <label class="checkbox">
    <?php echo $data['submission_date']; ?>
    </label>
    </div>
    </div>

    <div class="form-actions">
    <a class="btn" href="index.php">back</a>
    </div>

</div>
</div>
</div>
</body>
</html>
  •  Step7:delete.php
<?php
    require 'dbconnect.php';
  
if (empty($_GET['name'])) {

$id=$_GET['id'];
$delete = mysql_query ("DELETE FROM customers WHERE id=$id ") or die (mysql_error());
   
}

?>

<script type="text/javascript">
window.location = "index.php";
</script>

<img src="<?=$no['image']; ?>" style="width:90px;" /> <br>  <br>  


(i hope this is useds full for you)

Comments