0

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}; ?>">&nbsp;
    <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

  • I think this misplaced dollar sign is throwing the error: `$_POST$[{'tache' . $i}]`. It seems that it should be: `$_POST[${'tache' . $i}]`. – showdev Oct 09 '17 at 17:20
  • Why is `tache` not an array? – gre_gor Oct 09 '17 at 17:25
  • var_dump($_POST) and check your data please. And then, check $_POST$[{'tache' . $i}] bacuase $_POST$ it's wrong. And check value ${'tache'.$i} and check is this value exist in the $_POST – Maxi Schvindt Oct 09 '17 at 17:33
  • @showdev yup, my bad, typo error, I think I was tired .. i don't have error : Parse error: syntax error, unexpected '$' in #my_file# on line 148 but Undefined variable for each loop. working on this... – LordOptimistic Oct 09 '17 at 19:10
  • @Cuchu thanks, in fact it was a typo, i'll work on this thanks – LordOptimistic Oct 09 '17 at 19:11

0 Answers0