Keep a variable and add a second in database field

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
SB68
Forum Newbie
Posts: 1
Joined: Sun Jan 11, 2009 5:45 am

Keep a variable and add a second in database field

Post by SB68 »

Hi,

To give an example first of a working code,

<?php
$package = explode("|",$slcustom32);

?>
New order:<select name="newcustom32" >
<option value=""><? if (empty($package[1])) { echo "Choose a package"; } else { echo $package[1]; } ?></option>
<option value="<?=$package[0]?>|Storage package B|" <? if ( $package[1] == "Storage package B|" ) { echo "selected"; } ?> >Storage package B</option>

The idea here, which works, is that if $package[0] exists in the database, then keep that and add $package[1] as Storage package B, and separate $package[0] and $package[1] by a |


Now, how can I do the same for a text field input? Must I use a str_replace for this one, or is this impossible to do?
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Keep a variable and add a second in database field

Post by jaoudestudios »

I would not NED the code together like that, I would have a separate table (one-to-many relationship) - normalise your database.

Also you are mixing your php tags, will full and abbreviated versions. I would definitely advise against using shorten tags!
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Keep a variable and add a second in database field

Post by califdon »

jaoudestudios wrote:I would not NED the code together like that, I would have a separate table (one-to-many relationship) - normalise your database.

Also you are mixing your php tags, will full and abbreviated versions. I would definitely advise against using shorten tags!
I'll add my firm endorsement of that.

Even though I don't know what NED is. :wink:

First, in relational databases, every column in a table must be "atomic", that is, have a single value that cannot be separated into more than one value. So when you break that rule, all bets are off as to what you can do to recover data because it doesn't conform to the rules of database normalization. Secondly, the only time you should ever use the short-form PHP tag (<?) is when you don't care if the web server understands it or not--some older ones may not. Thirdly, to maintain your own sanity and enable others to read your code, don't bunch your code together like that.
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Keep a variable and add a second in database field

Post by jaoudestudios »

califdon wrote:Even though I don't know what NED is. :wink:
Hahaha...its a long story, in a nutshell, it is when something is fudged. Thought I would give it a try and see if it catches on :D
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Keep a variable and add a second in database field

Post by califdon »

jaoudestudios wrote:
califdon wrote:Even though I don't know what NED is. :wink:
Hahaha...its a long story, in a nutshell, it is when something is fudged. Thought I would give it a try and see if it catches on :D
Hmm, sounds like it's naughty language... :roll:

:P And here I thought I'd learn something new ... oh, well!
Post Reply