-2

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.

Paul Chu
  • 1,249
  • 3
  • 19
  • 27
Dikwan
  • 13
  • 1
  • 4
  • 1
    What does 'not working' mean? What's the specific error message or unexpected behaviour? – Shadow Feb 14 '18 at 01:31
  • after click the edit button on the right side of the table, it should let user edit all the information in the table. Sadly after click the edit, it not allow me to edit. – Dikwan Feb 14 '18 at 01:35
  • 1
    Then do some debugging on both php and sql level first. – Shadow Feb 14 '18 at 01:39
  • can u correct my code start from , if not mistaken that is the problem occur. – Dikwan Feb 14 '18 at 01:50
  • 1
    No, I can't because you cannot specify what the error is, so I do not even know where to look. You need to do some basic debugging to narrow down where things go south. – Shadow Feb 14 '18 at 01:58
  • oright got it, Notice: Undefined index: bil at 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']; } ?> – Dikwan Feb 14 '18 at 02:19
  • You specifically need the following answer from the duplicate question: https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php/12770836#12770836 – Shadow Feb 14 '18 at 06:08
  • how do i define the index. need to declare the index bil ? – Dikwan Feb 14 '18 at 06:29

1 Answers1

0

Disable all text input on page load through jquery selector and make function for edit button to enable them again.