<?php

session_start();

$path_to_root = "";
include $path_to_root . 'database.php';

$db = new database();

$user_id = $_SESSION['user_id'];

$nama = strip_tags(trim($_POST['nama']));
$hp = strip_tags(trim($_POST['hp']));
$alamat = strip_tags(trim($_POST['alamat']));

if ($_FILES['gambar']['name'] == '') {
	$sql = "UPDATE `profil_user` SET `nama`='".$nama."', `hp`='".$hp."', `alamat`='".$alamat."' WHERE `id_user`=".$user_id;

	$result = $db->db_query($sql);

	if ($result) {
		header("location:index.php?menu=profil&alert=2");
	} else {
		header("location:index.php?menu=profil&alert=3");
	}
} else {
	$namaFile = $_FILES['gambar']['name'];
	$namaSementara = $_FILES['gambar']['tmp_name'];

	$dirUpload = "img-profil/";

	$terupload = move_uploaded_file($namaSementara, $dirUpload.$namaFile);

	if ($terupload) {
		$sql = "UPDATE `profil_user` SET `nama`='".$nama."', `hp`='".$hp."', `alamat`='".$alamat."', `foto`='".$namaFile."' WHERE `id_user`=".$user_id;

		$result = $db->db_query($sql);

		if ($result) {
			header("location:index.php?menu=profil&alert=2");
		} else {
			header("location:index.php?menu=profil&alert=3");
		}
	}
}

?>