This post explains how to build a CRUD (Create-Read-Update-Delete) procedures in PHP and MySQL using PDO (PHP Data Objects). PDO is a PHP advance version that implements an intermediate to using MySQL databases in PHP. PDO is compact and strong. There are several great concepts of PDO. The greatest one is, it’s cross-database compatible. You don’t need to change your query function if you switch database server for your project. PDO extension helps different databases like MS SQL, MySQL, Oracle, SQLite, etc.
Error handling is another greater benefit of PDO extension. Using this we can able to write a code with try/catch block. It kept error logs as a file and user-friendly messages are displayed on a page. PDO also help to use prepared statements and stored methods. The important advantage of prepared statements is that you just need to prepare a query once and we can use many times with the same or different parameters.
In this post, we design the HTML Form with update operation in CRUD procedure using PDO connection. Here we just get the data and update it then stored into a database with the help of PDO connection object.
Step 1: Create a PDO connection file with database connection details and link with created database table.
1 2 3 4 5 6 7 |
<?php $DB_host = 'localhost'; $DB_username = 'root'; $DB_password = ''; $DB_name = 'li_demo'; $pdo_conn = new PDO( 'mysql:host='.$DB_host.';dbname='.$DB_name, $DB_username, $DB_password ); ?> |
Step 2: Make an HTML Form with required input fields and set the action file path and request method name. After fetching the selected row details from a database with a help of primary key id value and set the retrieved value as input boxes with edit id.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<?php $post_id = $_REQUEST['post_id']; $pdo_statement = $pdo_conn->prepare("SELECT * FROM li_ajax_post_load where post_id=" . $post_id); $pdo_statement->execute(); $result = $pdo_statement->fetchAll(); ?> <form action="" method="POST"> <input type="hidden" name="post_id" value="<?php echo $result[0]['post_id']; ?>"> <div class="form-group"> <label for="post_name" class="required" >Post name:</label> <input type="text" class="form-control" id="post_name" name="post_name" required value="<?php echo $result[0]['post_title']; ?>" /> </div> <div class="form-group"> <label for="message" class="required" >Post Description:</label> <textarea type="text" class="form-control" id="post_description" name="post_description" required ><?php echo $result[0]['post_desc']; ?></textarea> </div> <div class="clearfix"></div> <div class="form-action-group"> <button type="submit" name="post_comment" class="btn btn-primary btn-form-action">Submit</button> <button type="reset" class="btn btn-danger btn-form-action btn-reset">Clear</button> </div> </form> |
Step 3: Write PHP update procedure with updated post value into a database with help of primary key edit id value.
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php if(isset($_POST["post_comment"])) { $pdo_statement=$pdo_conn->prepare("update li_ajax_post_load set post_title='" . $_POST[ 'post_name' ] . "', post_desc='" . $_POST[ 'post_description' ]. "' where post_id=" . $_POST["post_id"]); $result = $pdo_statement->execute(); if (!empty($result) ){ echo '<div class="alert alert-success">Data saved successfully.</div>'; } else{ echo '<div class="alert alert-danger">Data saving failed.</div>'; } } ?> |