Postingan lainnya
Php & mysql database, sudah input data tetapi tidak tampil dan masuk ke database
Nama database: sparepart
Nama tabel: barang
Halaman index.
<?phprequire 'functions.php';
$barang = query ("SELECT * FROM barang");
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Halaman admin</title>
<style>
</style>
</head>
<body>
<h1>Database spare part</h1>
<a href="tambah.php"> tambah data</a>
<br><br>
<table border="1" cellspacing="0" cellpadding="10">
<tr>
<th> No.</th>
<th>Item code</th>
<th>Old Description</th>
<th>Long tex</th>
<th>Location</td>
<th>Qty sap</th>
<th> Uom</th>
<th> Mrp type </th>
<th>Max stock</th>
<th>Rop</th>
<th>safety stock</th>
<th>Planned delivery time</th>
<th>Price</th>
<th>Foto</th>
<th>Action </th>
</tr>
<?php $i=1 ; ?>
<?php foreach ($barang as $brg): ?>
<tr>
<td> <?= $i;?>
</td>
<td> <?= $brg["kode"]; ?> </td>
<td> <?= $brg["old"]; ?> </td>
<td> <?= $brg["longt"]; ?> </td>
<td> <?= $brg["loc"]; ?> </td>
<td> <?= $brg["qty"]; ?> </td>
<td> <?= $brg["uom"]; ?> </td>
<td> <?= $brg["mrp"]; ?> </td>
<td> <?= $brg["max"]; ?> </td>
<td> <?= $brg["rop"]; ?> </td>
<td> <?= $brg["safety"]; ?> </td>
<td> <?= $brg["delivery"]; ?> </td>
<td> <?= $brg["price"]; ?> </td>
<td> <img src="image/<?= $brg["foto"]; ?> " width="100"> </td>
<td>
<a href=""> Edit</a> |
<a href=""> Delete</a>
</td>
</tr>
<?php $i++ ;?>
<?php endforeach; ?>
</table>
</body></html>
Halaman tambah data:
<?php require 'functions.php';
// cek apakah tombol submit sudah pernah ditekan atau belum
if (isset($_POST ["submit"])) {
// cek apakah data berhasil di input atau tidak
if ( tambah ($_POST) >0 ) {
echo "
<script>
alert('data berhasil ditambahkan!');
document.location.href= 'index.php';
</script>
";
}else {
echo "
<script>
alert('data berhasil ditambahkan!');
document.location.href= 'index.php';
</script>
";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tambah data</title>
<style>
body {
background-color: #584d64;
}
form {
width:350px;
background-color:#5b743a;
padding: 30px;
margin: auto;
font-size:12px;
color: white;
}
input[type=text] {
width: 100%;
padding:5px;
font-size:18;
border:none;
border-radius:0 5px 0 0;
}
</style>
</head>
<body>
<form action="" method="post" >
<center>Insert data spare part</center>
<ul>
<li>
<label for="kode"> Item code: </label>
<input type="text" name="kode" id="kode" required>
</li>
<br>
<li>
<label for="old"> Old description: </label>
<input type="text" name="old" id="old">
</li>
<br>
<li>
<label for="longt"> Long text: </label>
<input type="text" name="longt" id="longt">
</li>
<br>
<li>
<label for="loc"> Location </label> <input type="text" name="loc" id="loc">
</li>
<br>
<li>
<label for="qty"> Qty SAP: </label>
<input type="text" name="qty" id="qty">
</li>
<br>
<li>
<label for="uom"> Uom: </label>
<input type="text" name="uom" id="uom">
</li>
<br>
<li>
<label for="mrp"> Mrp: </label>
<input type="text" name="mrp" id="mrp">
</li>
<br>
<li>
<label for="max"> Max: </label>
<input type="text" name="max" id="max">
</li>
<br>
<li>
<label for="rop"> Rop: </label>
<input type="text" name="rop" id="rop">
</li>
<br>
<li>
<label for="safety"> Safety: </label>
<input type="text" name="safety" id="safety">
</li>
<br>
<li>
<label for="delivery"> Delivery: </label>
<input type="text" name="delivery" id="delivery">
</li>
<br>
<li>
<label for="price"> Price: </label>
<input type="text" name="price" id="price">
</li>
<br>
<li>
<label for="foto"> Foto: </label>
<input type="text" name="foto" id="foto">
</li>
<br>
<button type="submit" name="submit"> Tambah data!</button>
<br><br>
</ul>
<a href="index.php"> Back to home </a>
</form>
</body>
</html>
Halaman Function:
<?php
// koneksi ke database isi: host, username, password, nama database
$conn = mysqli_connect("localhost", "root", "", "sparepart");
function query ($query) {
global $conn;
$result = mysqli_query($conn, $query);
$rows = [];
while ( $row = mysqli_fetch_assoc($result) ) {
$rows[] = $row ;
}
return $rows;
}
// supaya field tidak di susupi hacker tambahkan "htmlspecialchars"
function tambah ($data) {
global $conn;
$kode = htmlspecialchars( $data ["kode"]);
$old = htmlspecialchars ($data ["old"]);
$longt = htmlspecialchars($data ["longt"]);
$loc =htmlspecialchars ($data ["loc"]);
$qty =htmlspecialchars ($data ["qty"]);
$uom =htmlspecialchars ($data ["uom"]);
$mrp =htmlspecialchars ($data ["mrp"]);
$max =htmlspecialchars ($data ["max"]);
$rop =htmlspecialchars ($data ["rop"]);
$safety =htmlspecialchars ($data ["safety"]);
$delivery =htmlspecialchars ($data ["delivery"]);
$price =htmlspecialchars ($data ["price"]);
$foto = htmlspecialchars($data ["foto"]);
// query insert data
$query = "INSERT INTO barang
VALUES
('', '$kode', '$old', '$longt', '$loc', '$qty', '$uom', $mrp', '$max', '$rop', '$safety', '$delivery', '$price', '$foto')";
mysqli_query($conn, $query);
return mysqli_affected_rows($conn);
}
?>
2 Jawaban:
<div>The charm and exuberance that overruns with these women' presence can leave any person comatose, so they are an excellent settlement method. If you dedicate on your own to company, you can leave your clients amazed just with the visibility of among these angels, capitalizing and shutting the best deal of your life.<br><a href="https://www.freeblogclub.com">https://www.freeblogclub.com</a></div>
<div>pada</div><pre><form action="" method="post" ></pre><div>action dirubah ke halaman function, contoh:</div><pre><form action="function.php" method="post" ></pre><div><br>dan<br><br>coba tambahkan</div><pre><em>if</em> (isset($_POST['submit'])) { tambah($_POST); }</pre><div>sebelum fungsi tambah()<br><br>dan<br><br>pada halaman index<br>ada typo</div><pre><?phprequire 'functions.php';</pre><div>seharusnya</div><pre><?php require 'functions.php';</pre>