Why is this posting the ID instead of the name?

Discussion in 'PHP' started by Matt Ridge, Dec 30, 2011.

  1. #1
    I have a drop down menu that pulls successfully from a database, when submitting something from the drop down menu the database records the ID number of said company instead of the company name itself. As far as I can tell I've made it so it should post the name and not the ID. Can someone help me out and show me where the code is wrong?

    Thanks.

    P.S. The table in said database is comp, the fields in the table are id_comp and name.

    
    <?php
     require_once('connectvars.php');
            $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
            or die('Error connecting to MySQL server.');
     
    if (isset($_POST['submit'])) {
        $comp = mysqli_real_escape_string($dbc,$_POST['comp']);
    }
    
    
    //Access the Database
        if (!empty($comp)) {
            $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
            or die('Error connecting to MySQL server.');
            
        $query = "INSERT INTO ncmr (comp)
    
    
        VALUES ('$comp')";
        
        $data = mysqli_query($dbc, $query) or die("MySQL error: " . mysqli_error($dbc) . "<hr>\nQuery: $query");
            mysqli_close($dbc);
        }
    
    
    echo "<form method='post'>";
            echo '<fieldset>';
            
                echo'<div id="comp">';
                    $mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); 
                        $mysqli->select_db('comp');
                        echo '<span class="b">Company:&nbsp;&nbsp;</span>';
                        $result = $mysqli->query("SELECT * FROM comp"); 
                        $i = 0;
                    echo "<SELECT name='comp'>\n";
                        while($row = $result->fetch_assoc()) {
                        if ($i == 4) echo '<option value="lines">-----</option>';
                    echo "<option value='{$row['comp_id']}'>{$row['name']}</option>\n";
                        $i++;
                        }
                    echo "</select>\n";
                echo '</div>';
                echo '<div id="button"><input type="submit" value="Submit NCMR" name="submit" /></div>';
        echo '</fieldset>';
    echo '</form>';
    ?>
    Code (markup):

     
    Last edited: Dec 30, 2011
    Matt Ridge, Dec 30, 2011 IP
  2. Basti

    Basti Active Member

    Messages:
    625
    Likes Received:
    6
    Best Answers:
    3
    Trophy Points:
    90
    #2
    Thats because
                    echo "<option value='{$row['comp_id']}'>{$row['name']}</option>\n";
    
    Code (markup):
    The option has a value which is the company id, not the name.
     
    Basti, Dec 30, 2011 IP