本文介绍了如何将值数组插入到mysql表的不同列中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在像这样的数组中获取值:

I am getting values in an array like this:

 Array
(
    [0] => English
    [1] => Arabic
)

我在数据库中有2列,如下所示.

I am having 2 columns in database like below.

lanuage1 language2

我想将第一个数组[0] =>插入英语到language1列,插入[1] =>阿拉伯语到language2,有人可以建议我该怎么做吗?谢谢.

I want to insert the first array [0] => English to language1 column and [1] => Arabic to language2 ,can any one suggest how can I do that? Thanks.

我尝试过这样:

$data = explode( ',', $language ) ;

print_r($data);

INSERT INTO contact(lanuage1 ,language2) VALUES ($data [0], $data [1]);

但是它不起作用...

but its not working...

推荐答案

如果要正确格式化数组值,则可以使用implode().考虑以下示例:

If you want your array values to be properly formatted, you could just use implode(). Consider this example:

$values = array('English', 'Arabic');
$statement = 'INSERT INTO contact (`language1`, `language2`) VALUES ("' . implode('", "', $values) . '")';
echo $statement;

样本输出:

INSERT INTO contact(`language1`, `language2`) VALUES ("English", "Arabic")

这篇关于如何将值数组插入到mysql表的不同列中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 02:12