简体   繁体   English

PHP清单获取ID和值并将其存储

[英]PHP checklist get ID and value and store it

So I have a form to add a new item to database with a checkbox as follows 所以我有一个表单,可以通过复选框将新项目添加到数据库,如下所示

在此处输入图片说明

在此处输入图片说明

So my difficulty is the checkbox. 所以我的困难是复选框。 I can easily enough create the array for all items checked but I need an ID for them along with it. 我可以轻松地为所有检查的项目创建数组,但是我需要为其提供ID。 I've tried to think of many ways and searched a lot but I just can't think of a way to get the ID in a way that is then useable to me along with the name of the feature (checklist). 我尝试过多种方法并进行了很多搜索,但我只是想不出一种方法来获取ID,然后将该ID与功能名称(清单)一起使用。 Since I have to get each feature item and add it to the table houses_has_features. 由于我必须获取每个功能项并将其添加到表houses_has_features中。

<?php
$title = 'Add a new house';
require_once 'header.php';
require_once 'nav.php';
require_once 'mysqli-con.php';

$conn = new MYSQLI($hn, $un, $pw, $db);

// If house name and type is set then add them into the database
if( !empty($_POST['h_name']) && !empty($_POST['h_type']) ) {
    $house_name = $conn->real_escape_string($_POST['h_name']);
    $house_type = $conn->real_escape_string($_POST['h_type']);

    //show names added
    echo '<b>House name: </b>'.$house_name . '<br><b> House type:</b>  ' . $house_type;

    $query = "INSERT INTO `house_names` (`id`, `name`) VALUES (NULL, '$house_name')";
    $result = $conn->query($query);
    if (!$result) die ("<b class='text-danger'><p>Insert failed ERRROR: " . $conn->error. "</p>");

    global $house_name_id;
    $house_name_id = $conn->insert_id;
    $query = "INSERT INTO `house_types` VALUES ('$house_name_id', '$house_type')";
    $result = $conn->query($query);
    if (!$result) die ("<b class='text-danger'><p>Insert failed ERRROR: " . $conn->error. "</p>");
    } else {
    global $house_name_id;
    $house_name_id= NULL;
    }



//Start container for page content
echo '<div class="container">';

//Display an error message if house name is filled in but not house type
if ( !empty($_POST['h_name']) && empty($_POST['h_type']) || empty($_POST['h_name']) && !empty($_POST['h_type']) ) {
    echo "<p class='error-text'>* Please fill in both the house name and house type *</p>";
}




$query_feat = $conn->query('SELECT * FROM features');
$rows = $query_feat->num_rows;


$features_list  = $_POST['check_list'];
$feature_id = $_POST['feature_id'];
//display checked boxes.
if(isset($_POST['check_list'])) {
    for ($i=0; $i<sizeof($features_list); $i++){

        //echo '<br>House name id:' . $house_name_id . '<br> $_POST[] = ' . "$features_list[]";
        print_r($features_list); echo '<br>';
        print_r($feature_id);
    }
}




// Add house form
echo <<<_END


    <h1>Add a house</h1>
</div>
<div class="container">
    <form action="add.php" method="post">
        <p>House Name: <input type="text" name="h_name"></p>
        <p>House type: <input type="text" name="h_type"></p>
        <b>features:</b> 
        <ul class="list-group">
_END;
        for ($c = 0 ; $c < $rows ; ++$c){

        $query_feat->data_seek($c);
        $feat = $query_feat->fetch_array(MYSQLI_NUM);


        echo '<li><input type="checkbox" name="check_list[]" value="' .$feat[1]. '">'.$feat[1].'</li>';

}


echo <<<_END
        <ul>
        <input class="btn-primary" type="submit" value="Submit">
    </form>
</div>

_END;



require_once 'footer.php';

I'm really lost on this one any help would be greatly appreciated :) 我真的迷失了这一点,任何帮助将不胜感激:)

change your value of checkbox to id or anything you want. 将您的复选框的值更改为id或您想要的任何值。

<li><input type="checkbox" name="check_list[]" value="' .$feat[0]. '">'.$feat[1].'</li>


$feat[1] => $feat[0] or else $ feat [1] => $ feat [0]否则

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM