Update Record File in PHP

 <?php include 'header.php'; ?>

<div id="main-content">
    <h2>Edit Record</h2>
    <form class="post-form" action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
        <div class="form-group">
            <label>Id</label>
            <input type="text" name="sid" />
        </div>
        <input class="submit" type="submit" name="showbtn" value="Show" />
    </form>

    <?php
    if (isset($_POST["showbtn"])) {
        include "config.php";

        $stu_id = $_POST["sid"];

        $sql = "SELECT * FROM student WHERE sid = {$stu_id}";
        $result = $conn->query($sql);

        if ($result->num_rows > 0) {
            while ($row = $result->fetch_assoc()) {

    ?>

                <form class="post-form" action="updatedata.php" method="post">
                    <div class="form-group">
                        <label for="">Name</label>
                        <input type="hidden" name="sid" value="<?php echo $row["sid"]; ?>" />
                        <input type="text" name="sname" value="<?php echo $row["sname"]; ?>" />
                    </div>
                    <div class="form-group">
                        <label>Address</label>
                        <input type="text" name="saddress" value="<?php echo $row["saddress"]; ?>" />
                    </div>
                    <div class="form-group">
                        <label>Class</label>
                        <?php
                        $sql1 = "SELECT * FROM studentclass";
                        $result1 = $conn->query($sql1);

                        if ($result1->num_rows > 0) {
                            echo '<select name="sclass">';
                            while ($row1 = $result1->fetch_assoc()) {
                                if ($row["sclass"] == $row1["cid"]) {
                                    $select = "selected";
                                } else {
                                    $select = "";
                                }
                                echo "<option {$select} value='{$row1['cid']}'>{$row1['cname']}</option>";
                            }
                            echo "</select>";
                        } else {
                            echo "No Record Found";
                        }
                        $conn->close();
                        ?>
                    </div>
                    <div class="form-group">
                        <label>Phone</label>
                        <input type="text" name="sphone" value="<?php echo $row["sphone"]; ?>" />
                    </div>
                    <input class="submit" type="submit" value="Update" />
                </form>
    <?php
            }
        } else {
            echo "No Record Available for id={$_POST['sid']}";
        }
    }
    ?>
</div>
</div>
</body>

</html>

Comments

Popular posts from this blog

Index File in PHP

Edit OR Update Data File in PHP