Retrieving table data in the database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
emilcarlo
Forum Commoner
Posts: 43
Joined: Wed Jul 21, 2010 12:38 pm

Retrieving table data in the database

Post by emilcarlo »

Good Morning all,

I have a problem in retrieving table data to a drop down menu. Here's the setup:

I have a drop down menu where the user will need to select a territory. The code for that one is

<select name="territory" width="230" style="width: 143px" id="territory">
<option selected="selected" disabled="disabled">Select Territory</option>
<option>Territory 1</option>
<option>Territory 2</option>
<option>Territory 3</option>
<option>Territory 4</option>
<option>Territory 5</option>
<option>Territory 6</option>
<option>Territory 7</option>
<option>Territory 8</option>
<option>Territory 9</option>
<option>Territory 10</option>
</select>

Adding the information in the database is working fine. My problem is the retrieval of the information to the drop down menu. In case the user would want to edit information, the data being displayed by the drop down menu is not the data stored in the database, but the "Select Territory" option. So if the user modifies the information, and updated some of the fields, but not the drop down menu, the database will be updated to "blank".

I am a newbie in the industry and is trying my best to learn codes, and not just copy the code in the Internet and paste it on the program. I hope anyone can help me with this. Any response will be appreciated. Thank you.
Baseball435
Forum Newbie
Posts: 2
Joined: Fri Jul 30, 2010 1:45 pm

Re: Retrieving table data in the database

Post by Baseball435 »

I don't fully understand what your saying or trying to do. But if you are saying like you want the user to type in something save it, send that to the database, retrieve it, and then put it into the drop down menu you would have to have a variable like $territory1 and make it so that the value is ['territory1'] in your mysql database or whatever your using. Then have a field which the user types in and then is saved as ['territory1'] in your database. Yes it's confusing but it's kind of like a register form. A person registers and the fields are sent and saved to the database. Look on YouTube or google on "making a register form in php" to get an idea
emilcarlo
Forum Commoner
Posts: 43
Joined: Wed Jul 21, 2010 12:38 pm

Re: Retrieving table data in the database

Post by emilcarlo »

Hi baseball, thank you for that fast response :)

I have a field on the database "territory" so whenever the user selects a territory on the drop down menu, let's say territory 4, it saves on the database. That's no problem at all, my problem is, when I try to retrieve the territory 4, it doesn't automatically set the drop down menu on territory 4, but the default selection "Select Territory".

Like for example, I would want to retrieve a data from the database and display it on a text box, i will simply need to do <?php echo $job_title; ?> right? I tried this with the drop down (noob here sorry xD) but it didn't work xD
jraede
Forum Contributor
Posts: 254
Joined: Tue Feb 16, 2010 5:39 pm

Re: Retrieving table data in the database

Post by jraede »

Grab the territory as a variable, and then have PHP echo out 'selected="selected"' if the variable value matches the option value. Unfortunately HTML can't just do this automatically.

Something like...

Code: Select all

<?php  $territory = $_GET['territory']; //assuming it comes from a URL query string, but it can come from anywhere 
?>
<select name="territory" width="230" style="width: 143px" id="territory">
<option selected="selected" disabled="disabled">Select Territory</option>
<option <?php if($territory == 'Territory 1') echo 'selected="selected"' ?>>Territory 1</option>
<option <?php if($territory == 'Territory 1') echo 'selected="selected"' ?>>Territory 2</option>
...etc

emilcarlo
Forum Commoner
Posts: 43
Joined: Wed Jul 21, 2010 12:38 pm

Re: Retrieving table data in the database

Post by emilcarlo »

Thanks jraede,

Really low grader here xD Still reading the Dummy Book for PHP and MySQL hihi

I'll try this one out, and post the results. Thanks again ^^
emilcarlo
Forum Commoner
Posts: 43
Joined: Wed Jul 21, 2010 12:38 pm

Re: Retrieving table data in the database

Post by emilcarlo »

Amazing! Really there are still lots of things to learn in programming xD Thanks very much for the big help jraede!
jraede
Forum Contributor
Posts: 254
Joined: Tue Feb 16, 2010 5:39 pm

Re: Retrieving table data in the database

Post by jraede »

No problem, glad I could help.
Post Reply