Just thought this would be helpful... I've found the occassional problem with trying to insert a script-generated null value (rare but it does happen). If this becomes a problem, remember that NULL is just another word for "nothing". MySQL will accept either of the following:
INSERT INTO table VALUES ('value1', 'value2', NULL, 'value3'); INSERT INTO table VALUES ('value1', 'value2', , 'value3'); NOTE: I didn't read the entire post below...it's too early in the morning to think about code. I'm off to populate some site content until my brain catches up with the clock. Mike Frazer "Russ Michell" <[EMAIL PROTECTED]> wrote in message news:SIMEON.10110231044.D@k1c. anglia.ac.uk... > Hi Rick: > > Cheers for the info. Thwe following code almost does it, however, the NULL value isn't inserted, if > checkboxes are != to 'yes': > > //If submit button selected, process those ticked checkbox categories: > if(isset($submit)) { > //Start off sql > $sql = "INSERT INTO $myanet02 (staffdev,strategies,rules,news,information,policies) VALUES("; > //loop while categories have been ticked > for($i=0;$i<count($category);$i++) { > if($category[$names] == 'yes') { > $sql .= "'$email',"; > } > else { > $sql .= "NULL,"; > } > } > //Remove the last comma > $sql = substr($sql,0,-1).")"; > //$query = mysql_query($sql,$connect) or die(mysql_error()); > echo $sql; > } > > I believe the problem to be this line: > 'if($category[$names] == 'yes') {' //Use this and NULL values aren't inserted > > 'if($HTTP_POST_VARS["category${$i}"] == 'yes') {' //Use this and ONLY NULL values are inserted > > The values are taken from the checkboxes thus: > > //Get all table column names, dynamically display them and use to process subscriptions: > $fields = mysql_list_fields("$dbName", "myanet_categories", $connect); > $columns = mysql_num_fields($fields); > for ($i=0; $i<$columns; $i++) { > $names = mysql_field_name($fields, $i) . "\n"; > $catselect .= "<b>$names </b><input type=\"checkbox\" name=\"category[$names]\" value=\"yes\"><br>\n"; > } > > You can see it happening at: > http://gertrude.anglia.ac.uk/intranet/myanet/index.php > > Many thanks. > Russ > > > > > On Mon, 22 Oct 2001 12:28:38 -0500 Rick Emery <[EMAIL PROTECTED]> wrote: > > > Construct your INSERT a piece at a time. Something like: > > > > $sql = "INSERT INTO $myanet02 > > (staffdev,strategies,rules,news,information,policies) VALUES("; > > > > for($i=0;$i<count($category);$i++) { > > if($HTTP_POST_VARS["category{$i}"] == 'yes') { > > $sql .= "\'$email\',"; > > } > > else { > > $sql .= "NULL,"; > > } > > } > > $sql = substring($sql,0,-1).")"; > > //$query = mysql_query($sql,$connect) or die(mysql_error()); > > echo $sql; > > > > -----Original Message----- > > From: Russ Michell [mailto:[EMAIL PROTECTED]] > > Sent: Monday, October 22, 2001 11:23 AM > > To: [EMAIL PROTECTED] > > Subject: [PHP-DB] looped insert > > > > > > Hi everyone: > > > > I've been fiddling with this all day, and well - if it was working you > > wouldn't be reading this right now! ;-) > > My script should take the selections made by users and process them via > > straightforward MySQL inserts: > > > > //Get all table column names, dynamically display them as checkboxes. Use to > > process subscriptions: > > $fields = mysql_list_fields("$dbName", "myanet_categories", $connect); > > $columns = mysql_num_fields($fields); > > for ($i=0; $i<$columns; $i++) { > > $names = mysql_field_name($fields, $i) . "\n"; > > $catselect .= "<b>$names </b><input type=\"checkbox\" > > name=\"category[$names]\" value=\"yes\"><br>\n"; > > } > > > > > > //If submit button selected, process those ticked checkbox categories: > > if(isset($submit)) { > > //loop while categories have been ticked > > for($i=0;$i<count($category);$i++) { > > if($HTTP_POST_VARS["category{$i}"] == 'yes') { //Not sure > > about this bit... > > $sql = "INSERT INTO $myanet02 > > (staffdev,strategies,rules,news,information,policies) > > VALUES('$email','$email','$email','$email','$email','$email')"; > > //$query = mysql_query($sql,$connect) or > > die(mysql_error()); > > echo $sql; > > } > > } > > } > > > > At the moment not even echoing my $sql works. I know this aint gonna work as > > it is, because what I really need to know is: > > > > * How do I insert into only those categories that have been checked? > > (default is 'null' in a VARCHAR(50) field) > > * How do I only declare '$email' a single time as this is the only value > > being inserted? > > > > Many thanks: > > Russ > > > > #-------------------------------------------------------# > > > > "Believe nothing - consider everything" > > > > Russ Michell > > Anglia Polytechnic University Webteam > > Room 1C 'The Eastings' East Road, Cambridge > > > > e: [EMAIL PROTECTED] > > w: www.apu.ac.uk/webteam > > > > www.theruss.com > > > > #-------------------------------------------------------# > > > > > > -- > > PHP Database Mailing List (http://www.php.net/) > > To unsubscribe, e-mail: [EMAIL PROTECTED] > > For additional commands, e-mail: [EMAIL PROTECTED] > > To contact the list administrators, e-mail: [EMAIL PROTECTED] > > > > > > > > -- > > PHP Database Mailing List (http://www.php.net/) > > To unsubscribe, e-mail: [EMAIL PROTECTED] > > For additional commands, e-mail: [EMAIL PROTECTED] > > To contact the list administrators, e-mail: [EMAIL PROTECTED] > > > > #-------------------------------------------------------# > > "Believe nothing - consider everything" > > Russ Michell > Anglia Polytechnic University Webteam > Room 1C 'The Eastings' East Road, Cambridge > > e: [EMAIL PROTECTED] > w: www.apu.ac.uk/webteam > > www.theruss.com > > #-------------------------------------------------------# > -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]