Click to See Complete Forum and Search --> : Retrieving values of checkboxes


chuckylefrek
09-20-2006, 09:39 AM
I have the following checkboxes on one page



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>



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:



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());
}



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

chuckylefrek
09-20-2006, 09:57 AM
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



<?php

if (isset($source)) {

if ($source == 'add_musician.htm') {

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'];

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

}

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

}

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

}

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

}

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

}

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

}

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

}

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

}

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

}

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

}

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

}


$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());
}



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