Tambah, Edit, dan Hapus Profil User menggunakan PHP MySQL

//lh4.googleusercontent.com/-eAXyxy2JJKA/AAAAAAAAAAI/AAAAAAAAGS4/OHgzqHzCvtQ/s512-c/photo.jpg
September 27, 2018




Selamat pagi kembali lagi bersama kami, kali ini kami akan share bagaimana membuat data record user dimana fitur yang ada pada tutorials ini cuma Tambah, Edit, dan Hapus Profil Pengguna menggunakan PHP / MySQL dan PDO.



Tujuan utama dari sebuah sistem seperti ini adalah admin atau pengguna sistem akan lebih mudah mengenali waja dan identitas dari user dan sebagai record user dalam sebuah perusahaan atau lainnya.

Yang pertama kalian harus membuat beberapa file dan setipa file mempunyai fungsi yang berbeda. Fungsi masing-masing dari file akan dijelaskan pad setiap bagia file.

dbcon.php
index.php
addmember.php
editform.php

Buat database dengan nama regusers kemudian masukka struktur databse dibawah ini.

 
CREATE TABLE `users` (
  `userid` int(11) NOT NULL,
  `username` varchar(20) NOT NULL,
  `description` varchar(50) NOT NULL,
  `userprofile` varchar(200) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

--
-- Dumping data for table `users`
--

INSERT INTO `users` (`userid`, `username`, `description`, `userprofile`) VALUES
(52, 'Arman', 'Basir', '180810.jpg'),
(51, 'Arman', 'Basir', '659774.jpg'),
(54, 'Arman', 'Basir', '21566.jpg'),
(55, 'Arman', 'Basir', '129622.jpg');

--
-- Indexes for table `users`
--
ALTER TABLE `users`
  ADD PRIMARY KEY (`userid`);

--
-- AUTO_INCREMENT for table `users`
--
ALTER TABLE `users`
  MODIFY `userid` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=56;
COMMIT; 

dbcon.php

File ini berisi konfigurasi untuk koneksi kedalam database.

 <?php
$DB_HOST = 'localhost';
$DB_USER = 'root';
$DB_PASS = '';
$DB_NAME = 'regusers';

try{
 $DB_con = new PDO("mysql:host={$DB_HOST};dbname={$DB_NAME}",$DB_USER,$DB_PASS);
 $DB_con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e){
 echo $e->getMessage();
}
?> 

index.php
File ini berisi script untuk menambahkan user dan menampilkan daftar user.

 <?php
require_once 'dbcon.php';

if(isset($_GET['delete_id']))
{
 $stmt_select = $DB_con->prepare('SELECT userprofile FROM users WHERE userid =:uid');
 $stmt_select->execute(array(':uid'=>$_GET['delete_id']));
 $imgRow=$stmt_select->fetch(PDO::FETCH_ASSOC);
 unlink("user_images/".$imgRow['userprofile']);
 $stmt_delete = $DB_con->prepare('DELETE FROM users WHERE userid =:uid');
 $stmt_delete->bindParam(':uid',$_GET['delete_id']);
 $stmt_delete->execute(); 
 header("Location: index.php");
}
?>
<!DOCTYPE html>
<html>
<head>
 <title>Add, Edit, Delete, User Profile</title>
 <link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
 <script src="bootstrap/js/bootstrap.min.js"></script>
</head>
<body>
 <nav class="navbar navbar-default navbar-static-top">
  <div class="container-fluid">
   <div class="navbar-header">
    <a class="navbar-brand" href="https://www.tutorialswb.com/">TutorialsWB</a>
    <ul class="nav navbar-nav">
     <li class="active"><a href="index.php">Home</a></li>
    </ul>
   </div>
  </div>
 </nav>
 <div class="container">
  <div class="page-header">
   <h1 class="h2">&nbsp; List Users<a class="btn btn-success" href="addmember.php" style="margin-left: 770px;"><span class="glyphicon glyphicon-user"></span>&nbsp; Add Member</a></h1><hr>
  </div>
  <div class="row">
   <?php
   $stmt = $DB_con->prepare('SELECT userid, username, description, userprofile FROM users ORDER BY userid DESC');
   $stmt->execute();
   if($stmt->rowCount() > 0)
   {
    while($row=$stmt->fetch(PDO::FETCH_ASSOC))
    {
     extract($row);
     ?>
     <div class="col-xs-3">
      <h3 class="page-header" style="background-color:cadetblue" align="center"><?php echo $username."<br>".$description; ?></h3>
      <img src="uploads/<?php echo $row['userprofile']; ?>" class="img-rounded" width="250px" height="250px" /><hr>
      <p class="page-header" align="center">
       <span>
        <a class="btn btn-primary" href="editform.php?edit_id=<?php echo $row['userid']; ?>"><span class="glyphicon glyphicon-pencil"></span> Edit</a> 
        <a class="btn btn-warning" href="?delete_id=<?php echo $row['userid']; ?>" title="click for delete" onclick="return confirm('Are You Sure You Want To Delete This User?')"><span class="glyphicon glyphicon-trash"></span> Delete</a>
       </span>
      </p>
     </div>       
     <?php
    }
   }
   else
   {
    ?>
    <div class="col-xs-12">
     <div class="alert alert-warning">
      <span class="glyphicon glyphicon-info-sign"></span>&nbsp; No Data Found.
     </div>
    </div>
    <?php
   }
   ?>
  </div>
 </div>
</body>
</html> 

addmember.php
File ini berisi script untuk menambahkan user baru.

 <?php
error_reporting( ~E_NOTICE );
require_once 'dbcon.php';

if(isset($_POST['btnsave']))
{
 $username = $_POST['user_name'];
 $description = $_POST['description'];
 $imgFile = $_FILES['user_image']['name'];
 $tmp_dir = $_FILES['user_image']['tmp_name'];
 $imgSize = $_FILES['user_image']['size'];
 if(empty($username)){
  $errMSG = "Please Enter Username.";
 }
 else if(empty($description)){
  $errMSG = "Please Enter Your Description.";
 }
 else if(empty($imgFile)){
  $errMSG = "Please Select Image File.";
 }
 else
 {
  $upload_dir = 'uploads/';
  $imgExt = strtolower(pathinfo($imgFile,PATHINFO_EXTENSION));
  $valid_extensions = array('jpeg', 'jpg', 'png', 'gif');
  $userprofile = rand(1000,1000000).".".$imgExt;
  if(in_array($imgExt, $valid_extensions)){
   if($imgSize < 5000000)    {
    move_uploaded_file($tmp_dir,$upload_dir.$userprofile);
   }
   else{
    $errMSG = "Sorry, Your File Is Too Large To Upload. It Should Be Less Than 5MB.";
   }
  }
  else{
   $errMSG = "Sorry, only JPG, JPEG, PNG & GIF Extension Files Are Allowed.";  
  }
 }
 if(!isset($errMSG))
 {
  $stmt = $DB_con->prepare('INSERT INTO users(username,description,userprofile) VALUES(:uname, :udes, :upic)');
  $stmt->bindParam(':uname',$username);
  $stmt->bindParam(':udes',$description);
  $stmt->bindParam(':upic',$userprofile); 
  if($stmt->execute())
  {
   $successMSG = "Successfully Added A New Member.";
   header("refresh:1;index.php");
  }
  else
  {
   $errMSG = "Error While Creating.";
  }
 }
}
?>
<!DOCTYPE html>
<html>
<head>
 <title>Add, Edit, Delete, User Profile</title>
 <link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
 <script src="bootstrap/js/bootstrap.min.js"></script>
</head>
<body>
 <nav class="navbar navbar-default navbar-static-top">
  <div class="container-fluid">
   <div class="navbar-header">
    <a class="navbar-brand" href="https://www.tutorialswb.com/">tutorialsWB</a>
    <ul class="nav navbar-nav">
     <li class="active"><a href="index.php">Home</a></li>
    </ul>
   </div>
  </div>
 </nav>
 <div class="container">
  <div>
   <h1 class="h2">&nbsp; Add New User<a class="btn btn-success" href="index.php" style="margin-left: 850px"><span class="glyphicon glyphicon-home"></span>&nbsp; Back</a></h1><hr>
  </div>
  <?php
  if(isset($errMSG)){
   ?>
   <div class="alert alert-danger">
    <span class="glyphicon glyphicon-info-sign"></span> <strong><?php echo $errMSG; ?></strong>
   </div>
   <?php
  }
  else if(isset($successMSG)){
   ?>
   <div class="alert alert-success">
    <strong><span class="glyphicon glyphicon-info-sign"></span> <?php echo $successMSG; ?></strong>
   </div>
   <?php
  }
  ?>   

  <form method="post" enctype="multipart/form-data" class="form-horizontal" style="margin: 0 300px 0 300px;border: solid 1px;border-radius:4px">
   <table class="table table-responsive">
    <tr>
     <td><label class="control-label">Username</label></td>
     <td><input class="form-control" type="text" name="user_name" placeholder="Enter Username" value="<?php echo $username; ?>" /></td>
    </tr>
    <tr>
     <td><label class="control-label">Description</label></td>
     <td><input class="form-control" type="text" name="description" placeholder="Your Description" value="<?php echo $description; ?>" /></td>
    </tr>
    <tr>
     <td><label class="control-label">Profile Picture</label></td>
     <td><input class="input-group" type="file" name="user_image" accept="image/*" /></td>
    </tr>
    <tr>
     <td colspan="2" align="center"><button type="submit" name="btnsave" class="btn btn-primary"><span class="glyphicon glyphicon-floppy-save"></span>&nbsp; Save</button>
     </td>
    </tr>
   </table>
  </form>
 </div>
</body>
</html> 

editform.php
File ini berisi script untuk mengedit atau memperbaharui user yang sudah terdaftar.

 <?php
error_reporting( ~E_NOTICE );
require_once 'dbcon.php';

if(isset($_GET['edit_id']) && !empty($_GET['edit_id']))
{
 $id = $_GET['edit_id'];
 $stmt_edit = $DB_con->prepare('SELECT username, description, userprofile FROM users WHERE userid =:uid');
 $stmt_edit->execute(array(':uid'=>$id));
 $edit_row = $stmt_edit->fetch(PDO::FETCH_ASSOC);
 extract($edit_row);
}
else
{
 header("Location: index.php");
}
if(isset($_POST['btn_save_updates']))
{
 $username = $_POST['user_name'];
 $description = $_POST['description'];  
 $imgFile = $_FILES['user_image']['name'];
 $tmp_dir = $_FILES['user_image']['tmp_name'];
 $imgSize = $_FILES['user_image']['size'];
 if($imgFile)
 {
  $upload_dir = 'uploads/';
  $imgExt = strtolower(pathinfo($imgFile,PATHINFO_EXTENSION));
  $valid_extensions = array('jpeg', 'jpg', 'png', 'gif');
  $userprofile = rand(1000,1000000).".".$imgExt;
  if(in_array($imgExt, $valid_extensions))
  {   
   if($imgSize < 5000000)
   {
    unlink($upload_dir.$edit_row['userprofile']);
    move_uploaded_file($tmp_dir,$upload_dir.$userprofile);
   }
   else
   {
    $errMSG = "Sorry, Your File Is Too Large To Upload. It Should Be Less Than 5MB.";
   }
  }
  else
  {
   $errMSG = "Sorry, only JPG, JPEG, PNG & GIF Extension Files Are Allowed.";  
  } 
 }
 else
 {
  $userprofile = $edit_row['userprofile'];
 }
 if(!isset($errMSG))
 {
  $stmt = $DB_con->prepare('UPDATE users SET username=:uname, description=:udes, userprofile=:upic WHERE userid=:uid');
  $stmt->bindParam(':uname',$username);
  $stmt->bindParam(':udes',$description);
  $stmt->bindParam(':upic',$userprofile);
  $stmt->bindParam(':uid',$id);
  
  if($stmt->execute()){
   ?>
   <script>
    alert('Successfully Updated...');
    window.location.href='index.php';
   </script>
   <?php
  }
  else{
   $errMSG = "Sorry User Could Not Be Updated!";
  }
 }   
}
?>
<!DOCTYPE html>
<html>
<head>
 <title>Add, Edit, Delete, User Profile</title>
 <link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
 <script src="bootstrap/js/bootstrap.min.js"></script>
 <script src="jquery-1.11.3-jquery.min.js"></script>
</head>
<body>
 <nav class="navbar navbar-default navbar-static-top">
  <div class="container-fluid">
   <div class="navbar-header">
    <a class="navbar-brand" href="https://www.tutorialswb.com/">tutorialsWB</a>
    <ul class="nav navbar-nav">
     <li class="active"><a href="index.php">Home</a></li>
    </ul>
   </div>
  </div>
 </nav>
 <div class="container">
  <div>
   <h1 class="h2">&nbsp; Update Profile<a class="btn btn-success" href="index.php" style="margin-left: 850px"><span class="glyphicon glyphicon-home"></span>&nbsp; Back</a></h1><hr>
  </div>
  <form method="post" enctype="multipart/form-data" class="form-horizontal" style="margin: 0 300px 0 300px;border: solid 1px;border-radius:4px">
   <?php
   if(isset($errMSG)){
    ?>
    <div class="alert alert-danger">
     <span class="glyphicon glyphicon-info-sign"></span> &nbsp; <?php echo $errMSG; ?>
    </div>
    <?php
   }
   ?>
   <table class="table table-responsive">
    <tr>
     <td><label class="control-label">Username</label></td>
     <td><input class="form-control" type="text" name="user_name" value="<?php echo $username; ?>" required /></td>
    </tr>
    <tr>
     <td><label class="control-label">Description</label></td>
     <td><input class="form-control" type="text" name="description" value="<?php echo $description; ?>" required /></td>
    </tr>
    <tr>
     <td><label class="control-label">Profile Picture</label></td>
     <td>
      <p><img src="uploads/<?php echo $userprofile; ?>" height="150" width="150" /></p>
      <input class="input-group" type="file" name="user_image" accept="image/*" />
     </td>
    </tr>
    <tr>
     <td colspan="2" align="center">
      <button type="submit" name="btn_save_updates" class="btn btn-primary"><span class="glyphicon glyphicon-floppy-save"></span>&nbsp; Save</button>
      <a class="btn btn-warning" href="index.php"> <span class="glyphicon glyphicon-remove"></span>&nbsp; Cancel</a>
     </td>
    </tr>
   </table>
  </form>
 </div>
</body>
</html> 

jika kalian ingin melihat demonya silahkan klik button demo dibawah dan kalian juga bisa mendownload file di atas.

DEMO         DOWNLOAD

Sekian share kali ini semoga apa yang kami share bermanfaat bagi anda, jika kalian belum paham atau ingin request post atau project lain silahkan tinggalkan komentar dibawah dan mulai berdiskusi dengan kami.


Related Post

SUBSCRIBE TO OUR NEWSLETTER

closed