-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate.php
68 lines (63 loc) · 2.34 KB
/
create.php
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
<?php
require("mysql_connection.php");
require("global_vars.php");
$conn = getDBConnection();
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$judul = $_POST["judul"];
$subjudul = $_POST["subjudul"];
$ringkasan = $_POST["ringkasan"];
$isi = $_POST["isi"];
$penulis = $_POST["penulis"];
$gambar = "";
if ($_FILES["gambar"]["name"] != "") {
$temp = explode(".", basename($_FILES["gambar"]["name"]));
$newfilename = date('dmYHis') . "." . $temp[count($temp) - 1];
$target_file = $images_dir . $newfilename;
move_uploaded_file($_FILES["gambar"]["tmp_name"], $target_file);
$gambar = $newfilename;
}
$sql = "INSERT INTO artikel (judul, subjudul, ringkasan, isi, penulis, gambar)
VALUES ('{$judul}', '{$subjudul}', '{$ringkasan}', '{$isi}', '{$penulis}', '{$gambar}')";
if ($conn->query($sql)) {
echo "<script>window.alert('Berhasil menambahkan artikel.');
window.location.href='/';</script>";
} else {
echo "Gagal menambahkan artikel: {$conn->error}";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>CRUD Artikel | Tambah</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<h1>Tambah Artikel</h1>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" enctype="multipart/form-data">
<label>Judul</label>
<input type="text" name="judul" size="100" maxlength="200" required>
<label>Subjudul</label>
<input type="text" name="subjudul" size="100" maxlength="150">
<label>Ringkasan</label>
<textarea name="ringkasan" rows="5" cols="100"></textarea>
<label>Isi</label>
<textarea name="isi" rows="15" cols="100" required></textarea>
<label>Penulis</label>
<select name="penulis" required>
<?php
foreach ($authors as $key => $value) {
echo "<option value='{$key}'>{$value}</option>";
}
?>
</select>
<label>Gambar</label>
<input type="file" name="gambar" accept="image/*">
<button class="btn" type="submit">Tambah</button>
</form>
</body>
</html>
<?php closeConnection($conn); ?>