Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

insert into database

Status
Not open for further replies.

cnw40007

Technical User
Mar 4, 2003
35
IE
Hi my code below checks an email address of stored email address in a mysql database and if it isn't in the db insert it. The problem is it is inserting it but when i view the databse there is just a blank line but yet the row number has gone up.

if( /^From: ([0-9a-zA-Z\.\-\_]{13,})+\@[0-9a-zA-Z\.\-]+$/)
{
if username is 14 or more letters.Insert into database,label subject with spam.
$subject = $_;
#$subject =~ s/Subject: /Subject: !SPAM!/g;
for (my $counter=0;$counter<$#list+1;$counter++)
{
$db = &quot;From: $list[$counter]\n&quot;;
if($subject ne $db)
{
my $sql = &quot;INSERT INTO addsubj VALUES ('$subject')&quot;;
}

}
$subject =~ s/: /: !SPAM!/g;
print $subject;
}
my $sth = $dbh->prepare($sql) || die &quot;Error preparing: DBI:errstr&quot;;
my $result = $sth->execute || die &quot;Error executing: $DBI::errstr&quot;;

It must be something with the sql statement but im not to sure.Thanks in advanced cnw40007
 
Looks like $subject doesn't have anything in it. Your print statement, does it print what you expect? Mike

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

It's like this; even samurai have teddy bears, and even teddy bears get drunk.
 
change your SQL to something as

my $sql = qq(INSERT INTO addsubj set subject = '$subject');

or as per your table structure..
---
cheers!
san
pipe.gif


&quot;The universe has been expanding, and Perl's kind of been expanding along with the universe&quot; - Larry Wall
 
I have modified my code $action is set to the email address and it is then set back to 'No' to start the loop again,

if(/^Subject:/ && $action eq 'Yes')
{
$myvar = $_;
$myvar =~ s/: /: !SPAM!/g;
print $myvar;
$action = 'No';
}


if(/^From:\s([0-9a-zA-Z\.\-\_]{13,})+\@[0-9a-zA-Z\.\-]+$/)
{ #if username is 14 or more letters.Insert into database,label subject with spam.

$action = 'Yes';

}
for (my $counter=0;$counter<$#list+1;$counter++)
{
$db = &quot;From: $list[$counter]\n&quot;;
if($action ne $db)
{
my $sql = qq(INSERT INTO addsubj set address = '$action');
}

}
The code gets the email address and labels the subject with spam as its supposed to and prints out $myvar. when i changed my sql statement its still entering a blank line into the database. Any more ideas???
 
That INSERT syntax doesn't look right

INSERT syntax is

INSERT INTO Table VALUES (1,2,3 ...);

Just as you had it, in other words

What columns (fields) are in your table? Which do you want to update? Mike

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

It's like this; even samurai have teddy bears, and even teddy bears get drunk.
 
I have two fields named email and subject and i want to update the email field. $myvar holds the email address so would i use
INSERT INTO addsubj VALUES ('$myvar');
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top