PDO, Array and Superglobal variable $_POST

<?php 
    $alltable = "SHOW TABLES";
    $statement = $pdo->prepare($alltable);
    $statement->execute();
    $result = $statement->fetchAll(PDO::FETCH_ASSOC);
    // echo "<pre>";
    // print_r($result); 
    // echo "</pre>";
    foreach($result as $table) {
    ?>
    <div class="checkbox">
    <label><input type="checkbox" class="checkbox_table" name="table[]" value="<?php echo $table["Tables_in_toolcula_apps"]; ?>" /> <?php echo $table["Tables_in_toolcula_apps"]; ?></label>
    </div>
<?php  } ?>

Hi there,

I have programmatically generated checkboxes based on an array input.
image

Now based on the checkboxes ticked I was looking around the way in which superglobal variable $_POST can hold those elements, which are actually table values from the database.

I wanted to do some operation here:
    if (isset($_POST['download'])) {
      foreach ($_POST['download'] as $table) {
        echo "Yes"."<br />";
      }
    }

But this gives error:
foreach ($_POST['download'] as $table) {

Warning : Invalid argument supplied for foreach() in /home1/…/pdo_back_up_option.php on line 18

Once the checkboxes are ticked doesn’t the superglobal variable $_POST holds the values in an array?

What does it show you, when you display the contents of the $_POST array? Does that give any clues as to why the error occurs?

Does this line give any clue as to why it might be failing?

<label><input type="checkbox" class="checkbox_table" name="table[]" value="<?php echo $table["Tables_in_toolcula_apps"]; ?>" /> <?php echo $table["Tables_in_toolcula_apps"]; ?></label>
1 Like

I realized my knowledge gap:
this helped:

I think we have to pick up from name=table[]

This might help:

$sql = "SHOW TABLES in burroughsFarms"; // Just Testing out on my Database:
$stmt = $pdo->prepare($sql);
$stmt->execute();

$tables = $stmt->fetchAll(PDO::FETCH_COLUMN);

echo "<pre>" . print_r($tables, 1) . "</pre>";

foreach ($tables as $table) {
    echo $table . "<br>";
}
1 Like

I was bored so I did a little more:

$db = DB::getInstance();
$pdo = $db->getConnection();

/* The Above is just my connection string and there is no CSS and hardly any HTML */

$sql = "SHOW TABLES in burroughsFarms"; // Just Testing out on my Database:
$stmt = $pdo->prepare($sql);
$stmt->execute();

$tables = $stmt->fetchAll(PDO::FETCH_COLUMN);

$submit = (htmlspecialchars($_POST['submit'] ?? null));

if ($submit === 'submit') {
    $tables = $_POST['tables'];
    echo "<pre>" . print_r($tables, 1) . "</pre>";
}
?>
<form class="tables" method="post" action="tables.php">
    <?php foreach ($tables as $table) { ?>
        <label><input type="checkbox" class="checkbox_table" name="tables[]" value="<?= $table ?>" ><?= $table ?></label>
    <?php } ?>
    <button type="submit" name="submit" value="submit">Submit</button>
</form>

There’s a small glitch in the script, but I let you figure it out. Beside I think it would make you better understand it.

1 Like

New for me. Thanks for your support.

True or False not 1.

In this case, 1 has the same effect as true, as you can see in your testing.

Something to keep in mind about HTML forms: unchecked boxes aren’t returned. So if I only clicked ‘cars’ and ‘jobs’… the array will only contain two values, instead of the 8 which you may be expecting. One way around this is to prep a default array which blank values for all 8 options, array_merge($form_inputs, $form_default ); This should give you an array with a value for each field that your PDO and then process. Hope that helps

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.