Re: RE: [PHP-DB] looped insert
| From: | Russ Michell | Date: | Thu, 01 Jan 1970 00:00:00 +0000 |
| Subject: | Re: RE: [PHP-DB] looped insert | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-13622@lists.php.net to get a copy of this message | ||
Hi all + Rick:
Since the last email I have got even closer, here is the code:
//If submit button selected, process those ticked checkbox categories:
if(isset($submit)) {
//Start off sql
$sql = "INSERT INTO $myanet02 (";
//loop while categories have been ticked
while(list($key,$val) = each($category)) {
if($val != 'yes') {
$sql1 .= "$key,";
$sql2 = substr($sql1,0,-1) . ")";
$sql3 .= "'NULL',";
$sql4 = $sql . $sql1 . $sql2 . "VALUES (" . $sql3;
}
else {
$sql1 .= "$key,";
$sql2 = substr($sql1,0,-1) . ")";
$sql3 .= "'$email',";
$sql4 = $sql . $sql1 . $sql2 . "VALUES (" . $sql3;
}
}
//Remove the last comma
$sql = substr($sql4,0,-1) . ")";
//$query = mysql_query($sql,$connect) or die(mysql_error());
echo $sql;
}
However, this returns the following echoed SQL query:
INSERT INTO myanet_categories (staffdev ,staffdev )VALUES ('russ@theruss.com')
This is almost correct, in that I only selected a single category to INSERT INTO (a single email
being the inserted value). But as you can see, that category has been repeated in the list of
fields to be inserted into. If I select say, 3 categories, then this is what happens:
INSERT INTO myanet_categories (staffdev ,rules ,policies ,staffdev ,rules ,policies )
VALUES ('russ@theruss.com','russ@theruss.com','russ@theruss.com')
See what I mean?
If anyone has any other pointers I would be more than happy to receive them!!
Cheers
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
#-------------------------------------------------------#