使用implode插入多行

时间:2011-10-06 19:29:24

标签: php insert rows implode

我很难让我的代码工作。我掌握了基本概念但无法使其发挥作用。我有一个包含多行的表单:

<form action="multiscript.php" method="post" id="form">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><input type="text" name="field_PK[]" id="field_PK" /></td>
<td><input type="text" name="sql_fk[]" id="sql_fk" /></td>
<td><input type="text" name="account_fk[]" id="account_fk" /></td>
<td><input type="text" name="field_code[]" id="field_code" /></td>
<td><input type="text" name="field_name[]" id="field_name" /></td>
<td><input type="text" name="field_px[]" id="field_px" /></td>
<td><input type="text" name="field_order[]" id="field_order" /></td>
</tr>
<tr>
<td><input type="text" name="field_PK[]" id="field_PK" /></td>
<td><input type="text" name="sql_fk[]" id="sql_fk" /></td>
<td><input type="text" name="account_fk[]" id="account_fk" /></td>
<td><input type="text" name="field_code[]" id="field_code" /></td>
<td><input type="text" name="field_name[]" id="field_name" /></td>
<td><input type="text" name="field_px[]" id="field_px" /></td>
<td><input type="text" name="field_order[]" id="field_order" /></td>
</tr>
</table>
<input type="submit" name="button" id="button" value="Submit" />
</form>

然后我有一个脚本来处理我想在我的mysql数据库中插入多行的表单。我的脚本看起来像这样:

<?php 
$values = array();?>
<?php
foreach( $_POST as $row ) { 
$values[] =  "(" . $row['field_PK'] . "," . $row['sql_fk'] . "," . $row['account_fk']. "," . $row ['field_code']. "," . $row['field_name']. "," . $row['field_px']. "," . $row['field_order'].")";
}
if( !empty($values) ) {
$query = "INSERT INTO `Table_fields` (field_PK, sql_fk, account_fk, field_code, field_name, field_px, field_order) VALUES ". implode(',',$values);

mysql_query($query);
}
?>

我知道有什么不对,因为当我“打印”查询时,我得到了这个:

INSERT INTO `Table_fields` (field_PK, sql_fk, account_fk, field_code, field_name, field_px, field_order) VALUES (,,,,,,),(,,,,,,),(,,,,,,),(,,,,,,),(,,,,,,),(,,,,,,),(,,,,,,),(S,S,S,S,S,S,S) 

有人可以查看我的表单和脚本并告诉我我做错了什么以及为什么它没有使用implode方法将多行插入我的数据库。谢谢。

3 个答案:

答案 0 :(得分:1)

您的$_POST超级全球实际上看起来像这样:

Array
(
    [field_PK] => Array
        (
            [0] => value1
            [1] => value2
        )
    [sql_fk] => ...
    [account_fk] => ...
    [field_code] => ...
    [field_name] => ...
    [field_px] => ...
    [field_order] => ...
    [button] => Submit
)

执行foreach( $_POST as $row )时,您正在循环遍历每个字段,而不是按预期方式遍历每行字段。因此,你得到大多数空白(我猜你提交了一个空白表格进行测试),然后是提交的'S'。

您可以按照自己的方式进行操作,但需要首先转置$_POST值。 This answer has a very simple transpose function.(他称之为“flipDiagonally”,但这是典型的转换)。

使用链接答案中的功能,您可以这样做:

$transposed = transpose( $_POST );

foreach( $transposed as $index => $row ) { 
    $values[] = "(" . $row['field_PK'] . "," . $row['sql_fk'] . "," . $row['account_fk']. "," . $row ['field_code']. "," . $row['field_name']. "," . $row['field_px']. "," . $row['field_order'].")";
}

答案 1 :(得分:0)

尝试

foreach($_POST as $index => $row)

反对

foreach($_POST as $values)

当然,不要忘记关于SQL注入攻击和类似的常见故事。

编辑:

看到所有字段都是文本类型,无论它们是否为空 - 它们都将被发送。它们也共享相同的索引,因此您可以以更优雅的方式键入它:

foreach($_POST['field_pk'] as $index => $values)
{
    $values[] =  "(" . $_POST[$index]['field_PK'] . "," . $_POST[$index]['sql_fk'] . "," . $_POST[$index]['account_fk']. "," . $_POST[$index]['field_code']. "," . $_POST[$index]['field_name']. "," . $_POST[$index]['field_px']. "," . $_POST[$index]['field_order'].")";

}

我必须承认,没有正确阅读导致我给出了错误的答案。应用相同的SQL注入防护规则,确保您的输入已过滤!

答案 2 :(得分:0)

您不希望迭代所有POST变量。相反,你想迭代$ _POST [“field_PK”]数组。