Simple Edit Data Using PHP and MYSQL [Responsive Page]

0
186

Hello! Good day everyone. Today, I will present the ” Simple Edit Data Using PHP and MYSQL [Responsive Page] “ to enable beginners to solve when they encounter “Deprecated: mysql_connect(): The MySQL extension is deprecated and will be removed in the future: use mysqli or PDO”. We use MYSQLi in constructing our SQL.

  • First, create a database, name it as “simple”.
  1. CREATE TABLE <code>names</code> ( <code>name_id</code> int(11) NOT NULL, <code>first_name</code> text NOT NULL, <code>middle_name</code> text NOT NULL, <code>last_name</code> text NOT NULL ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
  • Create a “connection.php” file. Then put the following codes.
  1. &lt;?php
  2. $mysqli = new mysqli('localhost', 'root', '', 'simple');
  3. ?&gt;
  • On the “index.php” file, put the following codes.
  1. &lt;?php
  2. include 'connection.php';
  3. ?&gt;
  4. &lt;!DOCTYPE html&gt;
  5. &lt;html&gt;
  6. &lt;head&gt;
  7. &lt;meta charset="UTF-8"&gt;
  8. &lt;title&gt;Edit Data From Database&lt;/title&gt;
  9. &lt;/head&gt;
  10. &lt;body&gt;
  11. &lt;?php
  12. /* Show data from the database */
  13. $data = $mysqli-&gt;query("SELECT * FROM names");
  14. while ($name_data = $data-&gt;fetch_assoc()) {
  15. echo $name_data['first_name'] . ' ' . $name_data['middle_name'] . ' ' . $name_data['last_name'] . ' &lt;a href="?action=edit&amp;id='.$name_data['name_id'].'"&gt;Edit&lt;/a&gt;' . '&lt;br&gt;';
  16. }
  17. ?&gt;
  18. &lt;?php
  19. /* when the edit link has been triggered */
  20. if (isset($_GET['action'])) {
  21. if ($_GET['action'] == "edit") {
  22. $id = $_GET['id'];
  23. $get_data = $mysqli-&gt;query("SELECT * FROM names WHERE name_id = $id");
  24. $getData = $get_data-&gt;fetch_assoc();
  25. ?&gt;
  26. &lt;br&gt;
  27. Edit:&lt;br&gt;
  28. &lt;form method="post" action="edit.php?id=&lt;?php echo $id; ?&gt;"&gt;
  29. First Name:&lt;br&gt;
  30. &lt;input type="text" name="fname" value="&lt;?php echo $getData['first_name'] ?&gt;" /&gt;&lt;br&gt;&lt;br&gt;
  31. Middle Name:&lt;br&gt;
  32. &lt;input type="text" name="mname" value="&lt;?php echo $getData['middle_name'] ?&gt;" /&gt;&lt;br&gt;&lt;br&gt;
  33. Last Name:&lt;br&gt;
  34. &lt;input type="text" name="lname" value="&lt;?php echo $getData['last_name'] ?&gt;" /&gt;&lt;br&gt;&lt;br&gt;
  35. &lt;button type="submit" name="edit"&gt;Edit&lt;/button&gt; &lt;a href="index.php"&gt;Cancel&lt;/a&gt;
  36. &lt;/form&gt;
  37. &lt;?php }
  38. }
  39. ?&gt;
  40. &lt;/body&gt;
  41. &lt;/html&gt;

 

  1. if(isset($_GET[‘action’])) { statement.. }

The “isset($_GET[‘action’])” statement means that if the action has been triggered, the condition statement will function. It can be seen on the URL when the edit link has been triggered.  The URL goes like this “index.php?action=edit”

Under the “isset($_GET[‘action’])” is the another condition which is the if($_GET[‘action’]==”edit”) .  Meaning if the value of the action is equal to edit, the edit form will appear.

Last, create an “edit.php” file then put the following codes.

  1. &lt;?php
  2.  
  3. include ‘connection.php’;
  4.  
  5. if (isset($_POST['edit'])) {
  6.  
  7. $id = $_GET['id'];
  8.  
  9. $fname = $_POST['fname'];
  10.  
  11. $mane = $_POST['mname'];
  12.  
  13. $lname = $_POST['lname'];
  14.  
  15. $edit = $mysqli-&gt;query("UPDATE names SET first_name = '$fname', middle_name = '$mane', last_name = '$lname' WHERE name_id = $id");
  16.  
  17. if ($edit) {
  18.  
  19. header("Location: index.php");
  20.  
  21. } else {
  22.  
  23. echo $mysqli-&gt;error;
  24.  
  25. }
  26.  
  27. &nbsp;
  28.  
  29. }
  30.  
  31. ?&gt;
Facebook Comments

LEAVE A REPLY

Please enter your comment!
Please enter your name here