Save sqlwheremultiple.php as sqledit1.php.
You already learned how to extract/display SQL data and to update it. You also know how to create links with $_GET data. You know how to create forms with data in using the value attribute. You might be able to work out how to combine all those to let users edit data but this page will take you through it just in case.
The user needs to be able to choose which row in the table to edit. One way is to have an icon, symbol or word which they can click on next to the data.
Add the text (edit) to the end of the echo line which outputs the list items inside the while loop. Put an a element around the word edit to turn it into a link. The href attribute should have the value sqledit2.php (you will need to escape the quotes). Then add $_GET data on the end of the href value using ?ref=$ref. Try the page and each link should point to the same page but should have a different number on the end when you hover over it.
Save sqledit1.php as sqledit2.php. Delete the two lines which echo the ul and /ul tags. Leave the query and the while loop for now.
Add a line at the top of the PHP block to extract $_GET["ref"] from the URL and put it into a variable $ref.
In the query change the WHERE to look for records where ref='$ref'. That should extract just the one matching row. Test it by loading sqledit1.php and clicking on a link. You can also delete ref from the list of fields to obtain and delete the first line inside the loop as we already know $ref.
Inside the while loop delete the single echo line which creates the list items.
Now you need to show the data for editing. The easiest is to use a form with the data already in it ready to change. Remember when using echo to create HTML there will be double quotes to escape.
Before the while loop change the echo line. You need to echo a form tag (with method and action attributes - the value of action being sqledit3.php) and after the end of the while loop (after the }) echo the closing /form tag.
At the end of the loop where the echo was you will now echo all of the form fields complete with values. Technically no loop is needed as there is only one record to show but as it is there we might as well use it. Use echo to output these form fields with label elements each pair in a paragraph:
- ref (this field should be readonly).
- date of birth
- personal name
- family name
- date of joining
Each field needs these attributes:
- name and id (the name and id can be the same for each field as usual)
- value (inside the escaped quotes put the appropriate variable (e.g. $dob) containing the data from the database query result)
When you test it (by clicking on a link in sqledit1.php) you should see just that record with the values inside the form fields. If not you should try using echo and/or print_r to check on various things (that the ref arrived safely, what the query looks like, how many rows were in the result and so on).
You need one more thing - a submit button. This should go just before the line creating the /form tag.
This should be fairly easy now you have the data as you have done all this before. Create sqledit3.php and UPDATE the data using SQL:
- extract the $_POST data and put it into variables
- create an UPDATE query to update the row (remember WHERE ref=$ref LIMIT 1 on the end or you will change all rows to the new data)
- show a message saying it worked and provide a link back to sqledit1.php
One page and other improvements
If you wanted to you could merge all three pages into one as you did with normal forms so that:
- if there is no $_GET or $_POST data you show the list
- if there is $_GET data someone must have clicked on an edit link so you show the form
- if there is $_POST data you save the changes