Postingan lainnya
Input data nama dan alamat tapi ga masuk ke database
Jadi gini, saya bingung kenapa pas input nama dan alamat ga mau masuk kedalam database, padahal menurut saya udah bener, tolong gan dibantu, saya pemula gan. nama database 'crud' dan di bagian 'info' lalu ada 3 kolom yaitu 'id' , 'name', 'address'.
index.php:
<?php include('server.php'); ?>
<!DOCTYPE html>
<html>
<head>
<title>CRUD</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<?php if (isset($_SESSION['msg'])); ?>
<div class="msg">
<?php
echo $_SESSION['msg'];
unset($_SESSION['msg']);
?>
</div>
<?php endif ?>
<table>
<thead>
<tr>
<th>Name</th>
<th>Adress</th>
<th coolspan="2">Action</th>
</tr>
</thead>
<tbody>
<?php while ($row = mysqli_fetch_array($results)) { ?>
<tr>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['address']; ?></td>
<td>
<a href="#">Edit</a>
</td>
<td>
<a href="#">Delete</a>
</td>
</tr>
<?php } ?>
</tbody>
</table>
<form method="post" action="server.php">
<div class="input-group">
<label>Name</label>
<input type="text" name="name">
</div>
<div class="input-group">
<label>Address</label>
<input type="text" name="address">
</div>
<div class="input-group">
<button type="submit" name="save" class="btn">Save</button>
</div>
</form>
</body>
</html>
server.php:
<?php
// initialize variables
$name = "";
$address = "";
$id = 0;
// connect to database
$db = mysqli_connect('localhost', 'root','', 'crud');
// if save button is clicked
if (isset($_POST['save'])) {
$name = $_POST['name'];
$address = $_POST['address'];
$query = "INSERT INTO info (name, address) VALUES ('$name', '$address')";
mysql_query($db, $query);
$_SESSION['msg'] = "Address saved"
header('location: index.php'); // redirect to index page aster insert
}
// retreive records
$results = mysqli_query($db, "SELECT * FROM info");
?>
style.css ( jika perlu):
body {
font-size: 19px;
}
table {
width: 50%;
margin: 30px auto;
border-collapse: collapse;
text-align: left;
}
tr {
border-bottom: 1px solid #cbcbcb;
}
th, td{
border: none;
height: 30px;
padding: 2px;
}
tr:hover {
background: #F5F5F5;
}
form {
width:45%;
margin: 50px auto;
text-align: left;
padding: 20px;
border: 1px solid #bbbbbbb;
border-radius: 5px;alignment-baseline:
}
.input-group {
margin: 10px 0px 10px 0px;
}
.input-group label {
display: block;
text-align: left;
margin: 3px;
}
.input-group input{
height: 30px;
width: 93%;
padding: 5px 10px;
font-size: 16px;
border-radius: 5px;
border: 1px solid gray;
}
.btn {
padding: 10px;
font-size: 15px;
color: white;
background: #5F9EA0;
border: none;
border-radius: 5px;
}
.msg {
margin: 30px auto;
padding: 10px;
border-radius: 5px;
color: #3c763d;
background: #dff068;
border: 1px solid #3c763d;
width: 50%;
text-align: center;
}
4 Jawaban:
coba hapus yg ini gan // initialize variables $name = ""; $address = ""; $id = 0;
trus di bagian values nya gk usahh di kasih kutip dirubah aja jadi ($name, $address)
udah bener bro cuma di bagian mysql_query($db, $query); ganti itu kurang huruf i jadi mysqli, jadi gini: mysqli_query($db, $query);
btw itu gak eror bro, kode ente tadi gw kopas bnyak yg kurang ";"
iyaa soalnya aga dari pertama udah mendefiniskan memakai koneksi dengan metode mysqli sedangkan agan execute querynya masih pake metode mysql_query, jadi kalo di awal udah sepakat koneksinya pake mysqli kesananya harus pake metode yg cocok juga untuk mysqli