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

    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:
    if (isset($_POST['submit'])){

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

    $sql "INSERT INTO zones (Zonename,Zonecapacity,buildingID) VALUES ('$_POST[Zonename]','$_POST[Zonecapacity]','$_POST[Buildingname]')";
    $inserted) {
    "New Zone succesfully entered";
            } else { 
    "fail "mysqli_error($con);
        } else { 
    "<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'>

    Last edited by eade; 04-24-13 at 18:00.

  2. #2
    Join Date
    Mar 2004
    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.

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

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

Posting Permissions

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