2012-03-06 98 views
0

新手在這裏。試圖綁定值以消除sql注入。我有下面的代碼,但我得到這個錯誤... 調用1個綁定變量時在my.cgi線需要47 803 和輸出的樣子..綁定值INSERT INTO mysql perl

$new_row='53616c7465645f5fd8b88f6a16704f8ebc0a2002dfg45633617bbb0446fa', 'test12', 'user', '2012-03-06', 'xcvb', 'xb', 'xcvbb', 'xcvbb', 'UT', 'US', '4566', '4564564566', '[email protected]', 'vbn', '', '200', 'Monthly', 'eBook', 'WebStore', '9.95', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 'http://my.com', 'my.com', '', '', '', '', '', '', '', '', '2012-03-06', '30-Day-Trial' 
$questionmarks=?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? 

我試着它帶/不帶引號和逗號。任何想法讚賞。

foreach my $field (@account_field_order) { 
$new_row .= "'" . param($field) . "', "; 
$questionmarks .="?, "; 
}#foreach 
$new_row .= "'$status'"; 
$questionmarks .= "? "; 
my $dsn = "DBI:mysql:$database"; 
my $dbh = DBI->connect($dsn, $MYSQLuserid, $MYSQLpassword) 
      or die $DBI::errstr; 
my $sth = $dbh->prepare(qq(INSERT INTO $table VALUES ($questionmarks))) 
or die $DBI::errstr; 
$sth->execute(qq($new_row)) or die $DBI::errstr; 

回答

0

首先,讓我們將第一語句:

@new_row=('53616c7465645f5fd8b88f6a16704f8ebc0a2002dfg45633617bbb0446fa', 'test12', 'user', '2012-03-06', 'xcvb', 'xb', 'xcvbb', 'xcvbb', 'UT', 'US', '4566', '4564564566', '[email protected]', 'vbn', '', '200', 'Monthly', 'eBook', 'WebStore', '9.95', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 'http://my.com', 'my.com', '', '', '', '', '', '', '', '', '2012-03-06', '30-Day-Trial'); 
$questionmarks="?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?"; 

這將創造價值的一個數組,並與所有的S一個字符串?

然後在執行語句:

$sth->execute(@new_row) or die $DBI::errstr; 

,將你的價值觀的數組中傳遞到執行線路,而不是單一的參數像你這樣做之前。

+0

謝謝。這是有道理的,但當我修改它,我得到這個錯誤...調用2綁定變量時,需要在account.cgi行752. 0 – 2012-03-06 19:32:42

+0

'foreach my $ field(@account_field_order){' \t'$ new_row。= 「'」。參數($ field)。 「」, 「;' \t'$ questionmarks =」?「;' \t'}#foreach' '@ NEW_ROW = $ NEW_ROW;'' 推(@new_row, 「 '$狀態'」); ' '$ questionmarks。=「?」;' 'my $ sth = $ dbh-> prepare(qq(INSERT INTO $ table VALUES「$ questionmarks」))or die $ DBI :: errstr;' '$ sth - >執行(@new_row)或死亡$ DBI :: errstr;' – 2012-03-06 19:38:25

+0

好的。想象出來......'foreach my $ field(@account_field_order){0}推薦(@new_row,「'param($ field)',」); \t $ questionmarks。=「?,」;; \t} #foreach push(@new_row,「'$ status'」); $ questionmarks。=「?」; my $ dsn =「DBI:mysql:$ database」; my $ dbh = DBI-> connect($ dsn,$ MYSQLuserid,$ MYSQLpassword) 或die $ DBI :: errstr; ($ INSERT INTO $表VALUES($ questionmarks)))或死$ DBI :: errstr; $ sth-> execute(@new_row)或者死於$ DBI :: errstr;' – 2012-03-06 22:25:10

2

你應該提供一個列表的參數,每個問號一個,而不是一個包含參數字符串的標量參數。當我以前answered your question,我告訴你做的事:

my @values = map param($_), @account_field_order; # add values to array 
push @values, $status;     # for simplicity 
$new_row = join ", ", ("?") x @values; # add ? for each value 

... # basically same code as before, except the execute statement: 

$sth->execute(@values);  # arguments given will be inserted at placeholders 

哪裏$new_row是你的佔位符字符串,而不是你的參數列表。 不:

$new_row .= "'" . param($field) . "', "; 
... 
$new_row .= "'$status'"; 
$sth->execute(qq($new_row)) or die $DBI::errstr; 

因爲$new_row算作一個參數,因爲它是一個標量。您需要一個數組或長度與問號數量相同的列表。

+0

我使用了你的代碼,並且出現了'值與列數不匹配'的錯誤,因爲它只執行了22個參數/值。該表有47個字段/列。但是,它給了我上面代碼似乎更接近的想法。 – 2012-03-06 19:49:51

+0

也許這是一個SQL錯誤。很難說,因爲你沒有給我太多的信息。 – TLP 2012-03-06 20:16:58