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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Inserting symbols using DBI 1

Status
Not open for further replies.

perlone

Programmer
May 20, 2001
438
US
Hi,

I have a mySQL database and table named news. One field called "subject" which is set to "TEXT" type. EverytimeI add some symbols such as "@" or "'s", it gives me an error saying:

Code:
Cannot execute statement!
You have an error in your SQL syntax near 's', '#', '1', 'Tue Oct 29 21:21:18 2002', 'October', '2002') ' at line 1

I have no idea on how t fix it. Anybody can give me some advice? Here's what I have for the query:

Code:
$sql = <<EOF;
INSERT INTO `news_index` (`nid`, `subject`, `body`, `author`, `post_time`, `month`, `year`) VALUES ('', '$news_subject', '$news_body', '$uid', '$post_time', '$month', '$year')
EOF

my $sth = $dbh->prepare(&quot;$sql&quot;);
$sth->execute or &header(&quot;Cannot execute statement!<br>&quot;.$sth->errstr);

Thanks for your time. There is no Knowledge that is not power.
Age: 17
E-mail: projectnet01@yahoo.com
Company:(not done yet) :-)
Status: Currently working with C++ for game developing. And making a musical band.
-Aaron
 
first, you do not need the field names.
second, get rid of the single quotes.

$sql=$dbh->prepare(&quot;INSERT INTO news_index (nid,subject,body,....) VALUES(?,?,?,?,?,?,?)&quot;);
$sql->execute('', $news_subject, $news_body, $uid, $post_time, $month, $year);

should do it. Of course I assume the variables $news_subject ,... have the right type.

The advantage of using placeholders (?) is that
you call the prepare once and if you are inserting in a loop
you call the execute once per iteration



Good luck, svar
 
Thanks svar! It's working :-) There is no Knowledge that is not power.
Age: 17
E-mail: projectnet01@yahoo.com
Company:(not done yet) :-)
Status: Currently working with C++ for game developing. And making a musical band.
-Aaron
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top