If you can update the prospect object's methods, I would recommend adding a method that will add the prospect to the database for you. Another solution would be a method that returns all of the data for you in an array or hash. Barring that, the following really ugly hack could work:

{ # Get the new prospect my $prospect = shift; # Connect to the database my $dbh = DBI->connect('DBI:mysql:menagerie', 'menagerie') or die "Couldn't connect to database:" . DBI->errstr; # Set all of the variables that we need to pass to MySQL my @data = qw/ name address address2 state c +ity phone phone2 phone3 phone4 phoneType phone2Type phone3Type phone4Type email url interestLevel designFirm hostingFirm companyName /; my @insert_data; my $place_holders = '?,' x @data; chop $place_holders; # remove trailing comma foreach ( @data ) { no strict; my $method = $data[$_]; push @insert_data, $prospect->$method; } # Insert the new prospect in the database my $sth = $dbh->prepare("INSERT INTO prospect VALUES ( $place_ +holders )" ); # Execute the statement $sth->execute( @insert_data ) or die print DBI->errstr; # Lets get the prospect id that we just added back my $ID = $dbh->{'mysql_insertid'}; return $ID; }

The problem, of course, is when you turn off strict for the method calls, you run the risk of unintentionally calling the wrong method if you misspelled it. Further, if you have inherited the misspelled method, you could be in big trouble. The solution would be to write an autoload routine that detects whether or not the method exists and croaks rather than allowing you to inherit something. Of course, if you can write the autoload method, you could write the other methods :)

If you can't change the prospect methods, subclass it and add your own method. Much safer that way!

Other comments: you don't have a password in your connect string. If you left it off because you don't want us to see it, good for you! If you left it off because there is no password, shame on you! :)

I also noticed that you have repetitive phone data. As soon as someone has a fifth phone, your schema breaks. If you need to have multiple phones, create a separate table for them.

Cheers,
Ovid

Vote for paco!

Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.


In reply to (Ovid) Re: MySQL / DBI / OOP by Ovid
in thread MySQL / DBI / OOP by rogueFalcon

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.