[PHP-DB] Re: looped insert

2001-10-23 Thread bill

Hi Russ,

It's not clear that you're keeping track of which categories are being checked.

You name them category[$names]  but you refer to them as $category[$i] where $i is a 
number.  How will you know what $category[1] refers to?

Other issues: It's also not clear where the value of $email comes from.  Nor is it 
clear if you're trying to obtain one row in the database with all subscriptions or 
whether
you could have multiple lines for multiple subscriptions.

Try this

while (list($key,$val)= each ($HTTP_POST_VARS[category]) ) {

$thisname=$key;  // this is the name of the checkbox, as in $category[$name]
$thisval=$val;   // this is the value, which in every case is yes

$sql=INSERT INTO $myanet02($thisname) values ($email);
// this will produce a line in the db for each subscription

// if you want one line for all subscriptions, remvoe the $sql line above and
// uncomment these lines below
// to compound the sql and run the query after the while

// if ($thefields) $thefields .= , $thisname; } else { $thefields = $thisname; 
}
// if($thevalues) $thevalues .= ,'$email'  } else { $thevalues = '$email'; }

$query = mysql_query($sql,$connect) or die(mysql_error());

} // end while

// uncomment following lines for single row entry
// $sql= INSERT INTO $myanet02($thefields) values ($thevalues);
// echo $sql;
// $query = mysql_query($sql,$connect) or die(mysql_error());

kind regards,

bill hollett

Russ Michell wrote:

 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 /binput 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;$icount($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-DB] Re: looped insert

2001-10-23 Thread Russ Michell

Relax!

$email comes from a text input, and the categories come from checkboxes dynamically 
displayed from 
table column names:

//Get all table column names, dynamically display, and use to accept subscriptions to 
category updates:
$fields = mysql_list_fields($dbName, myanet_categories, $connect);
$nocolumns = mysql_num_fields($fields);
for ($i=0; $i$nocolumns; $i++) {
$names = mysql_field_name($fields, $i) . \n;
$catselect .= b$names /binput type=\checkbox\ 
name=\category[$names]\ value=\yes\br\n;
}

I got it to work thus:

//Start off sql
$sql = INSERT INTO $myanet02 (;
//start display confirmation of subscribed categories
$category_list = ul\n\t;
//loop while categories have been ticked
while(list($key,$val) = each($category)) {
//trim whitespace from the end of $key (inherited from original var declaration of 
'$names' above)
$sql1 .= rtrim($key) . ,;
$sql2 = substr($sql1,0,-1). ); 
$sql3 .= '$email',;
$sql4 = $sql . $sql2 .  VALUES ( . $sql3;
//continue display confirmation of subscribed categories
$category_list .= li$key/li\n;
}
$category_list .= /ul;
//Remove the last comma from data to be inserted
$sql = substr($sql4,0,-1) . );
$query = mysql_query($sql,$connect) or die(mysql_error());  
$confirm = You have now been subscribed to the following categories: $category_list;
}
}

Thanks v.much for your concern, but it works quite well now!
Cheers.

Russ

On Tue, 23 Oct 2001 11:39:30 -0400 bill [EMAIL PROTECTED] wrote:

 Hi Russ,
 
 It's not clear that you're keeping track of which categories are being checked.
 
 You name them category[$names]  but you refer to them as $category[$i] where $i is a 
number.  How will you know what $category[1] refers to?
 
 Other issues: It's also not clear where the value of $email comes from.  Nor is it 
clear if you're trying to obtain one row in the database with all subscriptions or 
whether
 you could have multiple lines for multiple subscriptions.
 
 Try this
 
 while (list($key,$val)= each ($HTTP_POST_VARS[category]) ) {
 
 $thisname=$key;  // this is the name of the checkbox, as in $category[$name]
 $thisval=$val;   // this is the value, which in every case is yes
 
 $sql=INSERT INTO $myanet02($thisname) values ($email);
 // this will produce a line in the db for each subscription
 
 // if you want one line for all subscriptions, remvoe the $sql line above and
 // uncomment these lines below
 // to compound the sql and run the query after the while
 
 // if ($thefields) $thefields .= , $thisname; } else { $thefields = 
$thisname; }
 // if($thevalues) $thevalues .= ,'$email'  } else { $thevalues = '$email'; }
 
 $query = mysql_query($sql,$connect) or die(mysql_error());
 
 } // end while
 
 // uncomment following lines for single row entry
 // $sql= INSERT INTO $myanet02($thefields) values ($thevalues);
 // echo $sql;
 // $query = mysql_query($sql,$connect) or die(mysql_error());
 
 kind regards,
 
 bill hollett
 
 Russ Michell wrote:
 
  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 
 /binput 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;$icount($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 WebteamRoom 1C 'The Eastings' East Road, 
Cambridge
 e: [EMAIL PROTECTED]
w: www.apu.ac.uk/webteam