saving category name into db instead of id number

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
cjkeane
Forum Contributor
Posts: 217
Joined: Fri Jun 11, 2010 1:17 pm

saving category name into db instead of id number

Post by cjkeane »

hi.

i'm using the following code to list salutations from a salutations table. The user can select the salutation, but when my sql runs, it inserts the ID number. I really need it to insert the actual salutation. Any assistance would be appreciated. thx.

<label>
<?php
$result=mysql_query("select * from salutations");
$options="";
while ($row=mysql_fetch_array($result)) {
$id=$row["SalutationID"];
$categoryname=$row["Salutation"];
$options.="<OPTION VALUE=\"$id\">".$categoryname.'</option>';
}
?>
<select name="Salutation">
<option value="0">< select salutation > <?php echo $options ?></option>
</select>
</label>
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: saving category name into db instead of id number

Post by requinix »

Sorry, but no. You should be using the ID number instead of the name. That's the whole point of having the ID numbers.

But whatever. Spoiler:

Code: Select all

<option value="the value you want in the database">the value you want to show to the user</option>
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: saving category name into db instead of id number

Post by onion2k »

tasairis wrote:Sorry, but no. You should be using the ID number instead of the name. That's the whole point of having the ID numbers.
Actually, I don't agree. Using the text (aka a "natural language key") is preferable for things that don't change and that you'll always have to look up. There's little point in costing yourself a database JOIN every time you need to display the user's full name.
cjkeane
Forum Contributor
Posts: 217
Joined: Fri Jun 11, 2010 1:17 pm

Re: saving category name into db instead of id number

Post by cjkeane »

the reason i need to save the category name into the table instead of the id number is because i have other queries on other pages of the site looking up the info. for example.

name salutation
john dr
peter mr


that sort of thing.

the code that i'm using to list the salutations in a dropdown box do list the salutations but when posted to the database, i'm getting this.

name salutation
john 1
peter 2

how can i resolve this? thx.
Post Reply