允许用户编辑或更新HTML表

时间:2018-02-14 01:09:52

标签: php html mysql

我有这个html表,其中包含来自mysql的数据库信息:

pkg    | kod    | sek  | tahun  | makmal  | cat  | bil | Netbook |
Bidor  | aaa123 | aeu  | 2012   |   no    | book | 100 |    150  |  edit/delete

当用户点击右侧的href按钮编辑或删除时,用户可以编辑表格中的所有信息,但似乎我的代码无效。有人可以纠正我或教我,因为我还是初学者。

<?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>

用户点击表格右侧的编辑按钮后,这是我的代码。请帮助我,因为经过很多努力,我仍然无法使它发挥作用。

1 个答案:

答案 0 :(得分:0)

通过jquery选择器禁用页面加载时的所有文本输入,并使用编辑按钮功能再次启用它们。