-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprofile.php
More file actions
112 lines (100 loc) · 3.72 KB
/
Copy pathprofile.php
File metadata and controls
112 lines (100 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
include "koneksi.php";
$username = $_SESSION["username"];
$sql = "SELECT * FROM user WHERE username='$username'";
$hasil = $conn->query($sql);
$row = $hasil->fetch_assoc();
?>
<div class="container">
<div class="row">
<div class="table-responsive" id="">
<form method="post" action="" enctype="multipart/form-data">
<div class="modal-body">
<div class="mb-3">
<label for="formGroupExampleInput" class="form-label">Ganti Password</label>
<input type="hidden" name="id" value="<?= $row['id'] ?>">
<input type="text" class="form-control" name="password" placeholder="Tuliskan Password baru anda">
</div>
<div class="mb-3">
<label for="formGroupExampleInput2" class="form-label">Ganti Foto Profil</label>
<input type="file" class="form-control" name="gambar">
</div>
<div class="mb-3">
<label for="formGroupExampleInput3" class="form-label">Foto Profil Saat Ini</label>
<?php
if ($row["foto"] != '') {
if (file_exists('img/' . $row["foto"] . '')) {
?>
<br><img src="img/<?= $row["foto"] ?>" width="100">
<?php
}
}
?>
<input type="hidden" name="gambar_lama" value="<?= $row["foto"] ?>">
</div>
</div>
<input type="submit" value="simpan" name="simpan" class="btn btn-primary">
</form>
</div>
</div>
</div>
<?php
include "upload_foto.php";
//jika tombol simpan diklik
if (isset($_POST['simpan'])) {
$password = md5($_POST['password']);
//$username = $_SESSION['username'];
$gambar = '';
$nama_gambar = $_FILES['gambar']['name'];
//jika ada file yang dikirim
if ($nama_gambar != '') {
//panggil function upload_foto untuk cek spesifikasi file yg dikirimkan user
//function ini memiliki 2 keluaran yaitu status dan message
$cek_upload = upload_foto($_FILES["gambar"]);
//cek status true/false
if ($cek_upload['status']) {
//jika true maka message berisi nama file gambar
$gambar = $cek_upload['message'];
} else {
//jika true maka message berisi pesan error, tampilkan dalam alert
echo "<script>
alert('" . $cek_upload['message'] . "');
document.location='admin.php?page=profile';
</script>";
die;
}
}
//cek apakah ada id yang dikirimkan dari form
if (isset($_POST['id'])) {
//jika ada id, lakukan update data dengan id tersebut
$id = $_POST['id'];
if ($nama_gambar == '') {
//jika tidak ganti gambar
$gambar = $_POST['gambar_lama'];
} else {
//jika ganti gambar, hapus gambar lama
unlink("img/" . $_POST['gambar_lama']);
}
$stmt = $conn->prepare("UPDATE user
SET
password = ?,
foto = ?
WHERE id = ?");
$stmt->bind_param("ssi", $password, $gambar, $id);
$simpan = $stmt->execute();
}
if ($simpan) {
echo "<script>
alert('Simpan data sukses');
document.location='admin.php?page=profile';
</script>";
} else {
echo "<script>
alert('Simpan data gagal');
document.location='admin.php?page=profile';
</script>";
}
$stmt->close();
$conn->close();
}
?>