🐘 PHP Mga IT Gods Need your help

Status
Not open for further replies.
patingin nga po ng classes/Users.php
PHP:
<?php
require_once('../config.php');
Class Users extends DBConnection {
    private $settings;
    public function __construct(){
        global $_settings;
        $this->settings = $_settings;
        parent::__construct();
    }
    public function __destruct(){
        parent::__destruct();
    }
    public function save_users(){
        if(!isset($_POST['status']) && $this->settings->userdata('login_type') == 1){
            $_POST['status'] = 1;
            $_POST['type'] = 2;
        }
        extract($_POST);
        $oid = $id;
        $data = '';
        if(isset($oldpassword)){
            if(md5($oldpassword) != $this->settings->userdata('password')){
                return 4;
            }
        }
        $chk = $this->conn->query("SELECT * FROM `users` where username ='{$username}' ".($id>0? " and id!= '{$id}' " : ""))->num_rows;
        if($chk > 0){
            return 3;
            exit;
        }
        foreach($_POST as $k => $v){
            if(in_array($k,array('firstname','middlename','lastname','username','type'))){
                if(!empty($data)) $data .=" , ";
                $data .= " {$k} = '{$v}' ";
            }
        }
        if(!empty($password)){
            $password = md5($password);
            if(!empty($data)) $data .=" , ";
            $data .= " `password` = '{$password}' ";
        }

        if(empty($id)){
            $qry = $this->conn->query("INSERT INTO users set {$data}");
            if($qry){
                $id = $this->conn->insert_id;
                $this->settings->set_flashdata('success','User Details successfully saved.');
                $resp['status'] = 1;
            }else{
                $resp['status'] = 2;
            }

        }else{
            $qry = $this->conn->query("UPDATE users set $data where id = {$id}");
            if($qry){
                $this->settings->set_flashdata('success','User Details successfully updated.');
                if($id == $this->settings->userdata('id')){
                    foreach($_POST as $k => $v){
                        if($k != 'id'){
                            if(!empty($data)) $data .=" , ";
                            $this->settings->set_userdata($k,$v);
                        }
                    }
                    
                }
                $resp['status'] = 1;
            }else{
                $resp['status'] = 2;
            }
            
        }
        
        if(isset($_FILES['img']) && $_FILES['img']['tmp_name'] != ''){
            $fname = 'uploads/avatar-'.$id.'.png';
            $dir_path =base_app. $fname;
            $upload = $_FILES['img']['tmp_name'];
            $type = mime_content_type($upload);
            $allowed = array('image/png','image/jpeg');
            if(!in_array($type,$allowed)){
                $resp['msg'].=" But Image failed to upload due to invalid file type.";
            }else{
                $new_height = 200;
                $new_width = 200;
        
                list($width, $height) = getimagesize($upload);
                $t_image = imagecreatetruecolor($new_width, $new_height);
                imagealphablending( $t_image, false );
                imagesavealpha( $t_image, true );
                $gdImg = ($type == 'image/png')? imagecreatefrompng($upload) : imagecreatefromjpeg($upload);
                imagecopyresampled($t_image, $gdImg, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
                if($gdImg){
                        if(is_file($dir_path))
                        unlink($dir_path);
                        $uploaded_img = imagepng($t_image,$dir_path);
                        imagedestroy($gdImg);
                        imagedestroy($t_image);
                }else{
                $resp['msg'].=" But Image failed to upload due to unkown reason.";
                }
            }
            if(isset($uploaded_img)){
                $this->conn->query("UPDATE users set `avatar` = CONCAT('{$fname}','?v=',unix_timestamp(CURRENT_TIMESTAMP)) where id = '{$id}' ");
                if($id == $this->settings->userdata('id')){
                        $this->settings->set_userdata('avatar',$fname);
                }
            }
        }
        if(isset($resp['msg']))
        $this->settings->set_flashdata('success',$resp['msg']);
        return  $resp['status'];
    }
    public function delete_users(){
        extract($_POST);
        $avatar = $this->conn->query("SELECT avatar FROM users where id = '{$id}'")->fetch_array()['avatar'];
        $qry = $this->conn->query("DELETE FROM users where id = $id");
        if($qry){
            $avatar = explode("?",$avatar)[0];
            $this->settings->set_flashdata('success','User Details successfully deleted.');
            if(is_file(base_app.$avatar))
                unlink(base_app.$avatar);
            $resp['status'] = 'success';
        }else{
            $resp['status'] = 'failed';
        }
        return json_encode($resp);
    }
    public function save_student(){
        extract($_POST);
        $data = '';
        if(isset($oldpassword)){
            if(md5($oldpassword) != $this->settings->userdata('password')){
                return json_encode(array("status"=>'failed',
                                         "msg"=>'Old Password is Incorrect'));
            }
        }
        $chk = $this->conn->query("SELECT * FROM `personnel` where email ='{$email}' ".($id>0? " and id!= '{$id}' " : ""))->num_rows;
        if($chk > 0){
            return 3;
            exit;
        }
        foreach($_POST as $k => $v){
            if(!in_array($k,array('id','oldpassword','cpassword','password'))){
                if(!empty($data)) $data .=" , ";
                $data .= " {$k} = '{$v}' ";
            }
        }
        if(!empty($password)){
            $password = md5($password);
            if(!empty($data)) $data .=" , ";
            $data .= " `password` = '{$password}' ";
        }

        if(empty($id)){
            $qry = $this->conn->query("INSERT INTO personnel set {$data}");
            if($qry){
                $id = $this->conn->insert_id;
                $this->settings->set_flashdata('success',' User Details successfully saved.');
                $resp['status'] = "success";
            }else{
                $resp['status'] = "failed";
                $resp['msg'] = "An error occurred while saving the data. Error: ". $this->conn->error;
            }

        }else{
            $qry = $this->conn->query("UPDATE personnel set $data where id = {$id}");
            if($qry){
                $this->settings->set_flashdata('success','User Details successfully updated.');
                if($id == $this->settings->userdata('id')){
                    foreach($_POST as $k => $v){
                        if($k != 'id'){
                            if(!empty($data)) $data .=" , ";
                            $this->settings->set_userdata($k,$v);
                        }
                    }
                    
                }
                $resp['status'] = "success";
            }else{
                $resp['status'] = "failed";
                $resp['msg'] = "An error occurred while saving the data. Error: ". $this->conn->error;
            }
            
        }
        
        if(isset($_FILES['img']) && $_FILES['img']['tmp_name'] != ''){
            $fname = 'uploads/student-'.$id.'.png';
            $dir_path =base_app. $fname;
            $upload = $_FILES['img']['tmp_name'];
            $type = mime_content_type($upload);
            $allowed = array('image/png','image/jpeg');
            if(!in_array($type,$allowed)){
                $resp['msg'].=" But Image failed to upload due to invalid file type.";
            }else{
                $new_height = 200;
                $new_width = 200;
        
                list($width, $height) = getimagesize($upload);
                $t_image = imagecreatetruecolor($new_width, $new_height);
                imagealphablending( $t_image, false );
                imagesavealpha( $t_image, true );
                $gdImg = ($type == 'image/png')? imagecreatefrompng($upload) : imagecreatefromjpeg($upload);
                imagecopyresampled($t_image, $gdImg, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
                if($gdImg){
                        if(is_file($dir_path))
                        unlink($dir_path);
                        $uploaded_img = imagepng($t_image,$dir_path);
                        imagedestroy($gdImg);
                        imagedestroy($t_image);
                }else{
                $resp['msg'].=" But Image failed to upload due to unkown reason.";
                }
            }
            if(isset($uploaded_img)){
                $this->conn->query("UPDATE personnel set `avatar` = CONCAT('{$fname}','?v=',unix_timestamp(CURRENT_TIMESTAMP)) where id = '{$id}' ");
                if($id == $this->settings->userdata('id')){
                        $this->settings->set_userdata('avatar',$fname);
                }
            }
        }
        
        return  json_encode($resp);
    }
    public function delete_student(){
        extract($_POST);
        $avatar = $this->conn->query("SELECT avatar FROM personnel where id = '{$id}'")->fetch_array()['avatar'];
        $qry = $this->conn->query("DELETE FROM personnel where id = $id");
        if($qry){
            $avatar = explode("?",$avatar)[0];
            $this->settings->set_flashdata('success',' User Details successfully deleted.');
            if(is_file(base_app.$avatar))
                unlink(base_app.$avatar);
            $resp['status'] = 'success';
        }else{
            $resp['status'] = 'failed';
        }
        return json_encode($resp);
    }
    public function verify_student(){
        extract($_POST);
        $update = $this->conn->query("UPDATE `personnel` set `status` = 1 where id = $id");
        if($update){
            $this->settings->set_flashdata('success',' User Account has verified successfully.');
            $resp['status'] = 'success';
        }else{
            $resp['status'] = 'failed';
        }
        return json_encode($resp);
    }
    
}

$users = new users();
$action = !isset($_GET['f']) ? 'none' : strtolower($_GET['f']);
switch ($action) {
    case 'save':
        echo $users->save_users();
    break;
    case 'delete':
        echo $users->delete_users();
    break;
    case 'save_student':
        echo $users->save_student();
    break;
    case 'delete_student':
        echo $users->delete_student();
    break;
    case 'verify_student':
        echo $users->verify_student();
    break;
    default:
        // echo $sysset->index();
        break;
}
 
hahaha kita niyo na po problema?
ito ba?
PHP:
if(empty($id)){
            $qry = $this->conn->query("INSERT INTO users set {$data}");
            if($qry){
                $id = $this->conn->insert_id;
                $this->settings->set_flashdata('success','User Details successfully saved.');
                $resp['status'] = 1;
            }else{
                $resp['status'] = 2;
            }
 
haha hindi yan paps.

PHP:
<?php
require_once('../config.php');
Class Users extends DBConnection {
    private $settings;
    public function __construct(){
        global $_settings;
        $this->settings = $_settings;
        parent::__construct();
    }
    public function __destruct(){
        parent::__destruct();
    }
    public function save_users(){
        if(!isset($_POST['status']) && $this->settings->userdata('login_type') == 1){
            $_POST['status'] = 1;
            $_POST['type'] = 2;
        }
        extract($_POST);
        $oid = $id;
        $data = '';
        if(isset($oldpassword)){
            if(md5($oldpassword) != $this->settings->userdata('password')){
                return 4;
            }
        }
        $chk = $this->conn->query("SELECT * FROM `users` where username ='{$username}' ".($id>0? " and id!= '{$id}' " : ""))->num_rows;
        if($chk > 0){
            return 3;
            exit;
        }
        foreach($_POST as $k => $v){
            if(in_array($k,array('firstname','middlename','lastname','username','type'))){
                if(!empty($data)) $data .=" , ";
                $data .= " {$k} = '{$v}' ";
            }
        }
        if(!empty($password)){
            $password = md5($password);
            if(!empty($data)) $data .=" , ";
            $data .= " `password` = '{$password}' ";
        }

        if(empty($id)){
            $qry = $this->conn->query("INSERT INTO users set {$data}");
            if($qry){
                $id = $this->conn->insert_id;
                $this->settings->set_flashdata('success','User Details successfully saved.');
                $resp['status'] = 1;
            }else{
                $resp['status'] = 2;
            }

        }else{
            $qry = $this->conn->query("UPDATE users set $data where id = {$id}");
            if($qry){
                $this->settings->set_flashdata('success','User Details successfully updated.');
                if($id == $this->settings->userdata('id')){
                    foreach($_POST as $k => $v){
                        if($k != 'id'){
                            if(!empty($data)) $data .=" , ";
                            $this->settings->set_userdata($k,$v);
                        }
                    }
                   
                }
                $resp['status'] = 1;
            }else{
                $resp['status'] = 2;
            }
           
        }
       
        if(isset($_FILES['img']) && $_FILES['img']['tmp_name'] != ''){
            $fname = 'uploads/avatar-'.$id.'.png';
            $dir_path =base_app. $fname;
            $upload = $_FILES['img']['tmp_name'];
            $type = mime_content_type($upload);
            $allowed = array('image/png','image/jpeg');
            if(!in_array($type,$allowed)){
                $resp['msg'].=" But Image failed to upload due to invalid file type.";
            }else{
                $new_height = 200;
                $new_width = 200;
       
                list($width, $height) = getimagesize($upload);
                $t_image = imagecreatetruecolor($new_width, $new_height);
                imagealphablending( $t_image, false );
                imagesavealpha( $t_image, true );
                $gdImg = ($type == 'image/png')? imagecreatefrompng($upload) : imagecreatefromjpeg($upload);
                imagecopyresampled($t_image, $gdImg, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
                if($gdImg){
                        if(is_file($dir_path))
                        unlink($dir_path);
                        $uploaded_img = imagepng($t_image,$dir_path);
                        imagedestroy($gdImg);
                        imagedestroy($t_image);
                }else{
                $resp['msg'].=" But Image failed to upload due to unkown reason.";
                }
            }
            if(isset($uploaded_img)){
                $this->conn->query("UPDATE users set `avatar` = CONCAT('{$fname}','?v=',unix_timestamp(CURRENT_TIMESTAMP)) where id = '{$id}' ");
                if($id == $this->settings->userdata('id')){
                        $this->settings->set_userdata('avatar',$fname);
                }
            }
        }
        if(isset($resp['msg']))
        $this->settings->set_flashdata('success',$resp['msg']);
        return  $resp['status'];
    }
    public function delete_users(){
        extract($_POST);
        $avatar = $this->conn->query("SELECT avatar FROM users where id = '{$id}'")->fetch_array()['avatar'];
        $qry = $this->conn->query("DELETE FROM users where id = $id");
        if($qry){
            $avatar = explode("?",$avatar)[0];
            $this->settings->set_flashdata('success','User Details successfully deleted.');
            if(is_file(base_app.$avatar))
                unlink(base_app.$avatar);
            $resp['status'] = 'success';
        }else{
            $resp['status'] = 'failed';
        }
        return json_encode($resp);
    }
    public function save_student(){
        extract($_POST);
        $data = '';
        if(isset($oldpassword)){
            if(md5($oldpassword) != $this->settings->userdata('password')){
                return json_encode(array("status"=>'failed',
                                         "msg"=>'Old Password is Incorrect'));
            }
        }
        $chk = $this->conn->query("SELECT * FROM `personnel` where email ='{$email}' ".($id>0? " and id!= '{$id}' " : ""))->num_rows;
        if($chk > 0){
            return 3;
            exit;
        }
        foreach($_POST as $k => $v){
            if(!in_array($k,array('id','oldpassword','cpassword','password'))){
                if(!empty($data)) $data .=" , ";
                $data .= " {$k} = '{$v}' ";
            }
        }
        if(!empty($password)){
            $password = md5($password);
            if(!empty($data)) $data .=" , ";
            $data .= " `password` = '{$password}' ";
        }

        if(empty($id)){
            $qry = $this->conn->query("INSERT INTO personnel set {$data}");
            if($qry){
                $id = $this->conn->insert_id;
                $this->settings->set_flashdata('success',' User Details successfully saved.');
                $resp['status'] = "success";
            }else{
                $resp['status'] = "failed";
                $resp['msg'] = "An error occurred while saving the data. Error: ". $this->conn->error;
            }

        }else{
            $qry = $this->conn->query("UPDATE personnel set $data where id = {$id}");
            if($qry){
                $this->settings->set_flashdata('success','User Details successfully updated.');
                if($id == $this->settings->userdata('id')){
                    foreach($_POST as $k => $v){
                        if($k != 'id'){
                            if(!empty($data)) $data .=" , ";
                            $this->settings->set_userdata($k,$v);
                        }
                    }
                   
                }
                $resp['status'] = "success";
            }else{
                $resp['status'] = "failed";
                $resp['msg'] = "An error occurred while saving the data. Error: ". $this->conn->error;
            }
           
        }
       
        if(isset($_FILES['img']) && $_FILES['img']['tmp_name'] != ''){
            $fname = 'uploads/student-'.$id.'.png';
            $dir_path =base_app. $fname;
            $upload = $_FILES['img']['tmp_name'];
            $type = mime_content_type($upload);
            $allowed = array('image/png','image/jpeg');
            if(!in_array($type,$allowed)){
                $resp['msg'].=" But Image failed to upload due to invalid file type.";
            }else{
                $new_height = 200;
                $new_width = 200;
       
                list($width, $height) = getimagesize($upload);
                $t_image = imagecreatetruecolor($new_width, $new_height);
                imagealphablending( $t_image, false );
                imagesavealpha( $t_image, true );
                $gdImg = ($type == 'image/png')? imagecreatefrompng($upload) : imagecreatefromjpeg($upload);
                imagecopyresampled($t_image, $gdImg, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
                if($gdImg){
                        if(is_file($dir_path))
                        unlink($dir_path);
                        $uploaded_img = imagepng($t_image,$dir_path);
                        imagedestroy($gdImg);
                        imagedestroy($t_image);
                }else{
                $resp['msg'].=" But Image failed to upload due to unkown reason.";
                }
            }
            if(isset($uploaded_img)){
                $this->conn->query("UPDATE personnel set `avatar` = CONCAT('{$fname}','?v=',unix_timestamp(CURRENT_TIMESTAMP)) where id = '{$id}' ");
                if($id == $this->settings->userdata('id')){
                        $this->settings->set_userdata('avatar',$fname);
                }
            }
        }
       
        return  json_encode($resp);
    }
    public function delete_student(){
        extract($_POST);
        $avatar = $this->conn->query("SELECT avatar FROM personnel where id = '{$id}'")->fetch_array()['avatar'];
        $qry = $this->conn->query("DELETE FROM personnel where id = $id");
        if($qry){
            $avatar = explode("?",$avatar)[0];
            $this->settings->set_flashdata('success',' User Details successfully deleted.');
            if(is_file(base_app.$avatar))
                unlink(base_app.$avatar);
            $resp['status'] = 'success';
        }else{
            $resp['status'] = 'failed';
        }
        return json_encode($resp);
    }
    public function verify_student(){
        extract($_POST);
        $update = $this->conn->query("UPDATE `personnel` set `status` = 1 where id = $id");
        if($update){
            $this->settings->set_flashdata('success',' User Account has verified successfully.');
            $resp['status'] = 'success';
        }else{
            $resp['status'] = 'failed';
        }
        return json_encode($resp);
    }
   
}

$users = new users();
$action = !isset($_GET['f']) ? 'none' : strtolower($_GET['f']);
switch ($action) {
    case 'save':
        echo $users->save_users();
    break;
    case 'delete':
        echo $users->delete_users();
    break;
    case 'save_student':
        echo $users->save_student();
    break;
    case 'delete_student':
        echo $users->delete_student();
    break;
    case 'verify_student':
        echo $users->verify_student();
    break;
    default:
        // echo $sysset->index();
        break;
}
PHP:
public function save_users(){
        if(!isset($_POST['status']) && $this->settings->userdata('login_type') == 1){
$_POST['status'] = 1;
// $_POST['type'] = 2; icomment mo to
 
sir nakita mo na yung problema sir.
ahh ito?
PHP:
public function save_users(){
        if(!isset($_POST['status']) && $this->settings->userdata('login_type') == 1){
            $_POST['status'] = 1;
            $_POST['type'] = 2;
        }
dapat alam mo kung anong babaguhin jan or "tatanggalin".
 
PHP:
if(!isset($_POST['status']) && $this->settings->userdata('login_type') == 1){
            $_POST['status'] = 1;
            $_POST['type'] = 2;
            $_POST['type'] = 3;
            $_POST['type'] = 4;
            $_POST['type'] = 5;
            $_POST['type'] = 6;
            $_POST['type'] = 7;
            $_POST['type'] = 8;
        }
 
PHP:
if(!isset($_POST['status']) && $this->settings->userdata('login_type') == 1){
            $_POST['status'] = 1;
            $_POST['type'] = 2;
            $_POST['type'] = 3;
            $_POST['type'] = 4;
            $_POST['type'] = 5;
            $_POST['type'] = 6;
            $_POST['type'] = 7;
            $_POST['type'] = 8;
        }
anu yan sir, 8 lang kalalabasan nyan

PHP:
public function save_users(){
        if(!isset($_POST['status']) && $this->settings->userdata('login_type') == 1){
$_POST['status'] = 1;
// $_POST['type'] = 2; icomment mo to
andito na po yung sagot sa problema niyo po.
 
Overall: wala po sa mismong post niyo po yung problema, wala rin po sa database. Nasa backend po yung problema, actually hindi po problema, naka assign lang po yung $_POST['type'] sa specific na value kaya automatically nag seset siya into STAFF or usertype = 2.
 
Status
Not open for further replies.

About this Thread

  • 116
    Replies
  • 5K
    Views
  • 13
    Participants
Last reply from:
Lianhua

Trending Topics

Online now

Members online
926
Guests online
1,859
Total visitors
2,785

Forum statistics

Threads
2,293,466
Posts
29,084,260
Members
1,208,811
Latest member
depoth231672
Back
Top