Generalised select query with multiple values

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.

This is what I have so far:

Front end form:

<form action="back.php" method="post">
<input type="checkbox" name="person" value="forename">Forename<br>
<input type="checkbox" name="person" value="surname">Surname<br>
<input type="checkbox" name="person" value="nationality">Nationality<br>
</form>

Back end:

$person=$_POST["person"];

$query = "select" . $person . " from Actor WHERE nationality = British";

Many thanks for your help

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?

He basically wants to know how to create his SQL statement to account for any range of entries coming from the checkbox, 1,2 or 3 values.

He cannot use radios for this since users have the option to select up to 3.

This isn’t really a PHP question, but rather a SQL question since he needs help with his SQL. I’m moving the thread accordingly.

i disagree, ryan, i think he’s asking a php question –

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}

But I’m not that familiar with MySQL.

Just my thoughts.

:slight_smile:

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.

That is why I think this is 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

i.e. a php answer :wink:

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