为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
函数式编程 2022-09-12 12:53:51
我正在尝试用我的mysql数据库中的数据填充复选框,但由于某种原因,只有最后一个复选框被选中(例如,如果应该检查汽车,木工和手工工具,只有手工工具被检查),我无法弄清楚为什么。mysql 语句运行正常,并提供了正确的信息。这是相关代码。<?phprequire_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 ="";      }
查看完整描述

1 回答

?
HUWWW

TA贡献1874条经验 获得超12个赞

您正在循环访问您的结果。这意味着对于每个循环,您将一个变量设置为“选中”,其余变量设置为空字符串。因此,将只检查最后一个。创可贴修复是将取消选中设置为循环外的默认,然后仅在需要时更改为选中。


但真正的解决方法是从数据库中提取此信息并使用它,而不是手动将数据库 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_id

FROM category c

LEFT JOIN tool_category tc ON c.category_ID = tc.category_id

    AND tc.tool_id = ?";

$stmt = $db->prepare($sql);

$stmt->bind_param("i", $_GET["id"]);

$result = $stmt->execute();


while($row = $stmt->fetch_assoc()) {

    $id = $row["category_ID"];

    $name = $row["category_name"];

    $checked = $row["tool_id"] ? "checked" : "";

    $tool["categories"][$id] = ["name" => $name, "checked" => $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 ?>


查看完整回答
反对 回复 2022-09-12
  • 1 回答
  • 0 关注
  • 59 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信