PHP Classes

Short Code CRUD: Perform CRUD operations on MySQL table records

Recommend this page to a friend!
  Info   View files Example   View files View files (5)   DownloadInstall with Composer Download .zip   Reputation   Support forum (10)   Blog    
Ratings Unique User Downloads Download Rankings
StarStarStarStar 62%Total: 1,190 All time: 3,183 This week: 148Up
Version License PHP version Categories
short-code-crud 1.9Freeware5HTML, PHP 5, Databases
Description 

Author

This class can perform CRUD operations on MySQL table records.

It can perform several types of operations with records of a given MySQL table like:

- InsertRecord : Insert Record to database
- InsertMultipleRecord : Insert Multiple Records
- GetSingleRecord : Retrieve Single Record for Edit or View Action
- GetRecord: Get All or a limited number of Records with pagination
- UpdateRecord : Edit or Update Record Method
- DeleteRecord : Delete Record Method
- GetCustom : For results for arbitrary queries

The class can also generate HTML to display or update the table records like:

- PagiNation : Pagination Display
- UploadFile : Upload File
- DeleteFile : Delete File
- DateDifference : Date Difference
- RedirectPage : To Redirect Specific Pages
- EditLink : Edit Action Link
- StatusLink : Change active or inactive status Action Link
- DeleteLink : Delete Action Link
- SendMail : Send Mail method
- GetRandomString : Generate Random String

Recommendations

What is the best PHP crud class?
How to create CRUD with PHP

Access data base
Pre-written PHP MySQL functions class

upload .txt file
I need code for upload .txt file

Upload multiple images path
I need to upload multiple images from one file tag in HTML

upload multiple files
I need script for upload multiple files and images

CRUD
Perform CRUD operations on MySQL table records

Picture of Bharat Parmar
  Performance   Level  
Name: Bharat Parmar <contact>
Classes: 11 packages by
Country: India India
Age: 32
All time rank: 52230 in India India
Week rank: 198 Up18 in India India Up
Innovation award
Innovation award
Nominee: 3x

Example

<?php
@include("config.php");
@include(
"class/Main.class.php");
$Main = new Main();
$Main->pagetitle="User Management";

/*BEGIN USER MANAGEMENT FUNCTIONS ASSIGN */
if(isset($_POST['user_addnew']))
{
   
array_splice($_POST, -1); //WILL REMOVE LAST ELEMENT (SUBMIT BUTTON KEY AND VALUE)
   
$insert_id = $this->InsertRecord("user_master",$_POST);

    if(
$insert_id>0)
    {
       
$_SESSION['message'] = array("status"=>1,"message"=>"Record has been added successfully.");
    }
   
$this->RedirectPage($this->pagefilename);
}
else if(isset(
$_POST['user_edit']) && isset($_GET['action']) && $_GET['action']=="edit" && isset($_GET['id']) && is_numeric($_GET['id']))
{
   
array_splice($_POST, -1); //WILL REMOVE LAST ELEMENT (SUBMIT BUTTON KEY AND VALUE)
   
$records = $this->UpdateRecord("user_master",$_POST,"user_id='".$_GET['id']."'");
    if(
$records>0)
    {
       
$_SESSION['message'] = array("status"=>1,"message"=> " Record has been updated successfully.");
    }
   
$this->RedirectPage($this->pagefilename);
}
else if(isset(
$_GET['action']) && $_GET['action']=="delete" && isset($_GET['id']))
{
   
$records = $this->DeleteRecord("user_master","user_id='".$_GET['id']."'");
    if(
$records>0)
    {
       
$_SESSION['admin_message'] = array("status"=>1,"message"=> " Record has been deleted successfully.");
    }
   
$this->RedirectPage($this->pagefilename);
}
else if(isset(
$_GET['action']) && $_GET['action']=="status" && isset($_GET['status']) && is_numeric($_GET['status']) && isset($_GET['id']))
{
   
$info_array = array("active_status"=>$_GET['status']);
   
$records = $this->UpdateRecord("user_master",$info_array,"user_id='".$_GET['id']."'");
    if(
$records>0)
    {
       
$_SESSION['admin_message'] = array("status"=>1,"message"=> " Active status has been changed successfully.");
    }
   
$this->RedirectPage($this->pagefilename);
}

/*END USER MANAGEMENT FUNCTIONS ASSIGN */
?>

