[英]foreach array as key => value .. insert a value in each column name as array key all in one row
foreach ($mysql_field_array as $key => $value) {
$query = "INSERT INTO mytable ($key) VALUES ('$value')";
mysql_query($query);
}
mysql_field_array mysql_field_array
Array
(
[bransh] => test@fb.com
[course] => <test lead: dummy data for full_name>
[mobile] => <test lead: dummy data for phone_number>
)
This adds values successfully to each column but in multiple rows like this image . 这会成功地将值添加到每个列,但是像这个图像一样多行。
I want to add them all in one row. 我想将它们全部添加到一行中。
You'll need to build your query parts in the loop and then put it together after: 您需要在循环中构建查询部分,然后在以下情况下将它们组合在一起:
$keys = '';
$values = '';
foreach ($mysql_field_array as $key => $value) {
$keys = $key.',';
$values = "'".$value."',";
}
$keys = rtrim($keys, ',');
$values = rtrim($values, ',');
$query = "INSERT INTO mytable ($keys) VALUES ($values)";
And then run your query, preferably with the mysqli_
function rather than the deprecated mysql_
one: 然后运行您的查询,最好使用
mysqli_
函数而不是弃用的mysql_
:
mysqli_query ($link, $query);
where $link
is your mysqli connection. 其中
$link
是你的mysqli连接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.