Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2013
    Posts
    2

    Unanswered: inserting data from one table into another, help please!

    Ok so I want to insert a new zone into my database, a zone should be linked with a building, at the moment I have made a form so that I can enter a new zone linked with a building but I have to manually type in the buildingID, however I want all of the buildings from the buildings table to be shown in a drop-down list so I can select it rather than having to type in the buildingID

    hope this makes sense, many thanks to anyone who can help me!

    PHP Code:
    <?php
    if (isset($_POST['submit'])){

    $con mysql_connect ("localhost","xxx","xxx");
    if (!
    $con){
    die(
    "can not connect: " mysql_error());
    }
    mysql_select_db("xxx",$con);

                                    
                                    
    $sql "INSERT INTO zones (Zonename,Zonecapacity,buildingID) VALUES ('$_POST[Zonename]','$_POST[Zonecapacity]','$_POST[Buildingname]')";
    $inserted=mysql_query($sql,$con);
        if(
    $inserted) {
                echo 
    "New Zone succesfully entered";
            } else { 
                echo 
    "fail "mysqli_error($con);
            };
        } else { 
            echo 
    "<form action='insertzone1.php' method='post'>
                    Insert New Zone:<br><br>
                    Zone Name:<br><input type='text' name='Zonename'><br><br>
                    Zone Capacity:<br><input type='int' name='Zonecapacity'><br><br>
                    Building:<br><input type='int' name='Buildingname'><br><br>
                    <input type='submit' name='submit'>
                    </form>"
    ;

    mysql_close($con);
    };
    Last edited by eade; 04-24-13 at 18:00.

  2. #2
    Join Date
    Mar 2004
    Posts
    21
    Sounds like you want to do another sql statement that pulls your current buildings table data (buildingID and building name) and then use the results to build your drop-down list. You could use a do/while for the drop-down list build. For your drop-down list, use the buildingID as the value and the building name for what is displayed in the list.

    Statement:
    $sqlBuildings = "SELECT buildingID, buildingName FROM buildings";
    $rsBuildings = mysqli_query($sqlBuildings, [link]) or die(mysql_error());
    $row_rsBuildings = mysql_fetch_assoc($rsBuildings);

    Drop-down:
    <select name="buildingID">
    <?php do { ?>
    <option value="<?php echo $rsBuildings['buildingID']?>"><?php echo $rsBuildings['buildingName']?></option>
    <?php } while ($row_rsBuildings = mysql_fetch_assoc($rsBuildings)); ?>
    </select>
    ---------------------
    Thanks
    David

Posting Permissions

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