Page 1 of 1

Form Select Tag into Multiple DB Rows

Posted: Fri Apr 18, 2003 2:32 pm
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.

Posted: Mon Apr 21, 2003 8:08 pm
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

Posted: Mon Apr 21, 2003 10:24 pm
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..)