Updating a form that was previously populated with a get method

Ive researched the topic online and can’t find an answer on this question:

If I passed a record to a form while using a hyperlink and the GET method and now I want to make change to the data inside the form, can I update this form while using the POST method to the database and update the existing record?

I am fairly new in php and don’t know all the rules yet. To me it seems like if I already have an input field set to value=“echo $_GET[‘firstname’]” I won’t be able to update my form to mysql since I am using the POST method and think the form would automatically revert to the data I populated the form with. I;m saying this because when I click my sumit button the form reverts back to the data I first populated the form with???

Why do you use GET to fill form?
It’s better practice to have only record ID in the URL, for example

edit.php?id=5

then you can select record from database by ID and fill form using that data
this will allow you to have up-to-date values each time you open the form

Yes, I’d think you can. Your form gets populated with data passed into it via $GET which it will use as default values, but as long as you set the form method to POST, you then call whatever php updates your form. By the time your browser gets around to submitting the form using the button, it has no knowledge of how the default values arrived in the form.

<form method="post" action="storechanges.php">
<input type="text" name="fname" maxlength="30" size="30" value="<?php echo $_GET['firstname']; ?>">
<input type="submit">
</form>

.. storechanges.php
<?php
$fname = $_POST['fname'];
...

If you’re having trouble with what it does when you click the submit button, post your code so someone can figure out what. Note that above, I’m not necessarily advocating changing field names for no apparent reason, just trying to make it clear.

As @megazoid said, it’s not good practice to pass the whole lot around as part of the URL. I am no security expert, but I would think it makes it very easy to stick any old data in there because the URL can be altered so easily.

My goodness I feel a bit ridiculous now after reading megazoid and your answer. Live and learn and get better lol. ok, so this bit of logic will solve a whole bunch of problem with sticky form and the echo of my updated database record. I think I will give this a shot and re-write my update code with this logic in mind and if I hit a brick wall I will come back and ask if anyone can verify my code. I find I learn a lot more if I try figuring the problem myself than be given the answer. thanks a bunch.

Thanks megazoid, please see the response I wrote to droopsnoot. I feel a bit embarrassed but that’s how one learns.

There’s no need to - as you said later, you have to learn somewhere. I only come on here to learn.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.