0

I am making a checkbox filter, I need to place the associative array chosen from the chekboxes in the sql statement so that in this case it shows the results of these two categories but I have not yet been able to achieve it if you can help me thanks

$array_category = array(
"1" => "design",
"2" => "food",

);

if ($sentence= $db->query("SELECT * FROM post WHERE state = 1  AND category IN ".implode(",", $array_category).")"){
1
  • It should be "SELECT * FROM post WHERE state = 1 AND category IN ('".implode("','", $array_category)."')" Commented Nov 30, 2020 at 3:24

1 Answer 1

1

It should be

"SELECT * FROM post WHERE state = 1  AND category IN ('".implode("','", $array_category)."')"

Update 1: For sql injection

\DB::select(\DB::raw("SELECT * FROM post WHERE state = 1 AND category IN (" . str_repeat("?,",count($array_category)-1) . "?" . ")"), $array_category)
Sign up to request clarification or add additional context in comments.

3 Comments

This is vulnerable to SQL injection.
I just only correct Razer 's sql syntax error
Right, but apparently the OP is unaware of that as well.

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.