View Full Version : insert values with a combo box

07-12-2005, 04:23 AM
Hi, I have an combo box and I want to make it insert the value we choose to the database.

How can we do that?

thanks in advance

07-12-2005, 10:13 AM
i would like to know this as well, only i have 3 combos and a textarea...any guidance would be welcome

07-12-2005, 03:57 PM
Choosing a select option is simple. Choosing a textarea or text type field is just as easy:
BTW, I assume mysql is your database of choice, and you have a connection already established.

<form action="form.php" method="post">
<input type="text" name="text_name" />
<textarea name="textarea_name"></textarea>
<select name="select_name">
<option value="1">1</option>
<option value="2">2</option>
<!-- Etc. -->
<input type="submit" name="submit" value="Submit" />

To the form:

$textfield = mysql_real_escape_string($_POST['text_name']);
$textarea = mysql_real_escape_string($_POST['textarea_name']);
$select = mysql_real_escape_string($_POST['select_name']);
// Ok, take your newly create variables:
$query = "INSERT INTO `table` ('textfield', 'textarea', 'select') VALUES ('" . $textfield . "', '" . $textarea . "', '" . $select . "')";
// Ok, all done :)

This is really simple and straight forward. You may have more, you may have less. You can also create an array for your input fields if you would like, though you would need to validate each and place into a string before doing your query. Otherwise your values will most likely be incorrect.
Hope that helps.

07-12-2005, 04:09 PM
Fairly simple once you understand how the form data is sent. You need to take advantage of PHP's array handling for this. Set out a form like below... (I have used the POST method but you could use GET)

<form action="somepage.php" method="POST">
<select name="values[]" multiple="multiple">
<option value="option1">Option 1</option>
<option value="option2">Option 2</option>
<option value="option3">Option 3</option>
<input type="submit" value="Submit" />

Notice how I called the select name values[] (with the brackets), when data is sent PHP actually parses this as an array. For example, if I select option1 and option3 from the select box, the data PHP gets is:


Which when accessed by PHP is:

$_POST['values'][0] = 'option1';
$_POST['values'][1] = 'option3';

Because the arrays are indexed starting at 0, you can use a for loop to loop through all the values and insert them into the database/file, easy eh!

07-12-2005, 04:10 PM
Whoops, I think I got the wrong impression, I thought you meant a multiple combo box. ah well :P, good luck anyways :)

07-12-2005, 04:11 PM
Good example using as an array!
You indented it as well :thumbsup:
Not all developers are as lazy as me, lol

07-12-2005, 04:14 PM
lol :P
For multiple selections, an array is really the only way to go (eg, where you can select more than 1 item from the combo box, but for just selecting a single value, your method is better, as an array is pointless.

07-13-2005, 12:33 AM
thanks very mutch. :D

However I have so problems because I want to UPDATE and not INSERT.

How can we do it?


07-13-2005, 04:42 AM
Single entry:

$query = "UPDATE `table` SET `column_name` = '" . mysql_real_escape_string($_POST['select']) . "' WHERE `row_identifier` = '{row_identifier_value}'";