Skip to content
Snippets Groups Projects
edit-profile.php 2.08 KiB
<?php


/* Model */
include "../model/user.php";
/* GET */
if(!$_POST) {
    $user = new User;
    $user_id = $_GET['id'];
    $result = $user->getUserById($user_id);
    header('Content-Type: text/xml');
    $xml = '<user>';

    while($row = mysqli_fetch_array($result)){
        $xml = $xml . '<id>' . $user_id . '</id><name>' . $row['name'] . '</name><image>' . $row['image'] .
            '</image><phone>' . $row['phone'] . '</phone><driver>' . $row['isDriver'] . '</driver>';
    }
    $xml = $xml . '</user>';

    print $xml;
}

/* POST */

if(isset($_POST["submit"])) {
    $user = new User;
    $userid = $_GET['id'];
    $isDriver = 0;
    $image = "";
    $name = "";
    $phone = "";
    $result = $user -> getUserById($userid);
    while($row = mysqli_fetch_array($result)) {
        $name = $row['name'];
        $phone = $row['phone'];
        $image = $row['image'];
        $isDriver = $row['isDriver'];
    }
    if(isset($_POST['name'])) {
        $name = $_POST['name'];
    }
    if(isset($_POST['phone'])) {
        $phone = $_POST['phone'];
    }
    if(file_exists($_FILES["image-file"]["tmp_name"])) {
        $targetDir = "../img/";
        $targetFile = $targetDir . basename($_FILES["image-file"]["name"]);
        $uploadOK = 1;
        $imageFileType = pathinfo($targetFile,PATHINFO_EXTENSION);
        $check = getimagesize($_FILES["image-file"]["tmp_name"]);
        if($check !== false) {
            $uploadOK = 1;
        } else {
            $uploadOK = 0;
        }
        if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
            && $imageFileType != "gif" ) {
            $uploadOk = 0;
        }
        if($uploadOK == 1) {
            if (move_uploaded_file($_FILES["image-file"]["tmp_name"], $targetFile)) {
                $image = basename($_FILES["image-file"]["name"]);
            }
        }
    }
    if(isset($_POST['driver'])) {
        $isDriver = 1;
    } else {
        $isDriver = 0;
    }
    $user->updateProfileById($userid, $name, $phone, $isDriver, $image);
    header('Location: ../view/edit-profile.php?id=' . $userid);

}