-1

New in php, older in MySQL.

I have a dynamically created table in php (colors from a table with an id and aname) with the names of the color and a checkbox for each. I have no value (now) for those checkboxes and I cannot address them.

I want to use those checked boxes to make a SELECT statement:

SELECT polish.*,colors.name 
FROM colors 
   inner join polish on colors.id=polish.colorid 
WHERE colors.name=.... 
   OR colors.name=.... 
 etc.;

something like that

            <table class="std">
                <tr>
                    <th>Colour</th>
                    <th>Include</th>
                    <th colspan="3">&nbsp;</th>
                </tr>

                <?php
                require_once("Includes/db.php");
                $result = polishDB::getInstance()->get_colours()?>
                <?php
                while ($row = mysqli_fetch_array($result)):
                    echo "<tr><td>" . htmlentities($row['name']) . "</td>";
                    ?>
                    <td><input type="checkbox" name="col_list[]" value="ON"/></td>
                    <?php
                    echo "</tr>\n";
                endwhile;
                mysqli_free_result($result);
                ?>
            </table>
5
  • not able to understand .. what is the exact requirement ? Commented Jun 8, 2016 at 10:51
  • I think this has to be change, because I cannot identify the checkboxes, like: value=value1 for the first and value=value2 for the second row of the table <td><input type="checkbox" name="col_list[]" value="ON"/></td> Commented Jun 8, 2016 at 10:54
  • add id as a value. Commented Jun 8, 2016 at 10:54
  • lolka_bolka please be specific, how to do that? Write the code. Thanks Commented Jun 8, 2016 at 11:03
  • I find the answer here, thanks stackoverflow.com/questions/4074846/php-dynamic-checkboxes?rq=1 Commented Jun 8, 2016 at 11:06

2 Answers 2

0

If your SQL is correct and returns correct rows so using your code you can do it just like You did it for a

$row['name']

<input type="checkbox" name="col_list[]" value="<?=$row['columnname']?>"/>

the columnname is a column name from database which stores the colour

Sign up to request clarification or add additional context in comments.

Comments

0

You need to assign values to the checkboxes - use the ID from the returned rows from the database, e.g.:

<td><input type="checkbox" name="col_list[]" value="<?= $row['id'] ?>"/></td>

Once the form is submitted, the $_POST will then contain an array (called col_list) of the selected colour IDs.

If I understand the second part of the question correctly, you can use that array to create your SQL SELECT query. You can use the MySQL 'IN' syntax instead of multiple 'OR's.

$colourIds = implode(', ', $_POST['col_list']);
$query = <<<SQL
SELECT polish.*, colors.name 
FROM colors 
INNER JOIN polish ON colors.id = polish.colorid 
WHERE colors.id IN ({$colourIds})
SQL;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.