RiCHiE RiCHiE - 1 month ago 4x
MySQL Question

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

enter image description here

enter image description here

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. 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). Since I have to get each feature item and add it to the table houses_has_features.

$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>';

// Add house form
echo <<<_END

<h1>Add a house</h1>
<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>
<ul class="list-group">
for ($c = 0 ; $c < $rows ; ++$c){

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

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


echo <<<_END
<input class="btn-primary" type="submit" value="Submit">


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.

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

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