2012-12-07 65 views
2

我的php代碼返回一些值,我試圖插入數據庫。插入前插入單引號

$string = '1,here,Skincare composition against free radicals'; //this is returned 

'(insert into import (S.No,Name,Title)) values ($string)' 

需要將該字符串轉換在適當的格式,以dB插入

'1','here','Skincare composition against free radicals' //This is expected 

破滅應該這樣做?但我不知道如何?

+0

這應該工作,因爲你的SQL是一個字符串... – christopher

+0

@christopher但你需要'''S,對吧? – irrelephant

+0

@irrelephant Ah duh。 Kuya的回答是好的 – christopher

回答

10

嘗試,

$string = "1,here,Skincare composition against free radicals"; 
$varList = explode(",", $string); 
$newQuery = "INSERT INTO `import` (`S.No`,`Name`,`Title`) VALUES ('" . $varList[0] . "','" . $varList[1] . "','" . $varList[2] . "')"; 

但查詢是SQL Injection脆弱的,請閱讀下面的文章,如何借鑑它來保護,

其他來源

+0

甚至更​​好,使用準備語句來防止SQL注入 – Raptor

+1

@ShivanRaptor是的,這就是爲什麼我添加了如何使用預處理語句':D'保護鏈接 –