Skip to content
Snippets Groups Projects
Commit 8ae0a2d2 authored by Fadhil Imam Kurnia's avatar Fadhil Imam Kurnia
Browse files

Make edit profile handler

parent 3ceb4ad1
Branches twibbon
Tags
1 merge request!3Profil
...@@ -13,8 +13,14 @@ class DB { ...@@ -13,8 +13,14 @@ class DB {
$dbpass = 'superadmin'; $dbpass = 'superadmin';
$dbname = 'db_dagojek'; $dbname = 'db_dagojek';
$this->_db = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass); try {
$this->_db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $this->_db = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
$this->_db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->_db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
} catch (PDOException $e) {
echo "Connection fail :".$e->getMessage();
}
} }
private function __clone(){} private function __clone(){}
......
...@@ -57,7 +57,78 @@ class ProfilController { ...@@ -57,7 +57,78 @@ class ProfilController {
} }
public static function SaveProfil() { public static function SaveProfil() {
var_dump($_POST);
// Check sending data
if (!isset($_POST["name"]) || !isset($_POST["phone"]) || !isset($_POST["isDriver"]) || !isset($_GET['u'])) {
echo "Invalid data";
return;
}
$isPhotoUploaded = false;
if (isset($_FILES["photo"]) && strlen($_FILES["photo"]["name"]) != 0) {
$isPhotoUploaded = true;
}
$conn = DB::getInstance();
$user_id = simpleCrypt($_GET['u'], 'd');
$user_name = $_POST["name"];
$user_phone = $_POST["phone"];
$user_driver = $_POST["isDriver"] == "yes" ? 1 : 0;
$user_photo = isset($_FILES["photo"]) ? $_FILES["photo"]["name"] : null;
// Saving image if available
$stmt = $isPhotoUploaded ?
$conn->prepare('
UPDATE user
SET
name = :name,
phone = :phone,
is_driver = :isDriver,
photo = :photo
WHERE
id = :id
') :
$conn->prepare('
UPDATE user
SET
name = :name,
phone = :phone,
is_driver = :isDriver
WHERE
id = :id
');
if (!$stmt) {
print_r($conn->errorInfo());
}
$data = array(
':name' => $user_name,
':phone' => $user_phone,
':isDriver' => $user_driver,
':id' => $user_id,
);
if ($isPhotoUploaded) {
$data += array(
':photo' => $user_photo,
);
}
$stmt->execute($data);
if ($stmt === false) {
echo "Fail :";
echo $stmt->errorCode(). "<br>";
print_r($stmt->errorInfo());
} else {
header('Location: /main/profil?u='.$_GET['u']);
}
// Close connection
$stmt = null;
$conn = null;
} }
} }
\ No newline at end of file
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
</div> </div>
</div> </div>
<div class="row"> <div class="row">
<form action="/main/profil/edit/save" method="post"> <form action="/main/profil/edit/save?u=<?=$id?>" method="post" enctype="multipart/form-data">
<div class="container"> <div class="container">
<div class="row"> <div class="row">
<div class="col-6"> <div class="col-6">
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment