使用PHP填充数据库中的复选框-只有最后一个选项被选中

使用PHP填充数据库中的复选框-只有最后一个选项被选中,php,mysql,Php,Mysql,我试图用mysql数据库中的数据填充复选框,但出于某种原因,只选中最后一个复选框(例如,如果应该选中汽车、木工和手动工具,则只选中手动工具),我不知道为什么。mysql语句正在正确运行,并提供了正确的信息。这是相关代码 <?php require_once('../../private/initialize.php'); require_login(); if(!isset($_GET['id'])) { redirect_to(url_for('/members/show_mem

我试图用mysql数据库中的数据填充复选框,但出于某种原因,只选中最后一个复选框(例如,如果应该选中汽车、木工和手动工具,则只选中手动工具),我不知道为什么。mysql语句正在正确运行,并提供了正确的信息。这是相关代码

<?php

require_once('../../private/initialize.php');
require_login(); 
if(!isset($_GET['id'])) {
  redirect_to(url_for('/members/show_member_tools.php'));
}
$id = $_GET['id'];

if(is_post_request()) {

  // Handle form values sent by new.php

  $tool = [];
  $tool['tool_ID'] = $id;
  $tool['serial_number'] = $_POST['serial_number'] ?? '';
  $tool['tool_name'] = $_POST['tool_name'] ?? '';
  $tool['tool_description'] = $_POST['tool_description'] ?? '';
  $tool['tool_picture'] = $_POST['tool_picture'] ?? '';
  $category =[];
  $category = $_POST['category_ID'];
  $result = update_tool($tool, $category);

    //get info for checkboxes
    global $db;


  if($result === true) {
    $_SESSION['message'] = "The tool has been updated sucessfully";
    redirect_to(url_for('/members/show_tool.php?id=' . $id));
  } else {
    $errors = $result;
  }


} else {

  $tool = find_tool_by_id($id);
      if(isset($_GET['id'])){
    $id=$_GET['id'];
    $sql = "select category_name from category INNER JOIN tool_category ON category.category_ID = tool_category.category_ID where tool_category.tool_id=$id";
    $query = mysqli_query($db, $sql);

    while($row=mysqli_fetch_array($query)) {

//      $str = "";
      $str = $row['category_name'];
      echo $str;


      if (strpos($str , "automotive")!== false){
        $checked1 ="checked";
        echo "made it to automotive";
        } else {
        $checked1 ="";
      }

      if (strpos($str , "carpentry")!== false){
        $checked2 ="checked";
        echo "made it to carpentry";
        } else {
        $checked2 ="";
      }

      if (strpos($str , "home maintenance")!== false){
        $checked3 ="checked";
        echo "made it to home maintenance";
      } else {
        $checked3 ="";
      }

      if (strpos($str , "plumbing")!== false){
        $checked4 ="checked";
      } else {
        $checked4 ="";
      }

      if (strpos($str , "yard and garden")!== false){
        $checked5 ="checked";
      } else {
        $checked5 ="";
      }

      if (strpos($str , "hand tools")!== false){
        $checked6 ="checked";
      } else {
        $checked6 ="";
      } 

    }//end while loop    

  } //end if

} //end else
  $tool_set = find_all_tools();
  $tool_count = mysqli_num_rows($tool_set);
  mysqli_free_result($tool_set);
?>

<?php $page_title = 'Edit Tool'; ?>
<?php include(SHARED_PATH . '/header.php'); ?>

<div id="content">

  <div class="center">
    <a href="<?php echo url_for('/members/show_member_tools.php'); ?>">&laquo; Back to My Tools</a>


    <h2>Edit Tool</h2>
  </div>
    <?php echo display_errors($errors); ?>
    <form action="<?php echo url_for('/members/edit_tool.php?id=' . h(u($id))); ?>" method="post">

      <fieldset class="form">
         <img src ="<?php echo h($tool['tool_picture']); ?>"  alt="<?php echo h($tool['tool_picture']); ?>"width="150"><br>
        <label for="serial_number">Serial Number</label><br>
          <input type="text" name="serial_number" value="<?php echo h($tool['serial_number']); ?>" ><br>

        <label for="tool_name">Tool Name</label><br>
          <input type="text" name="tool_name" value="<?php echo h($tool['tool_name']); ?>" ><br>

        <label for="tool_description">Tool Description</label><br>
          <input type="text" name="tool_description" value="<?php echo h($tool['tool_description']); ?>" ><br>
        <label for="category_ID">Tool Category: </label><br>  
         <input type="checkbox" name="category_ID[]" value="1" <?php echo $checked1; ?>> <label for="1">Automotive</label> <br>
         <input type="checkbox" name="category_ID[]" value="2" <?php echo $checked2; ?>> <label for="2">Carpentry</label> <br>
         <input type="checkbox" name="category_ID[]" value="3" <?php echo $checked3; ?>> <label for="3">Home Maintenance</label> <br>
         <input type="checkbox" name="category_ID[]" value="4" <?php echo $checked4; ?>> <label for="4">Plumbing </label><br>
         <input type="checkbox" name="category_ID[]" value="5" <?php echo $checked5; ?>> <label for="5">Yard and Garden</label> <br>
         <input type="checkbox" name="category_ID[]" value="6" <?php echo $checked6; ?>> <label for="6">Hand Tools</label> <br>

        <input type="submit" value="Edit Tool" >

          <a class="block" href="<?php echo url_for('/members/delete_tool.php?id=' . $id); ?>">Delete Tool</a>

      </fieldset>

    </form>
    <div class="push"></div>
  </div>

<?php include(SHARED_PATH . '/footer.php'); ?>

编辑工具

你在重复你的结果。这意味着在每个循环中,您将一个变量设置为“checked”,其余变量设置为空字符串。因此,将只检查最后一个。助写器修复是将未选中的设置为循环的默认<强>外部< /强>,然后仅在需要时更改为选中。

但真正的解决办法是从数据库中提取这些信息并使用它们,而不是手动将数据库ID映射到标签。通过将条件移动到联接中,可以提取所有类别。将选中具有工具ID的行,而不选中其他行。您还可以提取类别名称和ID,以便以编程方式构建复选框

有关DB示例,请参见此处:

现在,您可以执行此操作来自动生成所有复选框输入:

<?php foreach ($tool["categories"] as $id=>$category): ?>
    <input type="checkbox" name="category_ID[]" id="category_<?=$id?>" value="<?=$id?>" <?=$category["checked"]?>>
    <label for="category_<?=$id?>">
        <?=htmlspecialchars($category["name"])?>
    </label><br/>
<?php endforeach ?>


您在何处以及如何将
$checked1
定义为
$checked6
?能否添加获取$checked变量的位置/方式?很抱歉,它没有发布我的整个代码块,将发布答案
<?php foreach ($tool["categories"] as $id=>$category): ?>
    <input type="checkbox" name="category_ID[]" id="category_<?=$id?>" value="<?=$id?>" <?=$category["checked"]?>>
    <label for="category_<?=$id?>">
        <?=htmlspecialchars($category["name"])?>
    </label><br/>
<?php endforeach ?>