将逗号分隔的字符串插入多个列

问题描述:

我正在寻找一种解决方案,我可以插入逗号分隔的字符串,但将它们拆分出来并插入同一张表的不同列中。将逗号分隔的字符串插入多个列

我知道这可以实现,因为我查看了整个网络,并看到其他可能已经成功完成了这一点。

我有将被插入将始终是相同的,因为它是复制形式的另一源和将总是保持在相同的格式简单的<textarea name="stenData" placeholder="Paste Sten Data..."></textarea>

数据。像这样:

RP1 = 8,RP2 = 3,RP3 = 1 ...等

这将被插入到部件表与samn列,以便rp1rp2

在我只是简单地插入数据,但我知道你可以在发布的数据中包装一个explode()函数?我找了一些建议确实比什么都重要......

UPDATE

我通过在POST输入爆炸:

$stenData = explode(',', $_POST['stenData']); 

这给了我(作为一个例子):

array(2) { [0]=> string(5) "RP1=8" [1]=> string(5) "RP2=7" } 

但林不知道如何插入到多列

**更新2 **

我试图将数据插入我的表,所以我从我的形式发布的数据,像这样:

// Dashboard Name 
$dashboardName = $_POST['dashboardName']; 

// Member Details 
$firstName = $_POST['firstName']; 
$lastName = $_POST['lastName']; 
$score_1 = $_POST['score_1']; 
$score_2 = $_POST['score_2']; 
$score_3 = $_POST['score_3']; 
$score_4 = $_POST['score_4']; 
$score_5 = $_POST['score_5']; 
$score_6 = $_POST['score_6']; 
$score_7 = $_POST['score_7']; 
$score_8 = $_POST['score_8']; 
$stenData = explode('=', $_POST['stenData']); // this is the problem line 

// Team name 
$teamName = $_POST['teamName']; 

然后我尝试插入,所有这一切是从一个stenData OK开:

$sql2 = "INSERT INTO members (firstName, lastName, score_1, score_2, score_3, score_4, score_5, score_6, score_7, score_8, dashboard_id, rp_1) 
     VALUES ('$firstName', '$lastName', '$score_1', '$score_2', '$score_3', '$score_4', '$score_5', '$score_6', '$score_7', '$score_8', '$dashboard_id', '$stenData')"; 

该错误消息是:Notice: Array to string conversion和它简单地输入单词“数组”到列行单元。

+1

所以要处理粘贴到''

+0

@EatPeanutButter这就是它,所以我试过它'$ stenData = explode(',',$ _POST ['stenData']] );'哪个输出数组,但我不知道我怎样才能将这些数组值插入到多列 - 我不知道你的意思是通过拆分元素? – danjbh

+0

在每个元素上使用相同的'explode()'函数,但不使用逗号,而使用'='符号。这会给你一个列名('rp1')和值('8')的数组。然后你可以将该值插入到相应的列中 – WillardSolutions

$_POST['stenData'] = "RP1=8,RP2=3,RP3=1"; 
preg_match_all("|RP([0-9]+)=([0-9]+)|", $_POST['stenData'], $matches); 
$tmpArr = array(); 
foreach($matches[1] as $key => $value) 
{ 
    $tmpArr['RP'.$value] = $matches[2][$key]; 
} 

结果:

array (size=3) 
    'RP1' => string '8' (length=1) 
    'RP2' => string '3' (length=1) 
    'RP3' => string '1' (length=1)