How to update data from a MySQL database using PHP

How to update data from a MySQL database using PHP

In this tutorial, we will focus on learning how to update data from a MySQL database using PHP. Whether you are a beginner or an experienced developer, this guide will provide you with a comprehensive understanding of the process of fetching and updating data from a database using PHP. After this tutorial, you will be able to update any type of data in the database using PHP. This tutorial is a step-by-step guide for updating data in a database using PHP.

Step 1: Creating a file for database connection

<?php
$conn= mysqli_connect("localhost", "root","","database_name");
If(!$conn){
Alert('Database connection error');
?>

Step 2: Fetching data to display

<?php
include_once 'db.php';

?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>Bootstrap 4 Bordered Table</title>
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto|Varela+Round|Open+Sans">
    <link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function() {
            $('[data-toggle="tooltip"]').tooltip();
        });
    </script>
</head>

<body>
    <div class="bs-example">
        <div class="container">
            <div class="row">
                <div class="col-md-12">
                    <div class="page-header clearfix">
                        <h2 class="pull-left">Posts List</h2>
                    </div>
                    <?php
                    $result = mysqli_query($conn, 'SELECT * FROM posts');
                    ?>
                    <?php
                        if (mysqli_num_rows($result) > 0) {
                    ?>
                    <table class='table table-bordered table-striped'>
                        <tr>
                            <td>ID</td>
                            <td>Post Title</td>
                            <td>Post Auther</td>
                            <td>Category</td>
                            <td>Content</td>
                            <td>Action</td>
                        </tr>
                        <?php
                            $i=0;
                            while($row = mysqli_fetch_array($result)) {
                        ?>
                        <tr>
                            <td><?php echo $row['id']; ?></td>
                            <td><?php echo $row['title']; ?></td>
                            <td><?php echo $row['author']; ?></td>
                            <td><?php echo $row['category']; ?></td>
                            <td><?php echo $row['content']; ?></td>
                            <td><a href="update-action.php?postid=<?php echo $row["id"]; ?>">Update Post</a></td>
                        </tr>
                        <?php
                            $i++;
                            }
                        ?>
                    </table>
                    <?php
                        }else{
                            echo "No records fund!";
                        }
                    ?>
                </div>
            </div>
        </div>
    </div>
</body>

</html>

Step 3: Updating Data

<?php
    require_once "db.php";

    if(count($_POST)>0) {
        mysqli_query($conn,"UPDATE posts set  title='" . $_POST['title'] . "', author='" . $_POST['author'] . "' ,category='" . $_POST['category'] . "' ,content='" . $_POST['content'] . "' WHERE id='" . $_POST['id'] . "'");
        $message = "Record updated Successfully";
    }

    $result = mysqli_query($conn,"SELECT * FROM posts WHERE id='" . $_GET['postid'] . "'");
    $row= mysqli_fetch_array($result);

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Update post</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">

</head>
<body>
    <div class="wrapper">
        <div class="container-fluid">
            <div class="row">
                <div class="col-md-12">
                    <div class="page-header">
                        <h2>Update post</h2>
                    </div>

                    <form action="<?php echo htmlspecialchars(basename($_SERVER['REQUEST_URI'])); ?>" method="post">
                        <div class="form-group">
                            <label>Post Title</label>
                            <input type="text" name="title" class="form-control" value="<?php echo $row["title"]; ?>">
                        </div>
                        <div class="form-group ">
                            <label>Post Author</label>
                            <input type="text" name="author" class="form-control" value="<?php echo $row["author"]; ?>">
                        </div>
                        <div class="form-group">
                            <label>Post Category</label>
                            <input type="text" name="category" class="form-control" value="<?php echo $row["caregory"]; ?>">
                        </div>
                        <div class="form-group">
                            <label>Post Content</label>
                            <textarea name="content" class="form-control" id="" cols="30" rows="10">
                                <?php echo $row["caregory"]; ?>
                            </textarea>

                        </div>
                        <input type="hidden" name="id" value="<?php echo $row["id"]; ?>"/>
                        <input type="submit" class="btn btn-primary" value="Submit">
                        <a href="users.php" class="btn btn-default">Cancel</a>
                    </form>
                </div>
            </div>
        </div>
    </div>
</body>
</html>

I hope this tutorial is helpful for you.

Comments

No Comment posted Yet!

Leave a Reply

OK! You can skip this field.