You mean like this? It worked.
Actually, the reason this works is because you changed the first form to method=‘get’ (the id is passed in the url now) and you removed the action=’…’ attribute from the edit form. By removing the action=’…’ attribute, the browser will ‘automatically’ propagate any existing get parameters in the url, so the id will now exist after the edit form is submitted. This isn’t exactly what I suggested, but it’s okay for now. It would be better if you passed the id in a hidden field in the post method edit form.
The first form still needs to be made ‘sticky’ by pre-selecting the <option choice that matches the submitted id value. The way to do this is to output the ‘selected’ attribute inside the correct <option …> tag. You should also have a value=’…’ attribute inside each <option …> tag. Most browsers will submit the option display value if there is no value=’…’ attribute, but some will not. It is best to always have a value=’…’ attribute.
If there are any validation errors in the post method form processing code, you should re-populate the form fields with the submitted form data, not the data from the database query. In fact, if the post method edit form has been submitted, you should skip running the database query code. The way to accomplish this 'switch' between what data to use to populate the form fields with, is to use an internal variable, $data or similar name, that you copy the submitted form data to inside the post method form processing code. At the database query code, if the $data variable is empty, you would run the query and fetch the result of the query into the $data variable. If the $data variable is not empty, you would skip running the database query code.
Hey, I am not a native English speakers but I try to understand what you are trying to say that I should separate both on how to view and fetch data in a form right? (at least, make another form just to show the submitted data from the last data insertion and another form to see the data fetch from the table).
I mean, like, I should see the changes that I have made in a form just to ensure that the data experience changes, right?
Please rectify me if I understand it wrong.
You currently don’t have any validation logic, which you need, so you currently aren’t trying to re-display the form with the submitted form data in it. You are always getting the original data from the database table when displaying the form… If there are validation errors, such as an empty username or using an incorrectly formatted email address, you would want to re-display the form with the submitted form data in it, not the original data from the database table, so that you don’t need to keep retyping the changes in the fields that don’t have any validation errors. The pseudo logic to do this would be -
[php]
$data = []; // define an array to hold a working copy of the data
…start of post method form processing code
$data = $_POST; // copy the form data to the common variable being used in the rest of the code
// actually, you should trim the data when you make this copy of it, so that you can detect if all white-space characters were entered
…end of post method form processing code
… start of code getting the original row of data from the database table
// if $data is empty
if(empty($data))
{
query to get the row from registerlogin and store it in $data
}
… end of code getting the original row of data from the database table
// at this point $data either contains the original data from the database table or the submitted form data. use the elements in $data for the form field value=’…’ attributes.
// you would display the form at this point, not as part of the code getting the original row of data from the database table.[/php]
However, I did not get how you can store all the column in your '$data'? Is it by using array?
Yes, it’s an array. From the code getting the original row of data from the database table, it’s the fetched row from the query. From the post method edit form processing code, it’s a copy of the submitted $_POST data.
Mostly, I saw codes using 'md5()' which is just a hashing technique. I would like to hear anyone's clarification on this so I can make a decision what type of method I should use in my coding style.
The php password_hash() and password_verify() functions are the current best method for hashing passwords. The hash uses a ‘stronger’ hash (the md5()/sha() hashes are easily brute force matched by today’s personal computing hardware), has a random salt value for each hashed password, and has a cost/number of times the hash is looped to slow down the process of brute force matching of input to hash value.
What I understand from your writing, if the password is empty and it was updated into the table that somehow can trigger the stored hash inside the table? How is that? What the 'setting the passwd db column value to the same value it currently is' really means?
Since the value stored in the database table is the hash of the password, there’s no point in selecting it and trying to put it into the password form field value. So, initially, all you can do is leave the password field empty. If the admin who is doing this wants to set a new password, they would enter the password in the form field.
In the form processing code, if there is a non-empty value for the password form field, you would apply password_hash() to it and set the password field in the database table with this hash. However, if the password form field is empty, it means to not change the value stored in the database table. If you are setting the password column in the database table with a new hash, the set part of the UPDATE query would need to contain passwd = ? (assuming you are using a prepared query with a place-holder for the value.) If you are not setting the password column, you would either leave the passwd = ? term out of the set part of the sql query statement, or to set the password column to the same value it currently has, you would need to make this part of the sql query be passwd = passwd. However, you cannot specify a column name via a prepared query place-holder, so you would need to dynamically build the sql query statement or select between two different UPDATE sql query statements.
I do not understand this 'post method form processing code'.
It’s the php code that is processing the post method form data. It in your current code, it starts with the if( isset( $_POST[‘submitbutton’] ) ). The reason for putting it near the top of your file is so that any changes that the form processing code makes, can be used by the rest of the code, and so that you can display any validation errors when you (re)display the form. In your current code, the post method form processing code is after the point where you are displaying the form, so, any errors would get displayed after the bottom of the form where they would be easy to miss and you currently won’t be able to re-populate the form field values with the submitted form data.
The code in your file should generally be laid out as follows -
- Initialization.
- Post method form processing.
- Get method ‘business logic’ - this is code that knows how to get/produce data needed to display the page.
- Get method ‘presentation logic’ - this is code that knows how to produce the dynamic content on the page. For simple things, you can just put this logic at the appropriate places in the html document.
- The actual html document.
Hopefully, this addresses all the questions you asked.