RE: [PHP-DB] looped insert

From: Date: Mon, 22 Oct 2001 17:28:38 +0000
Subject: RE: [PHP-DB] looped insert
Groups: php.db 
Request: Send a blank email to php-db+get-13611@lists.php.net to get a copy of this message
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:r.j.michell@apu.ac.uk] Sent: Monday, October 22, 2001 11:23 AM To: php-db@lists.php.net 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: r.j.michell@apu.ac.uk w: www.apu.ac.uk/webteam www.theruss.com #-------------------------------------------------------# -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, e-mail: php-db-unsubscribe@lists.php.net For additional commands, e-mail: php-db-help@lists.php.net To contact the list administrators, e-mail: php-list-admin@lists.php.net

« previous php.db (#13611) next »