UPDATE / EDIT Records In Database Table: PHP & MySQL

Video tutorial demonstrates updating / editing of data / records from the database table and displaying it on the browser.

First look at these short videos:
Connect to the database
Simple / basic insertion operation
Insertion of Records into Database using Forms: PHP & MySQL (important)
SELECT / LIST Records From Database Table: PHP & MySQL

Source Code: SELECTING and Displaying Data

index.php

  1. <?php  
  2.   
  3. include_once('db.php');  
  4.    
  5. if(isset($_POST['name']))  
  6. {  
  7.   $name = $_POST['name'];  
  8.    
  9.   if(mysql_query("INSERT INTO apple VALUES('','$name')"))  
  10. echo "Successful Insertion!";  
  11.   else  
  12. echo "Please try again";  
  13. }  
  14.    
  15.    
  16. $res = mysql_query("SELECT * FROM apple");  
  17.    
  18.    
  19. ?>  
  20.    
  21. <form action="." method="POST">  
  22. Name: <input type="text" name="name"/><br />  
  23. <input type="submit" value=" Enter "/>  
  24. </form>  
  25.    
  26. <h1>List of companies ..</h1>  
  27. < ?php  
  28. while$row = mysql_fetch_array($res) )  
  29.   echo "$row[id]. $row[name]  
  30.                 <a href='edit.php?edit=$row[id]'>edit<br />";  
  31. ?>  

Video Tutorial: UPDATE / EDIT Records In Database Table: PHP & MySQL



https://www.youtube.com/watch?v=Hw1MwUlekeo [Watch the Video In Full Screen.]


Source Code: Updating / Editing Data / Records In The Database Table

edit.php

  1. <?php  
  2. include_once('db.php');  
  3.    
  4. if( isset($_GET['edit']) )  
  5. {  
  6. $id = $_GET['edit'];  
  7. $res= mysql_query("SELECT * FROM apple WHERE id='$id'");  
  8. $row= mysql_fetch_array($res);  
  9. }  
  10.    
  11. if( isset($_POST['newName']) )  
  12. {  
  13. $newName = $_POST['newName'];  
  14. $id   = $_POST['id'];  
  15. $sql     = "UPDATE apple SET name='$newName' WHERE id='$id'";  
  16. $res  = mysql_query($sql)   
  17.                                     or die("Could not update".mysql_error());  
  18. echo "<meta http-equiv='refresh' content='0;url=index.php'>";  
  19. }  
  20.    
  21. ?>  
  22.   
  23. <form action="edit.php" method="POST">  
  24. Name: <input type="text" name="newName" value="<?php echo $row[1]; ?/>"><br />  
  25. <input type="hidden" name="id" value="<?php echo $row[0]; ?/>">  
  26. <input type="submit" value=" Update "/>  
  27. </form>  

meta tag used to redirect to other page..

  1. <meta http-equiv='refresh' content='0;url=index.php'>  

MySQL Query

  1. mysql> UPDATE apple SET name='newName' WHERE id='session_id';  

Security Issue:
Make sure not to allow people to explicitly pass random id’s and retrieve and edit/update other’s data. To solve this issue, you can save the id of the logged in user in a session variable and instead of passing id to edit.php we can directly use the id stored in our session variable.
This would solve the issue.

Output: After Updating the records:

  1. Google USA
  2. Apple USA
  3. Microsoft USA
  4. Oracle USA
  5. Technotip IN

Video’s To Watch:
GET method in action
Post method in action
session variable basics
User login and session