Form Select Tag into Multiple DB Rows

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
AlphaWolf
Forum Newbie
Posts: 12
Joined: Sun Mar 23, 2003 8:49 pm

Form Select Tag into Multiple DB Rows

Post by AlphaWolf »

Hello,
My form looks something like:

Code: Select all

<form action="update_db.php" method="post">
<select name="variable1">
<option value="a">description
</select>
<select name="variable2" multiple>
<option value="1">description
</select>
Obviously, I would have more variables as options etc...

The data I want to select in this form is such that I will select one item from the first select tag and multiple items from the 2nd select tag.

What I then want is to have a new record inserted into the DB for each item selected from the variable2 select tag.

Does the information get passed to update_db.php as an array? So could I loop thru the array? If this is the case, I assume for each pass it would have the same value for variable1 and the different values selected for variable2?

Any help would be appreciated.
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Post by Jade »

Just post all the varibles and then select what you need:
page 1

<form action="update_db.php" method="post">
<select name="variable1">
<option value="a">description</option>
</select>
<select name="variable2" multiple>
<option value="1">description </option>
</select>

update_db.php

<?php

$varible1 = $_POST['varible1']; //this would return whatever they selected from the first select. For this script it would return "a"

$varible2 = $_POST['varible2']; //this would return whatever they selected from the second select. For this script it would return "1"

then insert things from the varibles into the database

$sql = "INSERT into db_name (varible1, varible2) VALUES ('$varible1', '$varible2') or die ('Error');

$result = mysql_query($sql);

There is no way you could get several things they selected in the second select box...at least not that i know of. If you want something like that you might want to considering making a second form for them to fill out.

Hope this helps,
Jade
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

I almost always process $_POST vars like this:

htmlspecialchars(addslashes(trim$_POST['var'])))

trim: coz '' or was it ' ' evaluates to true (damn can't remember)

addslashes: unless site visitors have the ', " or \ keys disabled

htmlspecialchars: blocks various nastiness if the POST var is a text string which will later be displayed in a browser or form field (and it could be a text string even if you expected an integer..)
Post Reply