Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/249.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何将数组值动态添加到字符串中?_Php_Arrays - Fatal编程技术网

Php 如何将数组值动态添加到字符串中?

Php 如何将数组值动态添加到字符串中?,php,arrays,Php,Arrays,我有一个具有db列名称的数组 $db_cols = ['name', 'age', 'gender']; 我需要从数组$db\u cols动态地将POST值添加到db查询中。结果一定是这样的 $db->query('INSERT INTO tbl (id, '.implode(', ', $db_cols).', order) VALUES (?, '.str_repeat('?, ', count($db_cols)).' ?)', [$_POST['id'],$_POST['name

我有一个具有db列名称的数组

$db_cols = ['name', 'age', 'gender'];
我需要从数组
$db\u cols
动态地将POST值添加到db查询中。结果一定是这样的

$db->query('INSERT INTO tbl (id, '.implode(', ', $db_cols).', order) VALUES (?, '.str_repeat('?, ', count($db_cols)).' ?)', [$_POST['id'],$_POST['name'], $_POST['age'], $_POST['gender'], $order]);
但是我需要修改上面代码的这部分
$\u POST['name']、$\u POST['age']、$\u POST['gender']

查询结果:

INSERT INTO tbl (id, name, age, gender, order) VALUES (?, ?, ?, ?, ?),[1, 'Joe', '35', 'male', 1]

谢谢你的帮助。

我的帖子帮了你吗?我的帖子帮了你吗?