Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    Jul 2003
    Posts
    90
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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

  • #2
    Regular Coder
    Join Date
    Jul 2005
    Location
    Guildford, UK
    Posts
    109
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i would like to know this as well, only i have 3 combos and a textarea...any guidance would be welcome

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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.

  • #4
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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!

  • #5
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Whoops, I think I got the wrong impression, I thought you meant a multiple combo box. ah well :P, good luck anyways

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Good example using as an array!
    You indented it as well
    Not all developers are as lazy as me, lol

  • #7
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #8
    New Coder
    Join Date
    Jul 2003
    Posts
    90
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks very mutch.

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

    How can we do it?

    thanks

  • #9
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Single entry:
    PHP Code:
    $query "UPDATE `table` SET `column_name` = '" mysql_real_escape_string($_POST['select']) . "' WHERE `row_identifier` = '{row_identifier_value}'"


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •