...

View Full Version : insert values with a combo box



devil_online
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

theexo51
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

Fou-Lu
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. -->
</select>
<input type="submit" name="submit" value="Submit" />
</form>

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 . "')";
mysql_query($query);
// 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.

missing-score
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>
</select>
<input type="submit" value="Submit" />
</form>


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:

values[]=option1&values[]=option3

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!

missing-score
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 :)

Fou-Lu
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

missing-score
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.

devil_online
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?

thanks

Fou-Lu
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}'";



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum