Dynamic dropdown menu to database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Bertman105
    New Member
    • Nov 2006
    • 4

    Dynamic dropdown menu to database

    Hey all,

    Heres the situation:
    I have a form page that needs to have multiple drop down menus that is pulling their options from a database. Right now I have one working out of 3. So that is one issue, now the issue that I am having with the one that is working, is that it is not writing anything to the database. No matter what you select, the database is empty. Any help would be sweet.
    So here is the form page, with just a few text fields, and the one dynamic drop down that is working:

    [PHP]<?PHP
    //this code is bringing in the values for the dropdown.
    $sql="SELECT * FROM category";
    $result=mysql_q uery($sql);
    $options="";

    while ($row=mysql_fet ch_array($resul t)) {
    $id=$row["cat"];
    $cat=$row["category"];
    $options.="<OPT ION VALUE=\"$id\">" .$cat.'</option>';

    }
    ?>
    <html>
    <head>
    <title>Manage Spots</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>

    <body><form action="process .php" method="post" enctype="multip art/form-data">
    <p>Title
    <input type="text" name="Title">
    </p>
    <p>Spot #
    <input type="text" name="Spot_Numb er">
    </p>
    <p>Series
    <select name="Series" id="Series">
    <option value="test">te st</option>
    </select>
    &nbsp;&nbsp; <a href="series_ed it.php">Manage Series</a> </p>
    <p>Date
    <input type="text" name="Creation_ Date">
    </p>
    <p>Category #1
    <select name="Category_ One" id="Category_On e">
    <option value="0"><? echo $options ?> </option>
    </select>
    &nbsp;&nbsp; <a href="category_ edit.php">Manag e Categories</a>
    </p>
    <p>&nbsp;</p>
    <p>
    <input type="submit" name="Submit" value="Submit">
    </p>
    </form>[/PHP]

    Then I have it going to a page called process.php that just inserts everything into the database, here is the page if that helps:

    [PHP]<?PHP
    $dbh=mysql_conn ect ("localhost" , "", "") or die ('I cannot connect to the database because: ' . mysql_error());
    mysql_select_db ("snmwebde_ryno db");
    $Title=$_POST{' Title'};
    $Spot_Number=$_ POST{'Spot_Numb er'};
    $Series=$_POST{ 'Series'};
    $Creation_Date= $_POST{'Creatio n_Date'};
    $Category_One=$ _POST{'Category _One'};
    ?>
    <?PHP
    $query = "insert into spots values
    ('$Title','$Spo t_Number','$Ser ies','$Creation _Date','$Catego ry_One')";
    $result = mysql_query($qu ery);
    ?>
    Entry added!!<br>
    To view database click <a href="viewdb.ph p">here</a>.

    </body>
    </html>[/PHP]

    Any help would be really great, seeing if i fix these 2 problems, I am done with this project. Thanks again
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    Your <option> statement is screwed up. Statement [php]<option value="0"><? echo $options ?> </option>[/php] generates [php]<option value="0">
    <option value=xx>yy</option>
    <option value=aa>bb</option>
    </option>[/php]
    I cannot predict the result of this. Fix that first and see the results.

    Ronald :cool:

    Comment

    • Bertman105
      New Member
      • Nov 2006
      • 4

      #3
      ya i noticed that after i posted, still nothing gets posted to the database for that field

      Comment

      • ronverdonk
        Recognized Expert Specialist
        • Jul 2006
        • 4259

        #4
        Originally posted by Bertman105
        Hey all,

        Heres the situation:
        I have a form page that needs to have multiple drop down menus that is pulling their options from a database. Right now I have one working out of 3. So that is one issue, now the issue that I am having with the one that is working, is that it is not writing anything to the database. No matter what you select, the database is empty. Any help would be sweet.
        So here is the form page, with just a few text fields, and the one dynamic drop down that is working:

        [PHP]<?PHP
        //this code is bringing in the values for the dropdown.
        $sql="SELECT * FROM category";
        $result=mysql_q uery($sql);
        $options="";

        while ($row=mysql_fet ch_array($resul t)) {
        $id=$row["cat"];
        $cat=$row["category"];
        $options.="<OPT ION VALUE=\"$id\">" .$cat.'</option>';

        }
        ?>
        <html>
        <head>
        <title>Manage Spots</title>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
        </head>

        <body><form action="process .php" method="post" enctype="multip art/form-data">
        <p>Title
        <input type="text" name="Title">
        </p>
        <p>Spot #
        <input type="text" name="Spot_Numb er">
        </p>
        <p>Series
        <select name="Series" id="Series">
        <option value="test">te st</option>
        </select>
        &nbsp;&nbsp; <a href="series_ed it.php">Manage Series</a> </p>
        <p>Date
        <input type="text" name="Creation_ Date">
        </p>
        <p>Category #1
        <select name="Category_ One" id="Category_On e">
        <option value="0"><? echo $options ?> </option>
        </select>
        &nbsp;&nbsp; <a href="category_ edit.php">Manag e Categories</a>
        </p>
        <p>&nbsp;</p>
        <p>
        <input type="submit" name="Submit" value="Submit">
        </p>
        </form>[/PHP]

        Then I have it going to a page called process.php that just inserts everything into the database, here is the page if that helps:

        [PHP]<?PHP
        $dbh=mysql_conn ect ("localhost" , "", "") or die ('I cannot connect to the database because: ' . mysql_error());
        mysql_select_db ("snmwebde_ryno db");
        $Title=$_POST{' Title'};
        $Spot_Number=$_ POST{'Spot_Numb er'};
        $Series=$_POST{ 'Series'};
        $Creation_Date= $_POST{'Creatio n_Date'};
        $Category_One=$ _POST{'Category _One'};
        ?>
        <?PHP
        $query = "insert into spots values
        ('$Title','$Spo t_Number','$Ser ies','$Creation _Date','$Catego ry_One')";
        $result = mysql_query($qu ery);
        ?>
        Entry added!!<br>
        To view database click <a href="viewdb.ph p">here</a>.

        </body>
        </html>[/PHP]

        Any help would be really great, seeing if i fix these 2 problems, I am done with this project. Thanks again
        Have you investigated any errors coming out of the select, like this:
        [php]$result=mysql_q uery($sql)
        or die("Select query error: " . mysql_error());[/php]
        At least you know then what the actual problem might be and where to look further.

        Ronald :cool:

        Comment

        • Bertman105
          New Member
          • Nov 2006
          • 4

          #5
          see there are no errors because everything else will write to the database, i just don't know if its even possible to take info like that and put it into a database

          Comment

          • ronverdonk
            Recognized Expert Specialist
            • Jul 2006
            • 4259

            #6
            Have you print_r the $_POST array to see if anything is passed in Category_One?

            Ronald :cool:

            Comment

            • Bertman105
              New Member
              • Nov 2006
              • 4

              #7
              Originally posted by ronverdonk
              Have you print_r the $_POST array to see if anything is passed in Category_One?

              Ronald :cool:
              how do i do that?

              Comment

              • ronverdonk
                Recognized Expert Specialist
                • Jul 2006
                • 4259

                #8
                As the first statements in your script:[php]<?php
                echo '<pre>'; // makes it better readable on screen
                print_r($_POST) ;[/php]This will show you all values passed t and stored in the $_POST associative array.

                Ronald :cool:

                Comment

                Working...