Mysql column populating drop down menu issue

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Philth
    New Member
    • Oct 2007
    • 38

    Mysql column populating drop down menu issue

    Hi there,

    I've essentially got a form with several drop down, each populated by columns in various tables.

    The populating bit works fine - the column rows appear as they should in the menu.

    Ideally the user needs to make their selections, and enter the form into a new database table.

    The problem is, when the selection is entered into the new table, only the first word of the string is entered. For example, the drop down may have...

    "Category One"
    "Category Two"
    "Category Three"

    When the form is submitted only the word "Category" is entered into the database.

    Here is the code, it takes the column 'courseTitle' from one table, displays it in a menu, and then saves the selection into a new table as 'eventName'.

    Code:
    <?
    include("connect.php"); 
    $query="SELECT courseTitle FROM course";
    $result = mysql_query ($query);
    echo "<select name='eventName'=''></option>";
    while($nt=mysql_fetch_array($result)){//Array or records stored in $nt
    echo "<option value=$nt[courseTitle]>$nt[courseTitle]</option>";
    }
    echo "</select>";// Closing of list box 
    ?>

    Apologies if I've made it slightly long winded.
  • dlite922
    Recognized Expert Top Contributor
    • Dec 2007
    • 1586

    #2
    I don't know why you have an equal sign and extra quotes here:

    Code:
    "<select name='eventName'=''></option>";
    but debug your post vars like this
    Code:
     die(print_r($_POST))
    and see if the data gets there fine.

    If that's fine, look in your insert SQL (not shown in your code). Just print it and see if it looks right.

    If you don't find anything, post that code back here and any other notes/comments.

    Apologies if I've made it slightly long winded.
    Nope, it's perfect. I wish every user provided this concise amount of info.



    Dan

    Comment

    • Philth
      New Member
      • Oct 2007
      • 38

      #3
      Many thanks for the reply Dan,

      I didn't notice the extra quotes. They made no difference either way.

      The insert SQL is nothing special, I can't spot anything wrong with it.

      Code:
      <? 
      
      if($_POST['submit'])  
      {
      include("connect.php"); 
         
         $eventName = $_POST['eventName'];
         $eventCategory1 = $_POST['eventCategory1'];
         $eventCategory2 = $_POST['eventCategory2'];
         $eventCategory3 = $_POST['eventCategory3'];   
         $eventLocation = $_POST['eventLocation'];
         $eventDate = $_POST['eventDate'];   
      	
         $result=MYSQL_QUERY("INSERT INTO events (eventName, eventCategory1, eventCategory2, eventCategory3, eventLocation, eventDate)".
            "VALUES ('$eventName', '$eventCategory1', '$eventCategory2', '$eventCategory3', '$eventLocation', '$eventDate')"); 
      
         echo "New Course Event Added"; 
      }
      ?>

      Comment

      • dlite922
        Recognized Expert Top Contributor
        • Dec 2007
        • 1586

        #4
        echo $eventName right before the mysql_query() call, and print it's output. I don't know if you know this, but here's how I check my variables when I debug:

        Code:
        die(var_dump($eventname));
        Another thing I can think of is your MySQL table definition. perhaps the field name is not long enough. ALTER TABLE and change that field to VARCHAR(100).

        FYI: You can do "desc events" to give you a definition of the table from a mysql command prompt, if you have access to one.

        Let me know,



        Dan

        Comment

        • Philth
          New Member
          • Oct 2007
          • 38

          #5
          I added the debug line on line 13 of the second script I posted - it returned the value "Null". The field is set to VARCHAR(100).

          Was line 13 the right place to put it?

          Comment

          • Markus
            Recognized Expert Expert
            • Jun 2007
            • 6092

            #6
            Originally posted by Philth
            I added the debug line on line 13 of the second script I posted - it returned the value "Null". The field is set to VARCHAR(100).

            Was line 13 the right place to put it?
            That's because you probably did $eventname where it should be $eventName.

            Anyway, var_dump() your POST array, to check it's being populated correctly by your form.

            Code:
            var_dump($_POST);
            // or
            print_r($_POST);
            Also, with your mysql_query()ies, while debugging, you're provided the function mysql_error() which is populated with a description of the latest mysql error (duh). So, as is popular, we append 'or die(mysql_error ());' to any mysql queries - some people don't like the 'or die()' statements, but it's purely preferential.

            Code:
            mysql_query($query) or die(mysql_error());
            Mark.

            Comment

            • Philth
              New Member
              • Oct 2007
              • 38

              #7
              Mark,

              Thanks for the reply.

              The var_dump gave the following info...

              array(7) { ["eventName"]=> string(6) "Master" ["eventCategory1 "]=> string(4) "VHDL" ["eventCategory2 "]=> string(4) "VHDL" ["eventCategory3 "]=> string(4) "VHDL" ["eventLocat ion"]=> string(7) "Germany" ["eventDate"]=> string(0) "" ["submit"]=> string(13) "Add New Event" }

              Event name should read "Master VHDL Effectively".

              I'm seriously confused.

              Comment

              • Markus
                Recognized Expert Expert
                • Jun 2007
                • 6092

                #8
                Ah, I see the problem: If you do not use quotes around an HTML attribute's value, it'll only read up to the first space as the value. Consider the following:

                Code:
                <input value=my name is mark name=name />
                // $_POST['name'] will hold the value 'my'.
                <input value="my name is mark" name="name" />
                // $_PSOT['name'] will hold "my name is mark"
                Always surround attribute values with quotes.

                Mark.

                Comment

                • Philth
                  New Member
                  • Oct 2007
                  • 38

                  #9
                  Hi Mark,

                  Apologies for not understanding. If the issue is to do with the values before being posted, then the error must be with the first script that I posted?

                  Code:
                  <?
                  include("connect.php"); 
                  $query="SELECT courseTitle FROM course";
                  
                  
                  $result = mysql_query ($query);
                  echo "<select name='eventName'></option>";
                  
                  while($nt=mysql_fetch_array($result)){
                  echo "<option value=$nt[courseTitle]>$nt[courseTitle]</option>";
                  
                  }
                  echo "</select>";
                  ?>
                  All HTML values are quoted.

                  Comment

                  • Dormilich
                    Recognized Expert Expert
                    • Aug 2008
                    • 8694

                    #10
                    Originally posted by Philth
                    Code:
                    echo "<option [U]value=$nt[courseTitle][/U]>$nt[courseTitle]</option>";
                    quoted?

                    ............... ...

                    Comment

                    • Markus
                      Recognized Expert Expert
                      • Jun 2007
                      • 6092

                      #11
                      Originally posted by Dormilich
                      quoted?

                      ............... ...
                      Yeah, what Dorm said. ;)

                      Comment

                      • Philth
                        New Member
                        • Oct 2007
                        • 38

                        #12
                        Yeah, sorry for being dim today.

                        Thanks for everyones help.

                        Comment

                        • Dormilich
                          Recognized Expert Expert
                          • Aug 2008
                          • 8694

                          #13
                          and while we're quoting........ . you should always quote the array's keys (unless they're variables or indeed constants (which is btw rather rare))

                          Comment

                          • dlite922
                            Recognized Expert Top Contributor
                            • Dec 2007
                            • 1586

                            #14
                            Originally posted by Philth
                            Yeah, sorry for being dim today.

                            Thanks for everyones help.
                            Me too, I knew it has something to do with the values not posting correctly, thus form issue.

                            I always quote my html attributes. It's best to always ensure they're valid also:

                            W3C's easy-to-use markup validation service, based on SGML and XML parsers.


                            preferably with XHTML 1.0

                            Cheers,



                            Dan

                            Comment

                            • Dormilich
                              Recognized Expert Expert
                              • Aug 2008
                              • 8694

                              #15
                              Originally posted by dlite922
                              preferably with XHTML 1.0
                              sadly, there is no XHTML support in IE.......

                              Comment

                              Working...