Part II: Updating and Deleting of MySQL data using an OOP approach in PHP
Submitted by GeePee on Saturday, April 11, 2015 - 23:10.
This tutorial is a continuation of our previous tutorial called Part I Creating and Reading of MySQL Data using an OOP approach in PHP. Be sure to read it before continuing this tutorial.
All we need is add three PHP files called edit_user.php, process_edit_user.php and deleteuser.php.
Create a file called edit_user.php. and add the following code:
The main purpose of this code is used to display the selected data from “index.php”. And will ask for a new set of data based on user inputs. Then, on submission of form it will be redirected into “process_edit_user.php” for the updating new sets of data.
Next we need to add another PHP file called:process_edit_user.php. And here’s the following code:
And lastly we will create a new PHP file called: deleteuser.php And add the following code:
And this code will take effect based on the selection of a user from “index.php”.Through passing the id of specific user using URL.
Finally, we need to modify our “index.php”.
This line of code will only focus on linking and passing of data through url using the anchor tag.
Then you can now test your application.
- <?php
- /**
- * Description: edit user.
- * Author: Joken E. Villanueva
- * Date Created: ###
- * Date Modified: ###
- */
- require_once("includes/initialize.php");
- include_layout_template_public('header.php');
- ?>
- <div id="module-name">Edit User
- </div>
- <?php
- $id = $_GET['id'];
- $user = new User();
- $rec = $user->singleUser($id);
- ?>
- <div id="content">
- <form method="post" action="process_edit_user.php">
- <table class="app_listing" width="50%">
- <tr>
- <th > <div class="app_title" align="left"> User Details</div></th>
- </tr>
- <tr class="form">
- <td class="form">
- <table class="app_form">
- <tr>
- <td class="label" width="120">Name :: </td>
- <td>
- <input type="text" name="user_name" id="user_name" class="txtbox" value = "<?php echo $rec->users_name;?>"/>
- </td>
- </tr>
- <tr>
- <td class="label" width="120">Username :: </td>
- <td>
- <input type="text" name="uname" id="uname" class="txtbox" value = "<?php echo $rec->uname;?>"/>
- </td>
- </tr>
- <tr>
- <td class="label"width="120">Password :: </td>
- <td>
- <input type="password" name="upass" id="upass" class="txtbox" />
- </td>
- </tr>
- <tr>
- <td class="label"width="120">User Type :: </td>
- <td>
- <select name="utype" id="utype" >
- <option value="Administrator">Administrator</option>
- <option value="Registrar">Registrar</option>
- <option value="Teacher">Teacher</option>
- <option value="Student">Student</option>
- </select>
- </td>
- </tr>
- <tr>
- <td class="label"width="120"></td>
- <td>
- <input type="submit" name="submit" value="Save" class="app_button">
- </td>
- <td><input type="hidden" name="id" id="id" class="txtbox" value = <?php echo $rec->user_id?> /></td>
- </tr>
- </table>
- </tr>
- </table>
- </form>
- <?php
- /**
- * Description: Process edit user.
- * Author: Joken E. Villanueva
- * Date Created: ###
- * Date Modified:###
- */
- require_once("includes/initialize.php");
- include_layout_template_public('header.php');
- ?>
- <div id="module-name">Edit User
- </div>
- <?php
- $id = $_POST['id'];
- $pass = $_POST['upass'];
- $rec = new User();
- $rec->user_id = $id;
- $rec->users_name = $_POST['user_name'];
- $rec->uname = $_POST['uname'];
- $rec->utype = $_POST['utype'];
- $rec->update($id);
- ?>
- <script type="text/javascript">
- alert("User successfully updated!");
- window.location = "index.php";
- </script>
- <?php
- /**
- * Description: Delete user.
- * Author: Joken E. Villanueva
- * Date Created: May 24,2013
- * Date Modified:June 6, 2013
- */
- require_once("includes/initialize.php");
- include_layout_template_public('header.php');
- ?>
- <div id="module-name">List of user
- </div>
- <div id="content">
- <?php
- $id = $_GET['id'];
- $user = new User();
- $user->delete($id);
- ?>
- <script type="text/javascript">
- alert("User has successfully deleted!");
- window.location = "index.php";
- </script>
- echo '<a href="edit_user.php?id='.$object->user_id.'" class="app_listitem_key">[Edit Entry]</a>';
- echo '<a href="deleteuser.php?id='.$object->user_id.'" class="app_listitem_key">[Delete Entry]</a>';
Add new comment
- 28 views