[英]Php mysql insert array key values for the same table id
I have an array like below. 我有一个像下面的数组。
Where i need to insert all the values of ActionKey
and ActionValue
corresponding to each index for the same AdId
我需要在其中插入与同一AdId
每个索引对应的所有ActionKey
和ActionValue
值
ie) 即)
0th index - sms - 213123 and 0索引-短信-213123和
1st index call - 12313 第一个索引调用-12313
I am trying something like, but not working out. 我正在尝试类似的方法,但没有解决。
$sDate = date("Y-m-d H:i:s");
$values = array();
$d_actionKey = $this->params()->fromPost('d_ActionKey');
$d_actionValue = $this->params()->fromPost('d_ActionValue');
$sql = "INSERT INTO `AdActions` (`CreatedDate`,`ActionKey`,`ActionValue`) VALUES";
foreach($values as $value) {
$values[] = "($sDate, $d_actionKey, $d_actionValue)";
}
My table data should look like. 我的表数据应该看起来像。
UPDATED CODE : 更新的代码 :
I need to get the AdId from another table's last inserted value. 我需要从另一个表的最后一个插入值中获取AdId。 Then take that last inserted AdId and insert into AdActions 然后将最后插入的AdId插入AdActions
$adActionsInsert = $this->getAdLibraryTable()->saveAdd($dataArray);
$query='INSERT INTO `AdActions` (`AdId`,`CreatedDate`,`ActionKey`,`ActionValue`) VALUES ';
for($i=0; $i < count($adActionsArray['ActionKey']); $i++)
{
if($i!=0)
$query .= ', ';
$query .= sprintf("(%d,'%s', '%s', '%d')",
$adActionsInsert,
$adActionsArray['CreatedDate'],
$adActionsArray['ActionKey'][$i],
$adActionsArray['ActionValue'][$i]);
}
I am able to get the last insert value like below (from the model file) 我能够从模型文件中获得如下所示的最后一个插入值
$this->tableGateway->lastInsertValue;
According to Inserting multiple rows in mysql , sets of data when inserting multiple rows at once should be separated by commas, like 根据在mysql中插入多行,一次插入多行时的数据集应以逗号分隔,例如
INSERT INTO table
(col1, col2)
VALUES
(data11, data12), -- first set of data, ended by comma
(data21, data22); -- second set of data
That means, add a comma at the end of $values[] = "($sDate, $d_actionKey, $d_actionValue)
and you can use substr
to delete the comma of the last statement as it is not needed. (and be aware of SQL injection) 这就是说,在$values[] = "($sDate, $d_actionKey, $d_actionValue)
的末尾添加逗号,然后可以使用substr
删除最后一条语句的逗号,因为它不需要。(并请注意SQL注入
Edit 编辑
dataxy would be $d_actionKey and $d_actionValue dataxy将是$ d_actionKey和$ d_actionValue
If you want to insert the $data array into the table AdActions. 如果要将$ data数组插入表AdActions中。 You can build your query like this. 您可以像这样构建查询。
$data = [
'CreatedDate' => '2018-12-12 08:04:32',
'ActionKey' => [
0=>'sms',
1=>'call'
],
'ActionValue' => [
0 => 213123,
1 => 12313
]
];
$query='INSERT INTO `AdActions` (`CreatedDate`,`ActionKey`,`ActionValue`) VALUES ';
for($i=0; $i < count($data['ActionKey']); $i++)
{
if($i!=0)
$query .= ', ';
$query .= sprintf("('%s', '%s', '%d')",
$data['CreatedDate'],
$data['ActionKey'][$i],
$data['ActionValue'][$i]);
}
echo $query;
This should give you a query like this 这应该给你这样的查询
INSERT INTO `AdActions` (`CreatedDate`,`ActionKey`,`ActionValue`) VALUES ('2018-12-12 08:04:32', 'sms', '213123'), ('2018-12-12 08:04:32', 'call', '12313')
I donot understand why you are using 我不明白你为什么使用
foreach($values as $value)
{
$values[] = "($sDate, $d_actionKey, $d_actionValue)";
}
section since you declared $values = array();
因为您声明了$values = array();
If your array is like 如果你的数组像
$arr = array(
'date' => '',
'ActionKeys' = > array(
[0] => 'sdsdsd',
[1] => 'fghfgh',
),
'ActionValues' = > array(
[0] => '345345',
[1] => '455496',
)
)
you can create values as
$ActionKeys = $arr[0]['ActionKeys'];
$ActionValues = $arr[0]['ActionValues'];
$date = $arr[0]['date']
$values = '';
for($i=0;$i<count();$i++){
if($i!=0)
$values.=','
else
$values.=' ($date, $ActionKeys[i], $ActionValues[i])';
}
then insert inside command add values as $values;
loop $arr if it is an array; $ arr如果是数组则循环;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.