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

我正在尝试用我的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 ="";

      }


函数式编程
浏览 60回答 1
1回答

HUWWW

您正在循环访问您的结果。这意味着对于每个循环,您将一个变量设置为“选中”,其余变量设置为空字符串。因此,将只检查最后一个。创可贴修复是将取消选中设置为循环外的默认,然后仅在需要时更改为选中。但真正的解决方法是从数据库中提取此信息并使用它,而不是手动将数据库 ID 映射到标签。通过将条件移动到连接中,可以拉取所有类别。将检查具有工具 ID 的行,而不检查其他行。您还将提取类别名称和 ID,以便以编程方式生成复选框。有关 DB 示例,请参阅此处:http://sqlfiddle.com/#!9/20b223/14/0$tool = find_tool_by_id($id);$tool["categories"] = [];$sql = "SELECT c.category_name, c.category_ID, tc.tool_idFROM category cLEFT JOIN tool_category tc ON c.category_ID = tc.category_id&nbsp; &nbsp; AND tc.tool_id = ?";$stmt = $db->prepare($sql);$stmt->bind_param("i", $_GET["id"]);$result = $stmt->execute();while($row = $stmt->fetch_assoc()) {&nbsp; &nbsp; $id = $row["category_ID"];&nbsp; &nbsp; $name = $row["category_name"];&nbsp; &nbsp; $checked = $row["tool_id"] ? "checked" : "";&nbsp; &nbsp; $tool["categories"][$id] = ["name" => $name, "checked" => $checked];}现在,稍后您可以执行此操作以自动构建所有复选框输入:<?php foreach ($tool["categories"] as $id=>$category): ?>&nbsp; &nbsp; <input type="checkbox" name="category_ID[]" id="category_<?=$id?>" value="<?=$id?>" <?=$category["checked"]?>>&nbsp; &nbsp; <label for="category_<?=$id?>">&nbsp; &nbsp; &nbsp; &nbsp; <?=htmlspecialchars($category["name"])?>&nbsp; &nbsp; </label><br/><?php endforeach ?>
打开App,查看更多内容
随时随地看视频慕课网APP