1

I'm trying to insert a variable into mysql table, but it does not do what i want (no error, it simply does not work)

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

Anyone can help me how to insert $string into table tablename? I tried replacing ???? with ($string), (".$string."), $string, but all of them does not insert the $string variable into the database.

Thanks in advance!

4

2 回答 2

2

You can use \" (an escaped quotation mark) to include quotes inside your query, like so:

$string = 'this is an example';
mysql_query("insert into tablename (columnname) values (\"$string\")");
于 2012-05-24T18:29:55.877 回答
1

您忘记了引号 ( '),您应该使用双引号作为字符串分隔符。

$string = "test abc";
mysql_query("insert into t (col) values ('$string')");
于 2012-05-24T18:16:34.487 回答