Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-12-2005, 03:23 AM   PM User | #1
devil_online
New Coder

 
Join Date: Jul 2003
Posts: 90
Thanks: 0
Thanked 0 Times in 0 Posts
devil_online is an unknown quantity at this point
insert values with a combo box

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
devil_online is offline   Reply With Quote
Old 07-12-2005, 09:13 AM   PM User | #2
theexo51
Regular Coder

 
Join Date: Jul 2005
Location: Guildford, UK
Posts: 109
Thanks: 0
Thanked 0 Times in 0 Posts
theexo51 is an unknown quantity at this point
i would like to know this as well, only i have 3 combos and a textarea...any guidance would be welcome
theexo51 is offline   Reply With Quote
Old 07-12-2005, 02:57 PM   PM User | #3
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Code:
<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:
PHP Code:

$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.
Fou-Lu is offline   Reply With Quote
Old 07-12-2005, 03:09 PM   PM User | #4
missing-score
Senior Coder


 
missing-score's Avatar
 
Join Date: Jan 2003
Location: UK
Posts: 2,194
Thanks: 0
Thanked 0 Times in 0 Posts
missing-score is on a distinguished road
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)

Code:
<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 is offline   Reply With Quote
Old 07-12-2005, 03:10 PM   PM User | #5
missing-score
Senior Coder


 
missing-score's Avatar
 
Join Date: Jan 2003
Location: UK
Posts: 2,194
Thanks: 0
Thanked 0 Times in 0 Posts
missing-score is on a distinguished road
Whoops, I think I got the wrong impression, I thought you meant a multiple combo box. ah well :P, good luck anyways
missing-score is offline   Reply With Quote
Old 07-12-2005, 03:11 PM   PM User | #6
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Good example using as an array!
You indented it as well
Not all developers are as lazy as me, lol
Fou-Lu is offline   Reply With Quote
Old 07-12-2005, 03:14 PM   PM User | #7
missing-score
Senior Coder


 
missing-score's Avatar
 
Join Date: Jan 2003
Location: UK
Posts: 2,194
Thanks: 0
Thanked 0 Times in 0 Posts
missing-score is on a distinguished road
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.
missing-score is offline   Reply With Quote
Old 07-12-2005, 11:33 PM   PM User | #8
devil_online
New Coder

 
Join Date: Jul 2003
Posts: 90
Thanks: 0
Thanked 0 Times in 0 Posts
devil_online is an unknown quantity at this point
thanks very mutch.

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

How can we do it?

thanks
devil_online is offline   Reply With Quote
Old 07-13-2005, 03:42 AM   PM User | #9
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Single entry:
PHP Code:
$query "UPDATE `table` SET `column_name` = '" mysql_real_escape_string($_POST['select']) . "' WHERE `row_identifier` = '{row_identifier_value}'"
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:58 AM.


Advertisement
Log in to turn off these ads.