Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: [PHP] Pulling information from the database to fill forms?  (Read 13008 times)

0 Members and 1 Guest are viewing this topic.

BennyF

    Topic Starter


    Rookie

    • Yes
    • MyGPal
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows 7
[PHP] Pulling information from the database to fill forms?
« on: June 08, 2013, 05:22:55 AM »
Hey Hopers, what I'm trying to do is pull information from my database and display it in html form fields, I'v already worked out how to do this for text but I'm unsure of how to do it with HTML Form Fields like Radio, Checkbox and Select.

What i'm trying to do currently is pull the result from the database and if the result=male it automatically checks male as active and if result=female it automatically checks female as active, I'm assuming this would definitely be something to do with the IF statement, just not sure how to work it.

I have used includes to include other files which connect it to the database, fetches and sets up defined variables for $row's, If anyone believe they may be the issue please let me know and I'll post the code for those, I'm assuming they're correct since it's working to fill in all of the text fields but I'm happy to post it in case I'v missed something

I tried this below;
Code: [Select]
<input type="radio" name="gender" id="gender" value="male"<?php
    
if ($row['gender'] == 'male') echo "checked";
?>
>
But for some reason it's not working, I'm using MySQLi fetch statements to ready the data for insertion and up until now it's worked well, inserting text into textfields brilliantly.

PrtScr example;


I haven't got to the birthdate fields yet but I'm sure i'll need some help putting the data from the DB into those as well if anyone cares to help, I'm thinking I'll store them with a string so when pulling them back out I'll likely have to decipher the string and split it back up and then once again I'd assume it'll be more IF statements, quite a lot i'd assume to tell the form what to do with it.

Any help with either/both things would be greatly appreciated, Haven't done PHP like this for quite some time and to top it off I'm learning MySQLi and PDO as I go so my mind is somewhat scrambled.

Thank you in advance CH'ers

Currently recapping my PHP Knowledge and learning MySQLi and PDO.

BC_Programmer


    Mastermind
  • Typing is no substitute for thinking.
  • Thanked: 1140
    • Yes
    • Yes
    • BC-Programming.com
  • Certifications: List
  • Computer: Specs
  • Experience: Beginner
  • OS: Windows 11
Re: [PHP] Pulling information from the database to fill forms?
« Reply #1 on: June 08, 2013, 07:53:52 AM »
I imagine it's due to your code expanding to this:

Code: [Select]
<input type="radio" name="gender" id="gender" value="male"checked>
I was trying to dereference Null Pointers before it was cool.

BennyF

    Topic Starter


    Rookie

    • Yes
    • MyGPal
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows 7
Re: [PHP] Pulling information from the database to fill forms?
« Reply #2 on: June 08, 2013, 04:22:21 PM »
I imagine it's due to your code expanding to this:

Code: [Select]
<input type="radio" name="gender" id="gender" value="male"checked>

When I do that on it's own it outputs a checked radio field, which is what I want but for some reason the field doesn't check with the PHP code, I Guess I'll have to look into my code a little more.

edit: Realised the issue was that I was pulling the data via sessionid but I had forgotten to include the file that handles sessions, sorry guys.
« Last Edit: June 08, 2013, 05:22:58 PM by BennyF »
Currently recapping my PHP Knowledge and learning MySQLi and PDO.

BennyF

    Topic Starter


    Rookie

    • Yes
    • MyGPal
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows 7
Re: [PHP] Pulling information from the database to fill forms?
« Reply #3 on: June 08, 2013, 07:17:29 PM »
Wont let me edit my post again, I guess there is a timer

For anyone who has this issue and finds this, I ended up changing my method for a more indepth 'failproof' method as the above was returning true if null for some reason.

 I ended up using an if statement and then calling the session information and then echoing the fields.;
Code: [Select]
<?php
               
if($session->userinfo["gender"] == "male")
               {
               echo 
'<input type="radio" name="gender" id="gender" value="male" checked>Male' ;
               }
               else
               {
               echo 
'<input type="radio" name="gender" id="gender" value="male">Male';
               }
               if(
$session->userinfo["gender"] == "female")
               {
               echo 
'<input type="radio" name="gender" id="gender" value="female" checked>Female' ;
               }
               else
               {
               echo 
'<input type="radio" name="gender" id="gender" value="female">Female';
               }
               
?>

Hopefully this can help someone else like me new to this type of PHP Coding

Now I'm up to the birthdate field, I could use the above method but It would take quite a few lines to get the correct results, what would be the best way for me to A. Store the Date of Birth and B. retrieve and display it for editing?

I'm assuming I'd convert the results into a string for storage but how would I split it back up on retrieving it?
Currently recapping my PHP Knowledge and learning MySQLi and PDO.