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>
    
      <? } ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题