-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd.php
72 lines (64 loc) · 1.68 KB
/
add.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
69
70
71
72
<?php
// Prepare MySQL connection
$servername = "localhost";
$username = "root";
$password = "";
$conn = new mysqli($servername, $username, $password);
if ($conn->connect_error) {
http_response_code(500);
die("MySQL connection failed: " . $conn->connect_error . "<br>");
}
if ($conn->query("USE `assignment`;") === FALSE) {
http_response_code(500);
echo "Can't select!<br>" . $conn->error . "<br>";
}
// Get POST
$name = $price = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$nama_barang = safe_input($_POST["name"]);
$kategori = safe_input($_POST["cat"]);
$satuan = safe_input($_POST["unit"]);
$jumlah = intval(safe_input($_POST["amount"]));
$harga = intval(safe_input($_POST["price"]));
$stmt = $conn->prepare("INSERT
INTO `masterbarang`
(`KodeBarang`, `NamaBarang`, `Kategori`, `Satuan`, `Jumlah`, `Harga`)
VALUES (
IFNULL(
(SELECT
CONCAT('B', LPAD(CAST(CAST(RIGHT(`a`.`KodeBarang`, 3) AS UNSIGNED) + 1 AS CHAR), 3, '0'))
FROM `masterbarang` AS `a`
INNER JOIN `masterbarang` AS `b` ON `a`.`KodeBarang` = `b`.KodeBarang
ORDER BY `a`.`KodeBarang` DESC
LIMIT 1),
'B001'),
?,
?,
?,
?,
?
);");
$stmt->bind_param('sssii', $nama_barang, $kategori, $satuan, $jumlah, $harga);
if ($stmt->execute() === TRUE) {
echo("Data inserted successfully");
} else {
http_response_code(500);
echo("Error: " . $q . "<br>" . $conn->error);
}
$stmt->close();
} else {
http_response_code(405);
echo "POST request expected.";
}
$conn->close();
function safe_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if (ctype_space($data) || $data == '') {
http_response_code(400);
die('Bad Request');
}
return $data;
}
?>