diff --git a/src/controller/Controller.php b/src/controller/Controller.php index d417e614a59a86650e41636f57064e2f47d9bd09..205cb798fae9a4baee0cab36c533f8cf315b13c6 100644 --- a/src/controller/Controller.php +++ b/src/controller/Controller.php @@ -13,8 +13,14 @@ class DB { $dbpass = 'superadmin'; $dbname = 'db_dagojek'; - $this->_db = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass); - $this->_db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + try { + $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(){} diff --git a/src/controller/ProfilController.php b/src/controller/ProfilController.php index 194a90d17a9cf8f192c5ebbc644d534b225007ff..6c8e96a9789486eb2ac7458e2a762c78a6b9f178 100644 --- a/src/controller/ProfilController.php +++ b/src/controller/ProfilController.php @@ -57,7 +57,78 @@ class ProfilController { } 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 diff --git a/src/view/profil_edit.php b/src/view/profil_edit.php index 3ab9ac7312b3bf61b3f73ed3f1887c08ba2d0c95..5a7caf6e6e4224b5f04ab0486c219304d05bb909 100644 --- a/src/view/profil_edit.php +++ b/src/view/profil_edit.php @@ -15,7 +15,7 @@ </div> </div> <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="row"> <div class="col-6">