I have strange problems inserting data in a MySQL (3.23.36) table. In a apparently casual way, my data are truncated. Here the code I'm using:

sub insert_msg { my $author = $query->param('author'); my $subject = $query->param('subject'); my $body = $query->param('body'); my $parent = $query->param('parent'); my $thread; if ( $parent ) { my $sth = $dbh->prepare(qq|select id, thread from messaggi whe +re id=?|); $sth->execute( $parent ) || die $DBI::errstr; my $hr = $sth->fetchrow_hashref(); $thread = ($hr->{'thread'} == 0) ? $hr->{'id'} : $hr->{'thread +'}; } else { $thread = 0; } $body = validate( $body ); $body = HTML::Entities::encode( $body ); $body =~ s/\[(.*?)\|(.*?)\]/target($1, $2)/eg; $body =~ s/\[(.*?)\]/target($1)/eg; my $sth = $dbh->prepare( qq|insert into messaggi values(null, ?, n +ull, ?, ?, ?, null)| ); $sth->execute( $thread, $author, $subject, $body ) || die $DBI::er +rstr; $template->process( $config->{'template'}->{'insert'} ); }

Here the table's description:

CREATE TABLE `messaggi` ( `id` int(10) unsigned NOT NULL auto_increment, `thread` int(10) unsigned default NULL, `data` timestamp(14) NOT NULL, `author` varchar(100) default NULL, `subject` varchar(200) default NULL, `body` text, `category` int(10) unsigned default NULL, PRIMARY KEY (`id`), FULLTEXT KEY `body` (`body`), ) TYPE=MyISAM

As you can guess, it's a mysql-based webforum. Very simple.
First of all, I fetch information about the thread where the message has to be inserted. Then, I validate the $body using HTML::Parser, stripping HTML tags that are not valid. Then, I replace links with appropriate HTML, like in PerlMonks. Eventually, I insert the message in the database.

Sometime the $body is truncated, casually, apparently. For example, inserting the same message after some minute, the body is inserted correctly.

Thank you for every advice.


In reply to Truncated data using MySQL and DBI by larsen

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.