/    Sign up×
Community /Pin to ProfileBookmark

Retrieving values of checkboxes

I have the following checkboxes on one page

[code]

Brass: <input class=”fieldStyle” type=”checkbox” name=”brass” style=”border: 0px”><br>
Early Music: <input class=”fieldStyle” type=”checkbox” name=”earlymusic” style=”border: 0px”><br>
Jazz: <input class=”fieldStyle” type=”checkbox” name=”jazz” style=”border: 0px”><br>
Keyboards: <input class=”fieldStyle” type=”checkbox” name=”keyboards” style=”border: 0px”><br>
Percussion: <input class=”fieldStyle” type=”checkbox” name=”percussion” style=”border: 0px”><br>
Rock and Pop: <input class=”fieldStyle” type=”checkbox” name=”rockandpop” style=”border: 0px”><br>
Strings: <input class=”fieldStyle” type=”checkbox” name=”strings” style=”border: 0px”><br>
Traditional: <input class=”fieldStyle” type=”checkbox” name=”traditional” style=”border: 0px”><br>
Vocal: <input class=”fieldStyle” type=”checkbox” name=”vocal” style=”border: 0px”><br>
Woodwind: <input class=”fieldStyle” type=”checkbox” name=”woodwind” style=”border: 0px”><br>
World: <input class=”fieldStyle” type=”checkbox” name=”world” style=”border: 0px”><br>

[/code]

And I have a field in my database for each of these checkboxes

i.e the database contains the following fields

brass
earlymusic
jazz
etc

Basically the page adds the details of a new musician to the database. Each musician can have multiple categories of music.

My code to update the database is as follows:

[code]

include “config.php”;
$db = mysql_connect($db_host,$db_user,$db_pass);
mysql_select_db ($db_name) or die (“Cannot connect to database”);

$name = $_POST[‘bandname’];

$brass = $_POST[‘brass’];
$earlymusic = $_POST[‘earlymusic’];
$jazz = $_POST[‘jazz’];
$keyboards = $_POST[‘keyboards’];
$percussion = $_POST[‘percussion’];
$rockandpop = $_POST[‘rockandpop’];
$strings = $_POST[‘strings’];
$traditional = $_POST[‘traditional’];
$vocal = $_POST[‘vocal’];
$woodwind = $_POST[‘woodwind’];
$world = $_POST[‘world’];

$website = $_POST[‘link’];
$description = $_POST[“description”];

$query = “INSERT INTO musicianstb(name, brass, earlymusic, jazz, keyboards, percussion, rockandpop, strings, traditional, vocal, woodwind, world, website, description)
VALUES(‘$name’,’$brass’,’$earlymusic’,’$jazz’,’$keyboards’,’$percussion’,’$rockandpop’,’$strings’,’$traditional’,’$vocal’,’$woodwind’,’$world’,’$website’,’$description’)”;
$result = mysql_query($query);

if (!$result) {
die(‘Invalid query: ‘ . mysql_error());
}

[/code]

But unfortunately it is setting a value of zero for all the checkbox fields in the database even if they have been checked.

Any help getting this to work much appreciated.

Thanks

Paul

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@chuckylefrekauthorSep 20.2006 — I have managed to get it working using the following code but it seems like alot of code to do this.

I would appreciate any advice on this. I am new to this PHP malarkey and learning on the job and would like to get started on a good footing.

Here is the code

<i>
</i>
&lt;?php

if (isset($source)) {

<i> </i>if ($source == 'add_musician.htm') {

<i> </i> include "config.php";
<i> </i> $db = mysql_connect($db_host,$db_user,$db_pass);
<i> </i> mysql_select_db ($db_name) or die ("Cannot connect to database");

<i> </i> $name = $_POST['bandname'];

<i> </i> if(isset ($_POST['brass'])) {
<i> </i> $brass = 1;
<i> </i> } else {
<i> </i> $brass = 0;

<i> </i> }

<i> </i> if(isset ($_POST['earlymusic'])) {
<i> </i> $earlymusic = 1;
<i> </i> } else {
<i> </i> $earlymusic = 0;

<i> </i> }

<i> </i> if(isset ($_POST['jazz'])) {
<i> </i> $jazz = 1;
<i> </i> } else {
<i> </i> $jazz = 0;

<i> </i> }

<i> </i> if(isset ($_POST['keyboards'])) {
<i> </i> $keyboards = 1;
<i> </i> } else {
<i> </i> $keyboards = 0;

<i> </i> }

<i> </i> if(isset ($_POST['percussion'])) {
<i> </i> $percussion = 1;
<i> </i> } else {
<i> </i> $percussion = 0;

<i> </i> }

<i> </i> if(isset ($_POST['rockandpop'])) {
<i> </i> $rockandpop = 1;
<i> </i> } else {
<i> </i> $rockandpop = 0;

<i> </i> }

<i> </i> if(isset ($_POST['strings'])) {
<i> </i> $strings = 1;
<i> </i> } else {
<i> </i> $strings = 0;

<i> </i> }

<i> </i> if(isset ($_POST['traditional'])) {
<i> </i> $traditional = 1;
<i> </i> } else {
<i> </i> $traditional = 0;

<i> </i> }

<i> </i> if(isset ($_POST['vocal'])) {
<i> </i> $vocal = 1;
<i> </i> } else {
<i> </i> $vocal = 0;

<i> </i> }

<i> </i> if(isset ($_POST['woodwind'])) {
<i> </i> $woodwind = 1;
<i> </i> } else {
<i> </i> $woodwind = 0;

<i> </i> }

<i> </i> if(isset ($_POST['world'])) {
<i> </i> $world = 1;
<i> </i> } else {
<i> </i> $world = 0;

<i> </i> }


<i> </i> $website = $_POST['link'];
<i> </i> $description = $_POST["description"];


<i> </i> $query = "INSERT INTO musicianstb(name, brass, earlymusic, jazz, keyboards, percussion, rockandpop, strings, traditional, vocal, woodwind, world, website, description)
<i> </i> VALUES('$name','$brass','$earlymusic','$jazz','$keyboards','$percussion','$rockandpop','$strings','$traditional','$vocal','$woodwind','$world','$website','$description')";
<i> </i> $result = mysql_query($query);

<i> </i> if (!$result) {
<i> </i> die('Invalid query: ' . mysql_error());
<i> </i> }



And here is a copy of the page that allows the admin people to add a new musician


http://www.superact.org.uk/add_musician.htm


Thanks

Paul
×

Success!

Help @chuckylefrek spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...