<?php if(isset($_GET['action']) && ($_GET['action']=="addnew" || $_GET['action']=="edit")) {

    if(
$_GET['action']=="edit")
    {
       
$info_array = array("where"=>"user_id='".$_GET['id']."'");
       
$userdata = $Admin->GetSingleRecord("user_master",$info_array);
    }

   
?>

    <form method="post" enctype="multipart/form-data">

            <div class="form-group">
                <lable>First Name</lable>
                <input type="text" name="firstname" id="firstname" class="form-control alphanumeric" value="<?php echo stripslashes(@$userdata['firstname']);?>" title="Enter First Name">
            </div>

            <div class="form-group">
                <lable>Last Name</lable>
                <input type="text" name="lastname" id="lastname" class="form-control alphanumeric" value="<?php echo stripslashes(@$userdata['lastname']);?>" title="Enter Last Name">
            </div>

            <div class="form-group">
                <lable>Email</lable>
                <input type="text" name="email" id="email" class="form-control" value="<?php echo stripslashes(@$userdata['email']);?>" title="Enter Email">
            </div>

            <div class="form-group">
                <lable>Password</lable>
                <input type="text" name="password" id="password" class="form-control" value="" placeholder="Enter Password Only If You Want to Set/Change it.">
            </div>

            <div class="form-group">
                <lable>Active Status</lable>
                <select name="active_status" id="active_status" class="form-control">
                    <option value="1" <?php if(@$userdata['active_status']=="1") echo "selected"; ?>>Active</option>
                    <option value="0" <?php if(@$userdata['active_status']=="0") echo "selected"; ?>>Inactive</option>
                </select>
            </div>

            <div class="btn-toolbar list-toolbar">
                  <input type="submit" name="user_<?php echo $_GET['action'];?>" value="Save" class="btn btn-primary">
                <input type="button" name="cancel_button" value="Cancel" onclick="window.location='user.php';" class="btn btn-default">
            </div>
          </form>
<?php } else { ?>



<table class="table table-hover" id="datarecord">
    <thead>
        <tr>
            <th>User ID</th>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Email</th>
            <th>Register Date</th>
            <th>Action</th>
        </tr>
    </thead>
      <tbody>
          <?php
              $startfrom
= 0;
             
$limit=$_SESSION['pagerecords_limit'];

              if(isset(
$_GET['page']) && is_numeric($_GET['page']))
              {
                 
$startfrom= ($_GET['page']*$_SESSION['pagerecords_limit']-$_SESSION['pagerecords_limit']);
              }

           
$info_array = array(
                                   
"orderby"=>"user_id",
                                   
"ordertype"=>"desc",
                                   
"limit"=>$limit,
                                   
"startfrom"=>$startfrom
                               
);
           
$records = $Main->GetRecord("user_master",$info_array);

           
$srno=$startfrom;
            if(@
count($records)>0)
            {
                foreach(
$records as $key=>$value)
                {
                   
?>
                        <tr>
                            <td><?php echo $value['user_id']; ?></td>
                            <td><?php echo stripslashes($value['firstname']); ?></td>
                            <td><?php echo stripslashes($value['lastname']); ?></td>
                            <td><?php echo stripslashes($value['email']); ?></td>
                            <td><?php echo date("d-m-Y H:i:s",strtotime($value['register_date'])); ?></td>
                            <td>
                                <?php if($value['active_status']==0) { ?>
<a href="<?php echo $Main->StatusLink($value['user_id'],1);?>"><span class="label label-danger">Inactive</span></a>
                                <?php } else { ?>
<a href="<?php echo $Main->StatusLink($value['user_id'],0);?>"><span class="label label-success">Active</span></a>
                                <?php } ?>

                                <a href="<?php echo $Main->DeleteLink($value['user_id']);?>" class="label label-danger" onclick="return confirm('Are You Sure To Delete This Record?');" style="margin-right:4px;">
                                <i class="fa fa-times"></i> Delete </a>
                            </td>
                        </tr>
                    <?php
               
}
            }
            else
            {
                echo
"<tr><td colspan='4' align='center'>Records not available.</td></tr>";
            }

       
?>
</tbody>
</table>

<?php $Main->PagiNation("user_master","user_id");?>

<?php } ?>


  Files folder image Files  
File Role Description
Files folder imageclass (1 file)
Accessible without login Plain text file user.php Example Example Script for the class
Accessible without login Plain text file config.php Conf. Configuration file for the script which includes the database connection and other settings for the script
Accessible without login Plain text file database.php Conf. Database File For the demo script
Accessible without login Plain text file README.txt Doc. Script Description file

  Files folder image Files  /  class  
File Role Description
  Plain text file Main.class.php Class Class file for all the methods

 Version Control Unique User Downloads Download Rankings  
 0%
Total:1,190
This week:0
All time:3,183
This week:148Up
User Ratings User Comments (7)
 All time
Utility:78%StarStarStarStar
Consistency:85%StarStarStarStarStar
Documentation:75%StarStarStarStar
Examples:67%StarStarStarStar
Tests:-
Videos:-
Overall:62%StarStarStarStar
Rank:1156
 
nice
6 years ago (muabshir)
80%StarStarStarStarStar
uses depricated php mysql driver
7 years ago (deep dhaliwal)
22%StarStar
Methods are very usefull for my new project setup.
8 years ago (Lucy Grill)
80%StarStarStarStarStar
nice work and codding structure.
8 years ago (Jack Thomas)
80%StarStarStarStarStar
lack of useful documentation is unacceptable, who's got time ...
8 years ago (fred capasso)
17%Star
This Code Very Helpful For me I save lot of time This code is...
8 years ago (Harsh Patel)
70%StarStarStarStar
Very Useful class and save more time.
8 years ago (Er. Chintan Vekariya)
70%StarStarStarStar