I’m trying to write a select query in MySQL which display all column names with checkboxes in the frontend form and allow the user to choose which of to use in the select query. I am OK with using $_POST to post across the values but I’m not sure how I can put these into my query string correctly as some users may select 1, while others may select 3.
You should use radio buttons if the user can only choose one option. I am unsure what you’re trying to do, maybe someone else does or maybe you can explain more in detail?
The first thing you should do is form validation to make sure that at least one checkbox is checked. If you’re using POST, you can check for length of a trimmed value. If it’s 0, then none of the checkboxes are checked.
In POST, if the checkboxes all have the same name, it should create a comma-delimited list of values. If you’re using parameterized queries, you should (theoretically) just pass that value for the SELECT:
SELECT var_name_for_POST_value
FROM tableA
WHERE {conditionals}
I think he’s asking how to create the query to allow for 3 different values.
He could do this by looping through all array values sent from the checkbox array (use a basic select statement grabbing 1 value at a time), or he can properly build the query to allow all 3 values to be grabbed by the SQL.
[quote=“RyanReese, post:6, topic:125710”]
I think he’s asking how to create the query to allow for 3 different values.[/quote]
okay, here’s the SQL for that –
SELECT Forename
, Surname
, Nationality
FROM daTable
WHERE ...
i don’t do php, but i think the answer he’s looking for is the explode function to produce comma-delimited names of columns