i've search for a topic similar but found nothing, I try to get my table name to create checkbox form, so people can check things in the form, and we insert the value of those checkbox in the good column in the database.
Right now the checkbox appear
$sql = "SELECT `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA`='#the name of my database#' AND `TABLE_NAME`='#the name of the table#'";
$result = mysqli_query($conn,$sql);
$i = '1';
while($row = mysqli_fetch_assoc($result))
{
$long_nom_colomne = print_r($row, true); // get my column name
$long_nom_colomne_fin = substr($long_nom_colomne, 28); // cut the beginning of the string: Array ( [COLUMN_NAME] =>
$nom_colomne = substr($long_nom_colomne_fin, 0, -2); // cut the end
${'tache' . $i} = $nom_colomne; // i got the name of the column in a variable $var name
$i++;
}
$debut = '12'; // start at $tache12 because i don't want to show 1 to 11 column name
$i = $debut;
$tache = mysqli_query($conn,$sql);
while($row = mysqli_fetch_assoc($tache))
{
if(isset(${'tache' . $i}))
{
?>
<label>
<input type="checkbox" name="<?php echo ${'tache' . $i}; ?>" value="<?php echo ${'tache' . $i}; ?>">
<span class="wpcf7-list-item-label"><?php echo ${'tache' . $i}; ?></span>
</label>
//each check box get their name from the column name = (i.e. cat, dog, fish, ...), get their value (i.e. cat, dog,....) and appear as a label. THIS WORK
<?php
}
$i++;
}
In my page of processing to put information in the database, I have
$sql = "SELECT `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA`='#name of my database#' AND `TABLE_NAME`='#name of the table#'";
$result = mysqli_query($conn,$sql);
$i = '1';
while($row = mysqli_fetch_assoc($result))
{
$truc = isset($_POST[${'tache' . $i}]) ? $_POST[${'tache' . $i}] : 'don t work';
echo $truc;
$i++;
}
$debut = '12'; // will be send in a post so i only have one place to change it if i want
$i = $debut;
$set_value = mysqli_query($conn,$sql);
while($row2 = mysqli_fetch_assoc($set_value))
{
if(isset(${'tache' . $i}))
{
$t = ${'tache' . $i};
echo $t; // to test if its not ${'tache' . $i}; who don't work in the request
${'sql_' . $s} = "INSERT INTO tache ($t) VALUES
('".mysqli_real_escape_string($conn,$t)."')";
}
$i++;
}
$i = $debut;
And I got the error :
Parse error: syntax error, unexpected '$' in #my_file# on line 148
Notice: Undefined variable
It's something i've never try to do before, but it's to avoid the need to change all the code each time I add a column in my database