PHP核对表获取ID和值并存储它

时间:2016-08-24 02:46:53

标签: php mysql forms

所以我有一个表单,用一个复选框向数据库添加一个新项目,如下所示

enter image description here

enter image description here

所以我的难度是复选框。我可以轻松地为所有检查的项目创建数组,但我需要一个ID以及它。我试过想了很多方法并且搜索了很多但是我想不出一种方法来获取ID,然后我可以使用该功能以及功能名称(清单)。因为我必须获取每个功能项并将其添加到表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';

我真的迷失在这一点上任何帮助都会非常感激:)

1 个答案:

答案 0 :(得分:2)

将复选框的值更改为id或您想要的任何内容。

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


$ feat [1] =&gt; $ feat [0]或者