Single drop down using MySQL with text input field

Hi,

We are searching for some simple PHP code for several days, but haven't found it so far.

The page should be presenting a drop down and below that a text input field + submit button.

The selection/input/submit actions create a new record in one table (insert into). The drop down is another table where the ID is the same as the one being inserted and displaying the name field in the drop down list.

There is a similiar post on this site with the name: INSERT VALUE FROM DROP DOWN BOX TO THE DATABASE USING PHP
[link=http://www.programmersheaven.com/mb/phpstuff/394518/394936/re-insert-value-from-drop-down-box-to-the-database-using-php/?S=B20000][/link]

However this solution is causing problems and is not using/retrieving values from a MySQL table.

Your help will be much appreciated!

Thank you.

Alex.

Comments

  • Hi.

    How far have you gone with a solution.

    I think you would have to do the first insert, get the id from the first insert and then use it as an id in the second table.

    Let us know what you table structure is.

    so we can help better
  • Hi,

    Thank you for your reply. We are not very familiar with MySQL/PHP yet, so below code is not perfect. But first our table structure: it is with innodb and connected with foreign keys. A Continent table with ContinentName, ContinentID and a table Country with CountryName, CountryID, ContinentID (ContinentID=linked).

    The PHP code so far:

    <?php
    // Connect database
    include("config.php");

    $sql="SELECT ContinentID, ContinentName FROM Continent ORDER BY ContinentName ASC";
    $result=mysql_query($sql);

    $options="";

    while ($row=mysql_fetch_array($result)) {

    $id=$row["ContinentID"];
    $continent_name=$row["ContinentName"];
    $options.="<OPTION VALUE="$id">".$continent_name;
    }

    if (isset($_POST['submit'])) {

    //This makes sure they did not leave any fields blank
    if (!$_POST['CountryName'] ) {
    die('You did not complete all of the required fields');
    }

    // checks if the country is in use
    if (!get_magic_quotes_gpc()) {
    $_POST['CountryName'] = addslashes($_POST['CountryName']);
    }
    $countrycheck = $_POST['CountryName'];
    $check = mysql_query("SELECT CountryName FROM Country WHERE CountryName = '$countrycheck'")
    or die(mysql_error());
    $check2 = mysql_num_rows($check);

    //if the name exists it gives an error
    if ($check2 != 0) {
    die('Sorry, the Country '.$_POST['CountryName'].' is already in use.');
    }

    // now we insert it into the database
    $insert = "INSERT INTO Country (CountryName, ContinentID)
    VALUES ('".$_POST['CountryName']."', '".$_POST['ContinentID']."')";
    $add_member = mysql_query($insert);
    ?>

    country created

    Thank you, you have created a country - you may now return to selecting your location details.

    <?php
    }
    else
    {
    ?>


    " method="post"> Choose
    <?=$options.="<OPTION VALUE="$id">".$continent_name.'';?>







    <?php
    }
    ?>

    click location details



    CountryName:

  • Is there already an answere for our question? We are still looking for our dropdown with input field+submit solution.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories