11 messages in com.mysql.lists.perlRe: Multiple inserts using a perl "fo...
FromSent OnAttachments
dupon16 Jul 2001 08:36 
dupon16 Jul 2001 09:11 
Robert Goff16 Jul 2001 10:47 
dupon16 Jul 2001 11:10 
Artiom Morozov16 Jul 2001 11:20 
Chuck O'Donnell16 Jul 2001 11:27 
Dodger16 Jul 2001 12:06 
dupon16 Jul 2001 12:16 
Dodger16 Jul 2001 12:40 
dupon16 Jul 2001 12:47 
Robert Goff16 Jul 2001 13:20 
Subject:Re: Multiple inserts using a perl "foreach" loop
From:dupon (dup@wlgroup.com)
Date:07/16/2001 12:47:56 PM
List:com.mysql.lists.perl

Sorry Chuck, I did a "chomp($num)" (please no remarks) I also did a "s/\n//g" I got the same results. Besides, if that was the case, the first $num would not insert either. However, the first one does, and the following ones do not. Dodger had a good idea I will have to try. However, I am still kind of raw at this and I am a bit nervous about using "use strict". For now, I like to give myself cushion. But I guess I will have to do that sooner or later in order to bring myself to the higher levels of programming prowess. I'll give it shot.

Check to make sure your file does not have carriage returns or other characters in it. Perl will leave them there, but mysql will stop processing and report the number as "0" since it looks like a string.

On Mon, Jul 16, 2001 at 01:10:52PM -0500, dupon wrote:

You would be correct. The print statement gives me exactly what I expect. Here is an example of some entries in my @ITEMS array.

10:Jim Bob 12:John Doe 24:Jane Smith

The actual insert statement pulls only the number (It is the persons ID number. The name itself is just for human readability). The string of data to be inserted is simply... NULL (auto increment), $Ida (which is a group ID number), and $num (which is the persons ID number). See blow...

"$sth->execute(NULL,$IDa,$num);"

After running the script, the html page reads like this (I'm only printing number and name)...

10 - Jim Bob 12 - John Doe 24 - Jane Smith

But my MySQL table looks like this...

ID | Group ID | User ID |

--------------------------- 122 | 13 | 0 |

--------------------------- 123 | 13 | 0 |

--------------------------- 124 | 13 | 10 |

---------------------------

But it should look like this....

ID | Group ID | User ID |

--------------------------- 122 | 13 | 24 |

--------------------------- 123 | 13 | 12 |

--------------------------- 124 | 13 | 10 |

---------------------------

Does this help?

#################################################################

$IDa = "variable"; #predeclared value for $IDa

#create general "prepare" statement $sth = $dbh->prepare("insert into <table_name> values(?,?,?)");

foreach $line (@ITEMS) {

($num,$name) = split(/:/,$line); #split $line at the ":"

$sth->execute(NULL,$IDa,$num); #execute each $line in @ITEMS

print "$num - $name<br>\n"; #print each $line in @ITEMS }

#################################################################

As you can see, if there were 10 items in "@ITEMS", each item should insert (one per $line in @ITEMS) with NULL (incremental number), $IDa (unique value), and $num (another unique value). However, only the first insert in the loop inserts correctly. All following items do insert also, except with the $num variable always being "0" (the $num variable prints onto the web page just fine (no, it is not "0")). Am I explaining this correctly? I can't get over this issue. Any help would be appreciated.

Offhand, I don't see an error in your code. I assume that the print statement gives you the anticipated results?

To unsubscribe, send a message to the address shown in the List-Unsubscribe header of this message. If you cannot see it, e-mail msql@lists.mysql.com instead.

To unsubscribe, send a message to the address shown in the List-Unsubscribe header of this message. If you cannot see it, e-mail msql@lists.mysql.com instead.