1

I am trying to populate dropdown list values from mysql and show it inside the td of html table,i tried with below code but it not populating values from mysql can any help me how to do that.

<table id="CPH_GridView1" style="width:1452px">
    <thead>
        <tr>
            <th style=" width:102px">Clien ID </th>
            <th style=" width:100px">Country</th>       
            <th style=" width:248px">Network Name </th>    
            <th style="text-align:center; width:102px" >cppn </th>        
        </tr>
    </thead>
    <tbody>
    <?php 
    $sql = mysql_query("SELECT * FROM clientpricenotifications");
    while($rows=mysql_fetch_array($sql))
    {
        if($alt == 1)
        {
            echo '<tr class="alt">';
            $alt = 0;
        }
        else
        {
            echo '<tr>';
            $alt = 1;
        }   
        echo '<td id="CPH_GridView1_clientid" style="width:140px" class="edit clientid '.$rows["id"].'">'.$rows["clientid"].'</td>
            <td id="CPH_GridView1_country" style="width:160px" class="edit country '.$rows['id'].'">'.$rows["country"].'</td>       
            <td id="CPH_GridView1_networkname" style="width:156px" class="edit networkname '.$rows["id"].'">'.$rows["networkname"].'</td>';
    ?>
        <td>
            <select name=' . customer_name . '>  
            <?php  
            $query = 'SELECT cppn FROM clientpricenotifications';  
            $result = mysql_query($query, $db) or die(mysql_error($db));  
            while ($row = mysql_fetch_assoc($result))  
            {  
                echo '<option value="' . $row['id'] . '"> ' . $row['cppn'] .     '</option>';  
            }   
            ?>  
            </select>
        </td>       

    </tr>'

}

?>

3 Answers 3

1

There seems to be a problem with this line:

<td> <select name='customer_name'>

Shouldn't it actually say either this:

<td> <select name="customer_name">

Or:

<td> <select name=' . customer_name . '>

And, that line is part of an echo statement that contains a string in single-quotes, but I can't see where the echo statement's closing single-quote is.

As a result, I think a large bulk of your output is being ignored by the browser because the tag is not being closed properly as some of the output is getting mangled. Check your output with View Source!

Sign up to request clarification or add additional context in comments.

3 Comments

Ah, I see that "david" has edited the original for better formatting. With his edits, the line above isn't in an echo, which makes more sense and makes it easier to read, and removes the error in the original. (The "name" field still uses single-quotes, whereas I'm sure HTML standards say that property values should be in double-quotes.)
i have edited my code now think error showing in line <td> <select name=' . customer_name . '>
Now this bit "<select name=' . customer_name . '>" isn't is an echo any more. My comments above were for when it was in an echo. If "customer_name" is a variable then you need to use serverside code to output it, either put it back in an echo, or, if it is just a static field name, use the first option I suggested above.
1

If your above code is complete, then I would guess that you're missing the connection to the MySQL server. See: http://www.php.net/manual/en/function.mysql-connect.php

For a related question with code sample, check the answer at: Create table with PHP and populate from MySQL

Not asked, but your table has non matching column widths defined in the styles: Clien(t) ID header 102px, while data cells are 140px.

Comments

0

Another place to look for is following line:

<td style="width:65px" class=" '.$rows["id"].'">

I would expect it should be the following:

<td style="width:65px" class="<?php echo $rows["id"] ?>">

As Vexen Crabtree mentioned, if you also check/post the html code of the HTML output, it would make it easier to diagnose the problem.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.