3 Replies - 28113 Views - Last Post: 19 January 2012 - 07:19 AM Rate Topic: -----

#1 Jezzabeanz   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 19-September 11

Multiple checkbox values into one column within database

Posted 18 January 2012 - 04:53 PM

Hi,

I am attempting to create a rota system. I am trying to add multiple checkbox values into one column within MySQL.

Example:

Role: "Bar, Waiter"

When the manager is designating staff for a shift the system will show all staff who can work on the relevant role with the following SQL query.
SELECT * FROM Employees WHERE role like '%bar%'; 


   <div class="cell-i"><input type="checkbox" name="role" value="Floor" /> Floor</div>
	<div class="cell-i"><input type="checkbox" name="role" value="Bar" /> Bar</div>
        <div class="cell-i"><input type="checkbox" name="role" value="Chef" /> Chef</div>


I'm sure I need to turn the name "role" into an array, however, how would I prepare them for the database concatenated?

Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Multiple checkbox values into one column within database

#2 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3786
  • View blog
  • Posts: 13,715
  • Joined: 08-August 08

Re: Multiple checkbox values into one column within database

Posted 18 January 2012 - 06:31 PM

Make role an array and then implode it.

<form name="test" method="post" action="check.php" >
<input type="checkbox" name="role[]" value="1" />1
<input type="checkbox" name="role[]" value="2" />2
<input type="checkbox" name="role[]" value="3" />3
<input type="checkbox" name="role[]" value="4" />4
<input type="submit" value="Submit" >
</form>


check.php:
<?php
if(isset($_POST['role'])) {
	$role = implode(",", $_POST['role']);	
} else {
	$role = "";
}
echo "role: ".$role;
?>

Was This Post Helpful? 2
  • +
  • -

#3 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: Multiple checkbox values into one column within database

Posted 19 January 2012 - 07:15 AM

I would think you need a separate Roles table and a Join Table.
Was This Post Helpful? 1
  • +
  • -

#4 Jezzabeanz   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 19-September 11

Re: Multiple checkbox values into one column within database

Posted 19 January 2012 - 07:19 AM

Thank you, CTphpnwb. I will use your method.

Also, thank you JackOfAllTrades, I was previously had a join table however I thought it redundant. I was obviously wrong. The solution CTphpnwb offered will work just fine however.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1