如何使用php将数组键作为表列和值作为列值插入mysql? [英] How to insert array key as table column and value as column value into mysql using php?

查看:111
本文介绍了如何使用php将数组键作为表列和值作为列值插入mysql?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的数组:

I have an array that looks like:

$frmData = array([name]=>Abhi[cell]=>050000000[email]=>abc@xyz.com[address]=>1/2 South Road)

现在,我想从数组中将数据插入到我的mysql数据库中,并使用数组键[name][cell][email][address]作为表列,并将值用作列值.

Now I want to insert data into my mysql db from the array, and use the array key [name][cell][email][address] as table column and values as column value.

我以任何方式尝试了几次,但不确定如何去做.任何帮助将不胜感激.

I tried several times any ways but not sure how to do it. Any help is greatly appreciated.

推荐答案

您可以尝试以下方法:

$columns = array_keys($frmData);
$values = array_values($frmData);

$query = "INSERT INTO your_table (" . implode(", ", $columns) . ") VALUES ('" . implode("', '", $values) . "')";

或者,如果要对值进行计算,可以使用foreach ($frmData as $column => $value)

OR if you want to make make calculation on values you can loop through your array with foreach ($frmData as $column => $value)

这篇关于如何使用php将数组键作为表列和值作为列值插入mysql?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