-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalter.php
63 lines (55 loc) · 1.46 KB
/
alter.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
<?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 = $no = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$kode_barang = safe_input($_POST["code"]);
$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("UPDATE `masterbarang`
SET
`NamaBarang` = ?,
`Kategori` = ?,
`Satuan` = ?,
`Jumlah` = ?,
`Harga` = ?
WHERE
`KodeBarang` = ?;");
$stmt->bind_param('sssiis', $nama_barang, $kategori, $satuan, $jumlah, $harga, $kode_barang);
if ($stmt->execute() === TRUE) {
echo("Data updated successfully");
} else {
http_response_code(500);
echo("Error: " . $q . "<br>" . $conn->error);
}
} 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;
}
?>