浏览了所有论坛,发现类似的问题,如 MySQL INSERT INTO with PHP $variable .但这还没有完全回答我的问题。
我想为列使用变量,但我的 MySQL 插入语句出错
$columns = 'id, test';
$sql_store = "INSERT into test ('$columns') VALUES (NULL, 1)";
$sql = mysqli_query($db, $sql_store) or die(mysql_error());
您的 SQL 语法有误;检查与您的 MariaDB 服务器版本对应的手册,了解在第 1 行的 ''id, Storlek') VALUES (NULL, 1)' 附近使用的正确语法
感谢您的帮助!
最佳答案
Problem : Your
$columns
variable is string which is not true.
这样试试,
PHP
$columns_array = array('id','test');
$columns = implode(",",$columns_array);
$sql_store = "INSERT into test (".$columns.") VALUES (NULL, 1)";
$sql = mysqli_query($db, $sql_store) or die(mysql_error());
关于php INSERT 到变量名 MySQL 的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44861030/