SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2000
    0 Post(s)
    0 Thread(s)
    I am having trouble inserting selection from pull down menus in my form into mysql table. Whenever I select from pull down menu, the selection is not put into mysql...
    Any feedback? I know I am doing something wrong.....


    if ($submit): // A new business has been listed // using the form below.

    $dbcnx = @mysql_connect("localhost", "removedforsecurity", "removedforsecurity");

    $sql = "INSERT INTO Business SET " .
    "Business_Name='$businessname', " .
    "Category='$category', " .
    "Sub_Category='$subcategory', " .
    "Neighborhood='$neighborhood', " .
    "Address_1='$address1', " .
    "Address_2='$address2', " .
    "City='$city', " .
    "State='$state', " .
    "Phone_Number='$phonenumber', " .
    "E_Mail='email', " .
    "Web_Site='$website', " .
    "Contact_Name='$contactname', " .
    if (mysql_query($sql)) {
    echo("<P>New Business Listing Added</P>");
    } else {echo("<P>Error Adding New Business Listing: " .mysql_error() . "</P>");

    <P><A HREF="<?php echo($PHP_SELF); ?>">
    Add Another Business Listing</A></P>
    <P><A HREF="authors.php">Return to Authors list</A></P>
    <?php else: // Allow The User To Enter A New Business Listing
    <FORM ACTION="<?php echo($PHP_SELF); ?>" METHOD=POST>
    <P>Enter Business Information:<BR>
    Business Name:
    <INPUT TYPE=TEXT NAME="businessname" SIZE=50 MAXLENGTH=100><BR>
    Sub Category:<select multiple name="subcategory[]">
    <option value="Test">Test
    <option value="Test2">Test 2

  2. #2
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Melbourne, Australia
    2 Post(s)
    0 Thread(s)
    The problem is that you're trying to assign multiple values to a single column in your database. There are technical reasons why this won't work, and database design reasons why you shouldn't be trying to do it.

    First of all, when you submit your form a variable called $subcategory is created. This isn't a run-of-the-mill variable though. As indicated by the name attribute of your select tag, which calls the variable subcategory[] (note the square brackets) it's an array.

    An array is a single variable designed to store multiple values. For example, in your form, if both options are selected then $subcategory[0] will be "Test" and $subcategory[1] will be "Test2". $subcategory alone refers to the entire array, which doesn't have any meaningful value when you try to assign it as the value of your database column.

    Have a read of Part 5 (Relational Database Design) and Part 6 (A Content Management System) of my article series "Building a Database-Driven Web Site using PHP and MySQL" on for more information on how to design your database to store multiple categories for each business and how to write the PHP code to do this.

    If you have any remaining questions, feel free to ask!

    -Kevin Yank.
    Helping Small Business Grow Online!


Posting Permissions

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