ok

Mini Shell

Direktori : /home2/selectio/www/thedentalpark.com/admin/
Upload File :
Current File : /home2/selectio/www/thedentalpark.com/admin/college-add.php

<?php  include("header.php");
error_reporting(E_ALL);

if(isset($_POST['college_name'])) {
    try {
        
        $college_name   = isset($_POST['college_name']) ?  $_POST['college_name'] : '';
        $college_url    = isset($_POST['college_url']) ?  $_POST['college_url'] : '';
        $college_image  = isset($_FILES['college_image']['name']) ?  $_FILES['college_image']['name'] : '';
        $status         = isset($_POST['status']) ?  $_POST['status'] : '';
        $update_id      = isset($_POST['update_id']) ?  $_POST['update_id'] : '';
        
        $old_college_image = isset($_POST['old_college_image']) ?  $_POST['old_college_image'] : '';
        
        $date = date("Y-m-d");
        
        
        function imageUpload($pre=null){
            
            // Get file details
            $image_name = $_FILES['college_image']['name'];
            $image_tmp = $_FILES['college_image']['tmp_name'];
            $imagesize = $_FILES['college_image']['size'];
            $extension = strtolower(pathinfo($image_name, PATHINFO_EXTENSION));
            $maxFileSize = 2 * 1024 * 1024; // 2MB
            
            if($extension != 'jpg' && $extension != 'png' && $extension != 'jpeg' && $extension != 'webp'){
                $_SESSION['error_message'] = 'Only JPG, JPEG, PNG, and WEBP files are allowed.' ;
                 header("Location: college-add.php");
                exit; 
            }elseif($imagesize > $maxFileSize){
                $_SESSION['error_message'] = 'File size exceeds the maximum limit of 2MB.' ;
                 header("Location: college-add.php");
                 exit; 
            }else{
                
                if(!empty($pre)){
                     if (file_exists('../assets/img/college/' . $pre)) {
                        unlink('../assets/img/college/' . $pre);
                    }
                }
                
                $final_name = 'college_'.rand(1000,9999).'.webp' ;
                if (move_uploaded_file($image_tmp, '../assets/img/college/' . $final_name)) {
                     return $final_name;
                } else {
                    $_SESSION['error_message'] = 'Failed to upload image' ;
                    header("Location: college-add.php");
                     exit;
                }
               
            }

        }
       

        if($update_id){
            
            
            if(!empty($college_image)){
              $college_img = imageUpload($old_college_image);
            }else{
               $college_img = $old_college_image;
            }
            
            
        	$statement = $pdo->prepare("UPDATE tbl_college SET college_name=?, college_image=?, college_url=?,  status=? WHERE id=?");
            $statement->execute(array($college_name, $college_img, $college_url, $status, $update_id));
            
            // Check result of database operation
            if($statement) {
                $_SESSION['success_message'] = 'College Details Updated Successfully' ;
                header("Location: college-list.php");
                exit;
            } else {
                 $_SESSION['error_message'] = 'Error Occured Not Added';
                header("Location: college-add.php");
                exit;
            }
         
        }else{
             
            if(!empty($college_image)){
                $college_img = imageUpload();
            }

          	// Inserting the slider details into the database
    		$statement = $pdo->prepare("INSERT INTO tbl_college (college_name, college_image, college_url, status) VALUES (?,?,?,?)");
    		$statement->execute(array($college_name, $college_img, $college_url, $status));

            // Check result of database operation
            if($statement) {
                $_SESSION['success_message'] = 'College Details Added Successfully' ;
                 header("Location: college-list.php");
                exit;
            } else {
                 $_SESSION['error_message'] = 'Error Occured Not Added';
                  header("Location: college-add.php");
                exit;
            }  
           
        }

    } catch(Exception $e) {
        $_SESSION['error_message'] =  $e->getMessage();  
        header("Location: college-add.php");
        exit;
    }
}


if(isset($_GET['id']) && $_GET['id']!=''){
    
    $id = isset($_GET['id']) ? $_GET['id'] : '';
    
    $statement = $pdo->prepare("SELECT * FROM tbl_college WHERE id=?");
	$statement->execute(array($id));
	$edit_result = $statement->fetchAll(PDO::FETCH_ASSOC);
	$edit_count = $statement->rowCount();
	
	
	$college_name = $edit_result[0]['college_name'];
	$college_url = $edit_result[0]['college_url'];
	$college_image = $edit_result[0]['college_image'];
	$status = $edit_result[0]['status'];
}
 
?>

    <div class="content-wrapper" style="background-color: white;">

        <div class="clearfix"></div>
        
        <section class="content">
            
            <div class="row">
                <div class="col-xs-12">
                    <?php if(!empty($_SESSION['error_message'])) { ?>
                		<div class="callout callout-danger">
                		    <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
                			<p> <i class="icon fa fa-warning"></i> <?php echo $_SESSION['error_message']; ?></p>
                		</div>
            		<?php  unset($_SESSION['error_message']); }?>
            
            		<?php if(!empty($_SESSION['success_message'])){ ?>
                		<div class="callout callout-success">
                		    <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
                			<p> <i class="icon fa fa-check"></i> <?php echo $_SESSION['success_message']; ?></p>
                		</div>
            		<?php  unset($_SESSION['success_message']); } ?>
        		</div>
            </div>
            
            <div class="row">
                <div class="col-xs-12">
                    <div>
                        <div class="box-header">
                            <h3 class="box-title"><b><?php if(isset($_GET['id']) && $_GET['id']!=''){ echo 'Update';}else { echo 'Add';} ?> College: </b></h3>
                        </div>
                        
                        <div class="box box-info">
                            <div class="box-body" style="box-shadow: rgba(0, 0, 0, 0.16) 0px 3px 6px, rgba(0, 0, 0, 0.23) 0px 3px 6px;">
                                <div class="col-lg-12">
                                    <div id="content-div">
                                        <form action="" enctype="multipart/form-data" method="post" id="college_form" accept-charset="utf-8">
                                            <div class="row">
                                                <input type="hidden" name="update_id" id="update_id" value="<?php echo (isset($_GET['id']) ? $_GET['id'] : '') ?>">
                                                <input type="hidden" name="old_college_image" id="old_college_image" value="<?php echo (isset($college_image) ? $college_image : '') ?>">
                                        
                                                <div class="col-md-6">
                                                    <div class="form-group">
                                                        <label class="required" for="college_name">College Name</label>                                    
                                                        <input type="text" name="college_name"  id="college_name" value="<?php echo (isset($college_name) ? $college_name : '') ?>" placeholder="Enter College Name" class="form-control" />
                                                    </div>
                                                </div>
                                                
                                                <div class="col-md-6">
                                                    <div class="form-group">
                                                        <label class="required" for="college_image">Choose The College Image</label>   <span>(Recommanded Size 600*400)</span>                                 
                                                        <input type="file" name="college_image"  id="college_image" value="" placeholder="Choose The banner Image" class="form-control tip"/>
                                                        
                                                        <?php 
                                                            if(!empty($college_image)){
                                                                echo "<img src='../assets/img/college/$college_image' alt='college image' style='width:150px;height:60px;object-fit:contain'>";
                                                            }
                                                          
                                                        ?>
                                                        
                                                    </div>
                                                </div>
                                                
                                                 <div class="col-md-6">
                                                    <div class="form-group">
                                                        <label class="required" for="college_url">College Url</label>                                    
                                                        <input type="text" name="college_url"  id="college_url" value="<?php echo (isset($college_url) ? $college_url : '') ?>" placeholder="Enter College Url" class="form-control" />
                                                    </div>
                                                </div>
                                                
                                                 <div class="col-sm-6">
                                                    <div class="form-group">
                                                        <label class="required" for="status">Status</label>
                                                        <select name="status" class="form-control select2" style="width:100%" id="status">
                                                            <option value="1" <?php if(!empty($status) && $status==1){ echo "selected";} ?>>Active</option>
                                                            <option value="0" <?php if(!empty($status) && $status==0){ echo "selected";} ?>>In-Active</option>
                                                        </select>
                                                    </div>
                                                </div>
                                                
                                            </div>
                                            <div class="row">
                                                <div class="form-group" style="text-align:center;margin-top:10px">
                                                    <button type="submit" id="submit_college" name="submit_college" class="btn btn-success"><?php if(isset($_GET['id']) && $_GET['id']!=''){ echo 'Update';}else { echo 'Add';} ?> College</button>
                                                </div>
                                            </div>
                                        </form>
                                        
                                        <div class="form-group" style="text-align:center;">
                                            <a href="college-add.php"><button class="btn btn-primary" ><i class="fa fa-refresh"></i> Reset Form</button></a>
                                            <a href="college-list.php"><button class="btn btn-warning" ><i class="fa fa-chevron-left"></i> Back to List</button></a>
                                        </div>
                                    </div>
                                     
                                     
                                    <!--<div class="container" id="loading-div" style="display: none; margin-top: 40px; margin-bottom: 40px;">-->
                                    <!--    <center> <img src="img/loading.gif" style="width: 250px;"><br><b class="blinking">Processing Please Wait...</b></center>-->
                                    <!--</div>-->
                            
                                    <!--<div class="container" id="success-div" style="display: none; margin-top: 40px; margin-bottom: 40px;">-->
                                    <!--    <center><b class="blinking" style="font-size:16px;"> College Details </?php if(isset($_GET['id']) && $_GET['id']!=''){ echo 'Updated';}else { echo 'Added';} ?> Successfully...</b><br>-->
                                    <!--        <img src="img/success.gif" style="width: 250px;">-->
                                    <!--        <br><br>-->
                                    <!--        <a href="college-add.php</?php if(isset($_GET['id']) && $_GET['id']!=''){ echo '?key='.$_GET['id'];}?>"><button class="btn btn-info btn-sm"><i class="fa fa-refresh"></i> View Updated Details</button></a>-->
                                    <!--        &nbsp;&nbsp;-->
                                    <!--        <a href="college-list.php"><button class="btn btn-warning btn-sm"><i class="fa fa-chevron-left"></i> Back to List</button></a>-->
                                    <!--    </center>-->
                                    <!--</div>-->
                                    
                                    <div class="clearfix"></div>
                                </div>
                            </div>
                        </div> 
                    </div>
                </div>
            </div>
        </section>


