0

Im creating a form which has multiple checkboxes based on a sql query so the results values are usually always different

<input type="checkbox" name="warrior[]" value="<? echo $warrior_id; ?>">

but i dont know how to take the values of the boxes that the user has checked from the array. Each checkbox has the id of a row in my database.

I need to run each id that is checked in another query to bring up more details i need for the rest of the script.

"SELECT col1, col2 FROM table WHERE id='$????'"

There is up to 10 values in the array (up to 10 checkboxes)

Thank you for any advise

Mystic
  • 147
  • 1
  • 14

2 Answers2

0

In HTML

<input type="checkbox" name="warrior[]" value="<? echo $warrior_id; ?>">

In PHP - you can implode checked value array. and check with IN in your database

$getvalues = $_GET['warrior']; //only for example i am using this. you have to filter and do validation on it.
$getImplodeValue = implode(',', $getvalues);

$sqlQuery = "SELECT col1, col2 FROM table WHERE id IN '$getImplodeValue'";
shubham715
  • 3,324
  • 1
  • 17
  • 27
  • Note that this code is **not** secure. An attacker could perform an SQL injection. You should use PDO or Prepared Statements and validate each single ID before putting them in your statement. – Œlrim Oct 07 '16 at 04:49
  • please read comment i already told him that i only want to explain that how he can use implode and in . – shubham715 Oct 07 '16 at 04:50
  • thanks, so when i have the query, do i cycle through the results with while($row = mysql_fetch_array($sqlQuery)) { – Mystic Oct 07 '16 at 05:00
  • and also dont forget to upvote and accept answer if its helpful for you ;) . – shubham715 Oct 07 '16 at 05:03
0
  1. check your validation

2.Then use below:

$getWarrior = $_REQUEST['warrior'];  //get value 

$arrWarrior = '';

if(!empty($getWarrior))
{          
  $arrWarrior = implode(',', $getWarrior);
}

$sqlQuery = "SELECT col1, col2 FROM table WHERE 1";
if(!empty($arrWarrior))
{
   $sqlQuery .= " and id IN ($getImplodeValue)";
}
Dipanwita Kundu
  • 1,637
  • 1
  • 9
  • 14