Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Appending multi-select box to database

Posted on 2005-03-22
5
Medium Priority
?
199 Views
Last Modified: 2012-03-15
A user may select a multiple number of these options within a single submit, each should be concatinated with the existing value for that field. I won't know how many selections they may make ahead of time. Load into an array?

Add Keywords: <select name="keywordparam" size="25" multiple>
            <option value="1">auditory</option>
            <option value="2">boundary</option>
            <option value="3">buildings</option>
            <option value="4">cadastre</option>
            <option value="5">climate</option>
            <option value="6">common</option>
            <option value="7">communications</option>
            <option value="8">cultural</option>
            <option value="9">demographics</option>
            <option value="10">ecology</option>
            <option value="11">environmental_hazards</option>
            <option value="12">flora</option>
            <option value="13">future_projects</option>
            <option value="14">geodetic</option>
            <option value="15">geology</option>
            <option value="16">hydrography</option>
            <option value="17">improvement</option>
            <option value="18">land_status</option>
            <option value="19">landform </option>
            <option value="20">military_operations</option>
            <option value="21">olfactory</option>
            <option value="22">soil</option>
            <option value="23">transportation</option>
            <option value="24">utilities</option>
            <option value="25">visual</option>
        </select>


Then send it to oracle like:

update table a
   set myfield = myfield || 'some new text'
 where ....

??
0
Comment
Question by:dprasad
  • 4
5 Comments
 
LVL 32

Expert Comment

by:Batalf
ID: 13600730
Make it an array

<select name="keywordparam[]" size="25" multiple>

Then loop through the array on the server

if(isset($_POST['keywordparam'])){
    for($no=0;$no<count($_POST['keywordparam']);$no++){
        echo $_POST['keywordparam'][$no]."<br>";
        // DO YOUR UPDATED HERE
    }


}
0
 
LVL 32

Expert Comment

by:Batalf
ID: 13600793
And you shouldn't concat the values into one single field in your database.

To protect your self from future problems, create a new table with a reference to your table above

create table paramList(
tableaID number(11) references tablea on delete cascade,
paramID number(11) references param on delete cascade,
userID number(11) references userTable on delete cascade);

where tableaID is a reference to your table above and
paramID is a reference to the table where your params are located and


Then you could update your values like this:

// Step 1 -> delete existing params for this user
$query="delete from paramList where tableaID='<whatever>'";

// Step 2 insert new values
if(isset($_POST['keywordparam'])){
    for($no=0;$no<count($_POST['keywordparam']);$no++){
        $query="insert into paramList(tableaID,paramID)values('<whatever>','".$_POST['keywordparam'][$no]."')";
    }
}

0
 
LVL 32

Expert Comment

by:Batalf
ID: 13600800
create table paramList(
tableaID number(11) references tablea on delete cascade,
paramID number(11) references param on delete cascade,
userID number(11) references userTable on delete cascade);

should be

create table paramList(
tableaID number(11) references tablea on delete cascade,
paramID number(11) references param on delete cascade);

userID isn't needed since it's the same as tableaID. Forgot to remove that line from the comment.
0
 

Author Comment

by:dprasad
ID: 13601315
yeah it is a bad idea to concatenate like this, will have to run it by the database guys, which could take a while unfortunately
0
 
LVL 32

Accepted Solution

by:
Batalf earned 2000 total points
ID: 13601393
Until then, you could get the the values from the selectbox concatinated like thsi:

if(isset($_POST['keywordparam'])){
    $values = implode(",",$_POST['keywordparam']);
}

now, $value will look like this

"1,2,5"

all depending on the ones the users selected
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
This holiday season, we’re giving away the gift of knowledge—tech knowledge, that is. Keep reading to see what hacks, tips, and trends we have wrapped and waiting for you under the tree.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question