jeudi 8 septembre 2016

Run a SELECT statement based on values in a Checkbox Array

I am trying to run a SELECT statement based on the checkboxes selected by the user on the previous page. Each Checkbox has the ID of a Product as a value.

print '<td><input type="checkbox" name="checkbox[]" value="'. $row["id_product"] .'"></td>';

If they select checkboxes 1, 2, 3, 4 and 5 I want the SELECT statement to select the product with the IDs 1 - 5. Here is what I have so far:

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $stmt = $conn->prepare("SELECT reference, price, unit_price_impact FROM ps_product_attribute WHERE id_product = ". implode(',', $_POST['checkbox']) ."");
    echo $stmt;
    $stmt->execute();
    $product = $stmt->fetchAll();

    echo '<div class="table-responsive">';
        echo '<table class="table table-striped">';
                foreach ($product as $row) {
                    print '<tr>';
                        print '<td>'. $row['reference'] .'</td>';
                        print '<td></td>';
                        print '<td>'. $row['price'] .'</td>';
                        print '<td></td>';
                    print '</tr>';
                }
        echo '</table>';
    echo '</div>';
}

Why is the $_POST['checkbox'] value empty?




Aucun commentaire:

Enregistrer un commentaire