By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,250 Members | 1,270 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,250 IT Pros & Developers. It's quick & easy.

Combining a variable in MySQL query statement

P: 97
Hi,

I am getting an DBI error (DBD::mysql) from the following code. Can anybody help and tell me if anything wrong in it? Thanks!


Expand|Select|Wrap|Line Numbers
  1.    foreach my $f (@{$field_ref})
  2.     {
  3.         next if $f->{value} eq "";           
  4.         $stmt .= "," if $stmt;               
  5.         $stmt .= $f->{name} . " = " . $dbh->quote ($f->{value});
  6.     }
  7.  
  8.    $stmt = "INSERT INTO myTable SET $stmt";    
  9.     $dbh->do ($stmt);    
Mar 24 '08 #1
Share this Question
Share on Google+
4 Replies


eWish
Expert 100+
P: 971
Here is an example that works. Instead of inserting the data directly, I am using placeholders which will escape any special characters. SET is used when you do an update.

Expand|Select|Wrap|Line Numbers
  1. my $insert_statement = $dbh->prepare('INSERT INTO table_name(column1, column2) VALUES(?,?)');
  2.    $insert_statement->execute($value1, $value2);
  3.    $insert_statement->finish(); 
--Kevin
Mar 24 '08 #2

P: 97
Thanks, Kevin!

I need to get the field name and value from the form a user enteres.
I am being forced to use a variable to extract those unknown, until user select, column name and the value.
Mar 25 '08 #3

eWish
Expert 100+
P: 971
Do have another question? Also, did you try what I showed as an example? Did you get any errors?

--Kevin
Mar 26 '08 #4

P: 97
Do have another question? Also, did you try what I showed as an example? Did you get any errors?

--Kevin
Hi Kevin,

In case you wonder...
My server was down and I didn't get a chance to get back on it. I will let you know... Thanks!
Mar 26 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.