doujia7779 2012-09-13 22:45
浏览 56
已采纳

如果数据库中存在值,请选中复选框

I have a form that allows the user to add or remove users from a job by using checkboxes. At the moment the engineer's names etc are stored in a table called 'users' and they are identified by their user type, engineers. The checkboxes on the form are created by using a while loop to display a mysql query. This is done so that new users can be added to the database via a webpage at a later date and their name will automatically be added to the list of available engineers. To add an engineer to a job their name is simply ticked and the form submitted.

This works well but I would like the user to be able to add or remove users by checking and unchecking boxes on another form but I can't think for the life of me how to do this.

The users assigned to a job are stored in a many-to-many table called calls_engineers which has 3 fields

id_ce (unique id) call_ce (id of job, a foreign key) engineer_ce (id of engineer, a foreign key)

The engineer_ce is the users primary key in the 'users' table which is 'id'.

The code I have so far is...

<?  $sql = "SELECT * FROM calls_engineers WHERE call_ce = '$diary_id'";
      $result = mysql_query($sql) or die(mysql_error());
      $row2 = mysql_fetch_array($result);
      $sql = "SELECT * FROM users WHERE type = 'engineer' ORDER BY first_name";
      $result = mysql_query($sql) or die(mysql_error());
      while ($row = mysql_fetch_array($result)){ ?>
          <div style="width:70px; float:left">
              <? 
                  if($row2['engineer_ce'] == $row['id']){
              ?>
              <input type="checkbox" name="engineer[]" checked="checked" value="<? echo $row['id']; ?>" />
              <? echo '   '.$row['first_name']; 
              } else { ?>
                  <input type="checkbox" name="engineer[]" value="<? echo $row['id']; ?>" />
              <? echo '   '.$row['first_name'];
              }?>
          </div>
  <?
      }

  ?>

All this does is create checkboxes for each user which is an engineer but it doesn't check them if the user is assigned to the job.

Any help will be greatly appreciated

  • 写回答

1条回答 默认 最新

  • dsgdfg30210 2012-09-13 22:51
    关注

    You never loops the calls_engineers table. Using

    $row2 = mysql_fetch_array($result);
    

    Will not actually find all assignments for the engineer. Now check every single engineer if this is included in current job:

    <?  
    
      $sql = "SELECT * FROM users WHERE type = 'engineer' ORDER BY first_name";
      $result = mysql_query($sql) or die(mysql_error());
      while ($row = mysql_fetch_array($result)){ ?>
    
          <div style="width:70px; float:left">
              <input type="checkbox" name="engineer[]" <? 
                  $result2 = mysql_query("SELECT * FROM calls_engineers WHERE call_ce = '".mysql_real_escape_string($diary_id)."' AND engineer_ce = ".$row['id']);
                  if(mysql_num_rows($result2) > 0) echo 'checked="checked"';
                 ?> value="<? echo $row['id']; ?>" />
              <? echo '   '.$row['first_name']; ?>
          </div>
    
      <? } ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择
  • ¥15 这款软件是什么?需要能满足我的需求
  • ¥15 SpringSecurityOauth2登陆前后request不一致
  • ¥15 禅道二次开发编辑版本,上传不了发行包