UPDATE / EDIT Records In Database Table: PHP & MySQL

Advertisement:

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

SELECTING and Displaying Data
index.php

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
28
29
30
< ?php
	include_once('db.php');
 
	if(isset($_POST['name']))
	{
	  $name = $_POST['name'];
 
	  if(mysql_query("INSERT INTO apple VALUES('','$name')"))
		echo "Successful Insertion!";
	  else
		echo "Please try again";
	}
 
 
	$res = mysql_query("SELECT * FROM apple");
 
 
?>
 
<form action="." method="POST">
Name: <input type="text" name="name"/><br />
<input type="submit" value=" Enter "/>
</form>
 
<h1>List of companies ..</h1>
< ?php
	while( $row = mysql_fetch_array($res) )
	  echo "$row[id]. $row[name] 
                <a href='edit.php?edit=$row[id]'>edit<br />";
?>

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


You need to a flashplayer enabled browser to view this YouTube video

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



Updating / Editing Data / Records In The Database Table
edit.php

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
< ?php
	include_once('db.php');
 
	if( isset($_GET['edit']) )
	{
		$id = $_GET['edit'];
		$res= mysql_query("SELECT * FROM apple WHERE id='$id'");
		$row= mysql_fetch_array($res);
	}
 
	if( isset($_POST['newName']) )
	{
		$newName = $_POST['newName'];
		$id  	 = $_POST['id'];
		$sql     = "UPDATE apple SET name='$newName' WHERE id='$id'";
		$res 	 = mysql_query($sql) 
                                    or die("Could not update".mysql_error());
		echo "<meta http-equiv='refresh' content='0;url=index.php'>";
	}
 
?>
<form action="edit.php" method="POST">
Name: <input type="text" name="newName" value="<?php echo $row[1]; ?/>"><br />
<input type="hidden" name="id" value="<?php echo $row[0]; ?/>">
<input type="submit" value=" Update "/>
</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.

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

22 thoughts on “UPDATE / EDIT Records In Database Table: PHP & MySQL”

  1. Got this error ?

    1
    2
    3
    4
    
    <br /><b>Notice</b>: 
    Undefined variable: 
    row in <b>C:wampwwwtechnotipedit.php</b> on line <b>25</b>
    <br />

    This is because, name and id are name of the attributes/fields in the database and are not defined variable names in PHP script. So instead of using $row[id] and $row[name], write $row[0] and $row[1]

  2. how to make update for multiple users?? for example i have my user account and my friend has his own account. so, how we can update our own private data individually(means with out knowing modified with each other? ).
    thanks
    from bahir dar university
    Ethiopia

  3. hi, I’m trying to edit more than one field at the same time. But it updates only the first field “name”.

    for example
    1. Google, Nexus, ,Android edit I’d like to edit this at the same time.
    2. Apple, Iphone, IOS edit
    etc

  4. Created a member’s only setting and using a session to log into the members area how would I put together a view profile and update profile by passing the $_SESSION value to the DB? I originally used you sign up, log in video to create it.

    1. Since you store the location of the actual image in your database table, it’s like regular text edit.
      You simply change the path of the image.
      Maybe from a old location to a new one OR from one image path to the other.

  5. Hi Satish, I am a student and I see your tutorial about php login and registration form in same project (phploginsession) and it is really nice. Could you please tell me how can i make same project adding update form with your login and register form using php and mysql database ? Thank you for your time .

    1. @syed, Simply add the above code to your project and make sure to check, if the user has logged in before making the edit – with the help of session variable.

      Use the search form present in the right sidebar and search for session and validation etc tutorials ..all those video tutorials would help for your project.

      All the best ..

  6. Great video! All working but i want to be able to add a new record, a new row which i can edit. But seems to be a conflict with ID. Whats the solution??ļ»æ

Leave a Reply

Your email address will not be published. Required fields are marked *