I have this html table which contain database information from mysql :
pkg | kod | sek | tahun | makmal | cat | bil | Netbook |
Bidor | aaa123 | aeu | 2012 | no | book | 100 | 150 | edit/delete
When user click the href
button edit or delete on the right side, user can edit all the information in the table but seems my code not working. Can someone correct me or teach me as I am still a beginner.
<?php
// including the database connection file
session_start();
include("connections.php");
if(isset($_POST['update']))
{
$bil = $_POST['bil'];
$pkg=$_POST['pkg'];
$kodsk=$_POST['kodsk'];
$namask=$_POST['namask'];
$tahun=$_POST['tahun'];
$makmal=$_POST['makmal'];
$catatan=$_POST['catatan'];
$murid=$_POST['murid'];
$netbook=$_POST['netbook'];
// checking empty fields
if(empty($pkg) || empty($kodsk) || empty($namask) || empty($tahun) || empty($makmal) || empty($catatan) || empty($murid) || empty($netbook))
{
if(empty($pkg)) {
echo "<font color='red'>Name field is empty.</font><br/>";
}
if(empty($kodsk)) {
echo "<font color='red'>Age field is empty.</font><br/>";
}
if(empty($namask)) {
echo "<font color='red'>Email field is empty.</font><br/>";
}
if(empty($tahun)) {
echo "<font color='red'>Email field is empty.</font><br/>";
}
if(empty($makmal)) {
echo "<font color='red'>Email field is empty.</font><br/>";
}
if(empty($catatan)) {
echo "<font color='red'>Email field is empty.</font><br/>";
}
if(empty($murid)) {
echo "<font color='red'>Email field is empty.</font><br/>";
}
if(empty($netbook)) {
echo "<font color='red'>Email field is empty.</font><br/>";
} else
{
//updating the table
$sql = "UPDATE data2017 SET pkg=:pkg, kodsk=:kodsk, namask=:namask, tahun=:tahun, makmal=:makmal, catatan=:catatan, murid=:murid, netbook=:netbook WHERE bil=:bil";
$query = $dbConn->prepare($sql);
$query->bindparam(':bil', $bil);
$query->bindparam(':pkg', $pkg);
$query->bindparam(':kodsk', $kodsk);
$query->bindparam(':namask', $namask);
$query->bindparam(':tahun', $tahun);
$query->bindparam(':makmal', $makmal);
$query->bindparam(':catatan', $catatan);
$query->bindparam(':murid', $murid);
$query->bindparam(':netbook', $netbook);
$query->execute();
header("Location: 2017.php");
}
}
}
?>
<?php
$bil = $_GET['bil'];
//selecting data associated with this particular id
$sql = "SELECT * FROM data2017 WHERE bil=:bil";
$query = $connect->prepare($sql);
$query->execute(array(':bil' => $bil));
while($row = $query->fetch(PDO::FETCH_ASSOC))
{
$pkg = $row['pkg'];
$kodsk = $row['kodsk'];
$namask = $row['namask'];
$tahun=$row['tahun'];
$makmal=$row['makmal'];
$catatan=$row['catatan'];
$murid=$row['murid'];
$netbook=$row['netbook'];
}
?>
<html>
<head>
<title>Edit Data</title>
</head>
<body>
<a href="2017.php">Home</a>
<br/><br/>
<form name="form1" method="post" action="update2017.php">
<table border="0">
<tr>
<td>pkg</td>
<td><input type="text" name="pkg" value="<?php echo $pkg;?>"></td>
</tr>
<tr>
<td>kodsk</td>
<td><input type="text" name="kodsk" value="<?php echo $kodsk;?>"></td>
</tr>
<tr>
<td>sek</td>
<td><input type="text" name="namask" value="<?php echo $namask;?>"></td>
</tr>
<tr>
<td>tahun</td>
<td><input type="text" name="tahun" value="<?php echo $tahun;?>"></td>
</tr>
<tr>
<td>makmal</td>
<td><input type="text" name="makmal" value="<?php echo $makmal;?>"></td>
</tr>
<tr>
<td>catatan</td>
<td><input type="text" name="catatan" value="<?php echo $catatan;?>"></td>
</tr>
<tr>
<td>murid</td>
<td><input type="text" name="murid" value="<?php echo $murid;?>"></td>
</tr>
<tr>
<td>netbook</td>
<td><input type="text" name="netbook" value="<?php echo $netbook;?>"></td>
</tr>
<tr>
<td><input type="hidden" name="bil" value=<?php echo $_GET['bil'];?>></td>
<td><input type="submit" name="update" value="Update"></td>
</tr>
</table>
</form>
</body>
</html>
This is my code after user click the edit button on the right side of table. please help me as I still fail to make it works after a lot of struggle.