5. Data Edit

1.You have to use two methods one is data fetch and second is data update.
obviously have to put this code in fresh.php. where have your database connect.

/* Data fetch from database */

    public function fetch($table, $id) {
        $query = mysqli_query($this->connect, "SELECT * FROM $table WHERE id=$id");
        if (mysqli_num_rows($query) > 0) {
            return mysqli_fetch_assoc($query);
        } else {
            return false;
        }
    }

    /* Data update */

    public function Update($table, $id, $name, $email, $phone, $address, $edit_id) {
        $query = mysqli_query($this->connect, "UPDATE $table SET id='$id',name='$name',email='$email',phone='$phone',address='$address' WHERE id=$edit_id ");
        if (mysqli_affected_rows($this->connect) > 0) {
            return true;
        } else {
            return false;
        }
    }

2.Create edit.php file and set these following code.

<?php
include_once("fresh.php");
/* data fetch by id */
if (isset($_REQUEST['id'])) {
    //echo "<pre>";
    //print_r($_REQUEST);
    extract($obj->fetch("members", $_REQUEST['id']));
}

if (isset($_REQUEST['edit_id'])) {
    //echo "<pre>";
    //print_r($_REQUEST);
    extract($_REQUEST);
    if ($obj->Update("members", $id, $name, $email, $phone, $address, $edit_id)) {
        header('location:tabulize_data.php');
    } else {
        $err_msg = "Update Fail";
    }
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Show Database</title>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
    </head>

    <body>

        <div style="width: 700px; margin: 0 auto">   
            <?php
            echo(isset($err_msg)) ? $err_msg : "";
            ?>
            <form>
                <table class="table table-bordered table-hover" style="text-align: center;">  
                    <tr>
                        <td colspan="2">Insert Your Information</td>
                    </tr>
                    <tr>
                        <td>Id</td>
                        <td><input name="id"class="form-control" type="text" value="<?php echo @$id; ?>" ></td>
                    </tr>
                    <tr>
                        <td>Name</td>
                        <td><input name="name" class="form-control" type="text" value="<?php echo @$name; ?>" placeholder="Enter Your Name" required="required"></td>
                    </tr>
                    <tr>
                        <td>E-mail</td>
                        <td><input name="email" class="form-control" type="text" value="<?php echo @$email; ?>" placeholder="Enter Your valid Email" required="required"></td>
                    </tr>
                    <tr>
                        <td>Phone</td>
                        <td><input name="phone" class="form-control" type="text" value="<?php echo @$phone; ?>" placeholder="Enter Your Number" required="required"></td>
                    </tr>
                    <tr>
                        <td>Address</td>
                        <td><textarea name="address" class="form-control"><?php echo @$address; ?></textarea></td>
                    </tr>  
                    <tr>
                        <td colspan="2" align="center">
                            <!-- need hidden id -->
                            <input type="hidden" name="edit_id" value="<?php echo $id; ?>" />
                            <input type="submit" value="Update" name="submit" class="btn btn-primary">
                        </td>
                    </tr>
                </table>
            </form>
        </div>
    </body>
</html>

Output following screenshot
edit

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s