2012-05-24 133 views
1

我試圖插入一個變量到MySQL表,但它不會做我想做(沒有錯誤,它只是不工作)插入變量到MySQL表不工作

$string = 'this is an example'; 
mysql_query("insert into tablename (columnname) values ????"); 

誰都可以幫我如何將$string插入到表格名稱中?我嘗試用($string)(".$string."),$string替換????,但是它們都沒有將$string變量插入到數據庫中。

在此先感謝!

+0

您可能沒有提交事務此外,請用準備好的語句,而不是字符串插值 – bernie

+0

嗨,Bernie,你能詳細解釋一下嗎? –

+0

'insert'語句需要提交,或者可以使用「自動提交」設置,但我更喜歡手動提交。這裏:http://php.net/manual/en/mysqli.prepare.php – bernie

回答

2

您可以使用\」(轉義引號)包括查詢引號內,就像這樣:

$string = 'this is an example'; 
mysql_query("insert into tablename (columnname) values (\"$string\")"); 
1

您忘記了報價('),您應該在您的字符串分隔符處使用雙引號。

$string = "test abc"; 
mysql_query("insert into t (col) values ('$string')"); 
+0

嗨Juergen,那麼可以用什麼來替代????在我的問題?它應該是值(「'。$ string。'」)? –

+0

我的意思是(「'。$ string。'」) –

+0

好的,問題已經解決。感謝您的幫助 –