开发者

Populate checkbox from database

开发者 https://www.devze.com 2023-01-20 12:06 出处:网络
I have two tables first one consist of id`s which are associated with names 1 - Car 2 - Train 3 - Plain ..etc

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

精彩评论

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

关注公众号