开发者

Populate checkbox from database

I have two tables first one consist of id`s which are associated with names 1 - Car 2 - Train 3 - Plain ..etc

The second table consists of two fields user_id and an id from the first table (for example 1) 1 2 1 3

I`m trying to repopulate selected 开发者_StackOverflow社区checkboxes when user goes to the page


First you query the database for results, your query should look like this (fill in the correct table and column names):

SELECT user_id, t2.id, name
FROM table1 t1
LEFT JOIN table2 t2 ON t1.id = t2.id 

Then add the following to your PHP code. mind you: I have not tested this, so it's possible it needs some debug.

 $qry = "SELECT user_id, t2.id, name
        FROM table1 t1
        LEFT JOIN table2 t2 ON t1.id = t2.id";
    $res = mysql_query($qry);

    while($row = mysql_fetch_array($result))
      {
      echo "<input type='checkbox' name='" + $row['name'] +"' checked='";
     if($row['user_id'] != NULL) echo "checked";
    echo "' />";
      }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