#!/usr/bin/perl
use DBI;
sub insert_user {
 my ($nim, $nama, $password) = @_;
 my ($insert_sql,$sth);
 $database = "practice_perl";
 $dbuser = "hendra";
 $dbpasswd = "hendra";
 $dbhost = "localhost";
 $dsn = "DBI:mysql:database=$database;host=$dbhost";
 $dbh = DBI->connect($dsn,$dbuser,$dbpasswd) || die $DBI::errstr;
 $insert_sql = qq {insert into user (nik,name,password) values ('$nik', '$name', 
'$password')};

 $sth = $dbh->prepare($insert_sql) || die "prepare: $insert_sql: $DBI::errstr";
 $sth->execute || die "Unable to execute query: $dbh->errstr\n";
 $sth->finish;
 $dbh->disconnect;
}#end insert_user
 
sub insert_log {
 my ($input) = @_;
 my ($insert_sql_log);
 $database = "practice_perl";
 $dbuser = "hendra";
 $dbpasswd = "hendra";
 $dbhost = "localhost";
 $dsn = "DBI:mysql:database=$database;host=$dbhost";
 $dbh = DBI->connect($dsn,$dbuser,$dbpasswd) || die $DBI::errstr;
 $insert_sql_log = "insert into log values ('',".$dbh->quote($input).")";
 $dbh->do($insert_sql_log) || die ($dbh->errstr);
 return $dbh->{'mysql_insertid'};
 $dbh->disconnect;
}#end insert_log
 
print "INPUT : ";
while ($input = <>) {
  #format insert : nik,name,password
  if($input =~/^(\d{8}),(.*),(\w{6})$/i){
     insert_user($1,$2,$3);
  }else{
    insert_log($input);
    print "Wrong Format";
  }#end if
  print "\nINPUT : ";
}#end while
 
hai everyone, i have write scripts perl on the top and if i input data 12345678,Hendra 
Kusnandar,123456 data will insert
into table user, and if i input wrong format data will insert into log.
this scripts is work but if i insert again 12345678,Hendra Kusnandar,123456 scripts 
will error and the message are : DBD::mysql::st execute failed: Duplicate entry 
'12345678' for key 1 at ./test.pl line 20, <> line 1.
Unable to execute query: DBI::db=HASH(0x81e1ed0)->errstr
i know field nik in table user on mysql is primary key that i can't insert same 
data...how to correct this scripts if i insert the same data and will show the message 
on shell "cannot insert, same data".
second i want if data insert into table user also data can insert into table log

I have tried every way possible I can think of and I cannot get it to work
at all. Any help with this would be greatly appreciated.
 
regards,

hendra kusnandar
 

 


---------------------------------
Do you Yahoo!?
Yahoo! Finance Tax Center - File online. File on time.

Reply via email to