在 INSERT INTO MySQL 查询中插入 PHP 变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8522063/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Insert PHP variable in INSERT INTO MySQL query
提问by cud_programmer
I am trying to insert a PHP variable into an "INSERT INTO" MySQL query. I have tried many many combinations with no luck.
我正在尝试将 PHP 变量插入到“INSERT INTO”MySQL 查询中。我尝试了很多组合,但都没有运气。
My code is below:
我的代码如下:
$sql = 'INSERT INTO `mse_names` (`id`, `name`, `day`) VALUES (NULL, ".$testing_for.", \'test_day\');';
mysql_query($sql);
I can confirm that $testing_for does contain a string - I've checked this by calling var_dump right before.
我可以确认 $testing_for 确实包含一个字符串 - 我之前已经通过调用 var_dump 进行了检查。
Any help is much appreciated, thanks in advance
非常感谢任何帮助,提前致谢
*edit: sorry, forgot to mention that this inputs .$testing_for. and test into the respect columns when I look in the table using phpmyadmin*
*编辑:抱歉,忘了提到这个输入 .$testing_for。并在我使用 phpmyadmin* 查看表格时测试尊重列
回答by Naftali aka Neal
Oy..
哎..
Fix your quotes...
修复您的报价...
$sql = 'INSERT INTO `mse_names` (`id`, `name`, `day`) VALUES (NULL, "'.$testing_for.'", "test_day");';
This was caught by the SO markdown editor ^_^
这是被 SO markdown 编辑器捕获的^_^
回答by ComFreek
You have to use double quotes if you want to directly insert variables in strings:
如果要直接在字符串中插入变量,则必须使用双引号:
$sql = "INSERT INTO `mse_names` (`id`, `name`, `day`) VALUES (NULL, '$testing_for', '$test_da');";
Or just use the .
operator:
或者只使用.
运算符:
$sql = 'INSERT INTO `mse_names` (`id`, `name`, `day`) VALUES (NULL, "'.$testing_for.'", "'.$test_da.'");';
回答by sudhakar
Here i insert two variable with current date
在这里我插入两个带有当前日期的变量
$var1 = "1";$var2 = "phpmyadmin";
$sql = 'INSERT INTO Table_name (`id`, `name`, `datetime`) VALUES ("'.$var1.'", "'.$var2.'", now());';
now() -> now function insert the current date and time
now() -> now 函数插入当前日期和时间
回答by Zefiryn
Your $sql is within single apostrophe so using quote to leave string is not working. Either change
您的 $sql 位于单个撇号内,因此使用引号离开字符串不起作用。要么改变
".$testing_for." to '.$testing_for.'
回答by SlavaNov
Why string concatenation?
为什么是字符串连接?
You can do it straightforward:
你可以直接做到:
$sql = "INSERT INTO `mse_names` (`id`, `name`, `day`) VALUES (NULL, '$testing_for', 'test_da');";
But if you want you can do also:
但如果你愿意,你也可以这样做:
$sql = "INSERT INTO `mse_names` (`id`, `name`, `day`) VALUES (NULL, '".$testing_for."', 'test_da');";
回答by Kristoffer Svanmark
Try this:
尝试这个:
$sql = "INSERT INTO mse_names (id, name, day) VALUES (null, '" . $testing_for . "', 'test_day')";
mysql_query($sql)
回答by w3b
try this
尝试这个
$sql = 'INSERT INTO `mse_names` (`id`, `name`, `day`) VALUES (NULL, "'.$name.'", "day");';
回答by Sajidur Rahman
It simply works like that as well. I'v tested
它也只是这样工作。我测试过
$insertDetais = mysql_query("INSERT INTO Persons (userName, poNumber, mobileNumber, emailAdd, appraisal, newsletter, guessFinal) VALUES ('$userName', '$poNumber', '$mobileNumber', '$emailAdd', '$appraisal', '$newsletter', '$guessFinal')", $conMySQL);
$insertDetais = mysql_query("INSERT INTO Persons (userName, poNumber, mobileNumber, emailAdd, Assessment, newsletter, guessFinal) VALUES ('$userName', '$poNumber', '$mobileNumber', '$emailAdd', '$appraisal' , '$newsletter', '$guessFinal')", $conMySQL);