Update record using PHP
Subject: Web development using PHP and MySQL
Let us assume that you have a small news web portal, after posting and displaying the news well paginated for your user to view but then you notice a mistake in the posted news, to correct this you will have to update or edit that content.
Updating a record through a web form as illustrated above involve four steps:
Step1. Select or retrieve and displayed the record as we have done in previous topic with or without pagination
Step2. Introduce the edit link to a page URL with a query string carrying the unique id of each record (this is where we will start this lesson from)
Step3. When the edit link is clicked, you are lead to a page (usually this page will contain a form), here you will use the value in the query string to retrieve or SELECT the record having that unique id from the database and display the individual values in the form element so that the user can see and modify the data in the form text fields.
Also this page will have an hidden field which we will also hold the unique id, because this form must be posted together to the final page with the id of the record to be edited
Step4. The form data with the hidden field is then posted or submitted to the URL that will affect the change in our database using the UDATE SQL query, the hidden id field data is use to know the exact record to update in our table.
The Edit Link (Step2)
From step 2, Open the feedback_pg.php where we have the record displayed and paginated. Now locate the table heading in the code and include an extra empty table data tag (td)
...
//the table header
print "
NAME |
EMAIL |
COMMENT |
DATE POST |
|
"; ...
Now we will have to do the same at the data section also with a special link (with a query string carrying the unique id of each record or row) to a page URL
....
//loop through the query and print the result
while($info = mysqli_fetch_array($result)){
$data_id = $info['id'];
$data_name = $info['name'];
$data_email = $info['email'];
$data_comment = $info['comment'];
$data_date = $info['date_submit'];
print "
$data_name |
$data_email |
$data_comment |
$data_date |
Edit |
";
}
print "
";
....
Let’s look at the newly introduced link, we are use to the first section of the link
a href=\"feedback_edit.php , while the
?id=$data_id\" section needs some explanation, the question mark ? is the character that we use in initiate the query link, the id is just a variable name (querystring variable - it does not begin with the dollar $ sign), so you can use any name, and the remaining =$data_id assigns the value of the variable ($data_id) to the querystring variable (id).
Notice that $data_id is already declared and assign a value earlier, see the line $data_id = $info['id']; which is the id of the record retrieve from the database.
Editing Mode (Step3)
Create the feedback_edit.php file that the link with send it's data to and let it have the following code:
feedback_edit.php
Company Feedback
The fields of this form in this file are expected to show the details of the selected record, so that we can edit by changing this detail from there before finally submitting for update.
Here is what the code in feedback_edit.php basically doing:
1. Connect of database
2. Query or SELECT from the required table using the value from the query link
3. Append the respective data to the form element (including the hidden field)
See the screenshot (Update form)
Update record (Step4)
Create the file feedback_edited.php with the code code below.
feedback_edited.php
Company Feedback
Return
Notice that the variable $id, is collected from the hidden field sel_id and kept in the $id, and use to specify the id of the record in the table to be update
....
$id=mysqli_escape_string($conn, $_POST['sel_id']);
.....
UPDATE feedback SET
name='$names',
email='$email',
comment='$comment'
WHERE id= '$id'
See the screenshots below.
Record with "Edit" link
Update form
Updated...
By:
Benjamin Onuorah
Login to comment or ask question on this topic
Previous Topic Next Topic