#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    26
    Rep Power
    0

    Populating Form then Updating this Data in Database


    Hi All,

    I am currently working on a project and am somewhat stumped and was wondering if anyone could lead me in the right direction.
    This is what I want:

    A webpage that draws info from a database on the local host and puts the said information in an editable form.

    Users will go to this page to see the information, if it needs to be changed they will do this by changing the info themselves and pressing update/submit.

    To my knowledge this is possible with PHP but I seem to have tried a few things and none have worked. Thanks for the help in advance.

    Also I am not looking for someone to do this for me, just some examples of similar things or a few things to point me in the right direction.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,337
    Rep Power
    594
    What have you tried and what didn't work? Have you looked at a PHP/Database tutorial?
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    26
    Rep Power
    0
    Originally Posted by gw1500se
    What have you tried and what didn't work? Have you looked at a PHP/Database tutorial?
    I have tried many things but ultimately my main problem is getting the database to pull data and being able to place that data in a form. I am not sure how this is done and I haven't been able to find examples on the internet. Most things I've found pull data and echo it out. I need the user to be able to edit the data and then submit changes.
  6. #4
  7. Confused badger
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Mar 2009
    Location
    West Yorkshire
    Posts
    1,111
    Rep Power
    487
    If you use the tutorial that you're referencing above, to put it into a form just build a normal HTML form but use the values that are returned; for example if I select some data from my "Animals" table using:-

    SELECT * FROM Animals WHERE name = 'willow'

    And I get a result set of:-

    animal_id | type | name
    -------------------------------
    3 | cat | willow

    Then I would loop through that resultset but for each item, instead of just echo'ing to screen, I would use it as the 'value' in a standard HTML text box (or textarea) ... something like this:-

    PHP Code:
    foreach ($results as $value) {
      echo 
    "<input type='text' name='{$value['name']}'>";

    As for examples of doing this, this forum right here is littered with countless examples and I asked google "using mysql data in a html form", that returned about 98,500,000 results (0.33 seconds) and in the top 10 results, I count at least 6 where they describe what you're trying to achieve.

    Comments on this post

    • wayne12690 agrees
    "For if leisure and security were enjoyed by all alike, the great mass of human beings who are normally stupefied by poverty would become literate and would learn to think for themselves; and when once they had done this, they would sooner or later realise that the privileged minority had no function and they would sweep it away"
    - George Orwell, 1984
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    26
    Rep Power
    0
    Originally Posted by badger_fruit
    If you use the tutorial that you're referencing above, to put it into a form just build a normal HTML form but use the values that are returned; for example if I select some data from my "Animals" table using:-

    SELECT * FROM Animals WHERE name = 'willow'

    And I get a result set of:-

    animal_id | type | name
    -------------------------------
    3 | cat | willow

    Then I would loop through that resultset but for each item, instead of just echo'ing to screen, I would use it as the 'value' in a standard HTML text box (or textarea) ... something like this:-

    PHP Code:
    foreach ($results as $value) {
      echo 
    "<input type='text' name='{$value['name']}'>";

    As for examples of doing this, this forum right here is littered with countless examples and I asked google "using mysql data in a html form", that returned about 98,500,000 results (0.33 seconds) and in the top 10 results, I count at least 6 where they describe what you're trying to achieve.
    Thank you! For some reason when I looked I could not find anything that i felt was worthwhile. This is definitely a step in the right direction.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    26
    Rep Power
    0
    PHP Code:
    echo "<tr>";
        echo 
    "<td width='305' valign='top'>"."<b>Conatct Name:</b>"' '$row2['First_Name']. ' '$row2['Last_Name'].  "</td>"
        echo 
    "</tr>";
        echo 
    "<tr>";
        echo 
    "<td width='305' valign='top'>"."<b>Email:</b>"' '$row2['Email']. "</td>";
        echo 
    "</tr>"
    How can I make this text into an editable form? So I would like to make the echo statements that return something its own text field or text area.

    I have had no success at doing so. Thanks
  12. #7
  13. Confused badger
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Mar 2009
    Location
    West Yorkshire
    Posts
    1,111
    Rep Power
    487
    Originally Posted by wayne12690
    Thank you! For some reason when I looked I could not find anything that i felt was worthwhile. This is definitely a step in the right direction.
    Hey, it's no problem ... an important thing to note, and I am sure you'll come across this in the tutorials etc, but whenever you're writing user provided data to screen, you should always run it through "HTMLENTITIES" before ...

    So in my example, we'd do something like this:-
    PHP Code:
    foreach ($results as $value) {
      
    $animal_name htmlentities($value['name'], ENT_QUOTES'UTF-8');
      echo 
    "<input type='text' name='{$animal_name}'>";

    And finally, an example using HTML TEXTAREAs:-

    PHP Code:
    foreach ($results as $value) {
      
    $animal_name htmlentities($value['name'], ENT_QUOTES'UTF-8');
      echo 
    "<textarea name='mytextarea'>{$animal_name}</textarea>";

    You can of course, use SELECTs ...

    PHP Code:
    echo "<select name='animal'>";
    foreach (
    $results as $value) {
      
    $animal_name htmlentities($value['name'], ENT_QUOTES'UTF-8');
      echo 
    "<option value={$value['animal_id']}>{$animal_name}</option>";
    }
    echo 
    "</select>"
    (In that example, note I don't use htmlentities on animal_id ... that's because I am presuming it's an 'auto id' created within MySQL)

    Anyway, you should be getting the idea now though, good luck / have fun!
    "For if leisure and security were enjoyed by all alike, the great mass of human beings who are normally stupefied by poverty would become literate and would learn to think for themselves; and when once they had done this, they would sooner or later realise that the privileged minority had no function and they would sweep it away"
    - George Orwell, 1984

IMN logo majestic logo threadwatch logo seochat tools logo