<?php include "footer.php";?>

<script type="text/javascript">

    $(document).ready(function() {

            $.validator.addMethod("large_text_field",function(value,element){
                if(value){
                    return this.optional(element) || (/[\=\<\>]+/.test(value))? false : true;
                }else{
                    return true;
                }
            },"Don't Use '=' Symbol and Tag '<>' Symbol ");

            $.validator.addMethod('filesize', function (value, element) {
                return this.optional(element) || (element.files[0].size < 2097153)
            }, 'File size must be less than 2mb');

            $('#college_form').validate({
                rules:{
                    'college_name': {
                        required: true,
                        large_text_field:true,
                        maxlength: 200
                    },
                    'college_image': {
                        required: true,
                        extension:'png|jpg|jpeg|webp',
                        filesize: true
                    },
                    'college_url':{
                       required: true,
                       url:true
                    },
                    'status':{
                       required: true
                    }
                   
                },
                errorPlacement: function(error, element) {
                    var ername = element.attr('name');
                    error.css('color', 'red');
                    element.parents('.form-group').append(error);
                   
                },
                messages:{
                    'college_image': {
                        extension: 'png, jpg, jpeg, webp formats only allowed'
                    }
                },
                submitHandler: function(form){
                     
                    $('#submit_college').attr('disabled',true).text("Processing...");
                    form.submit();
                    
                }


            });

            var update_id = $("#update_id").val();
            if(update_id){
               $("#college_image").rules('remove', 'required');
            }
            
        });
</script>

Zerion Mini Shell 1.0