dream198731 2011-10-06 19:29
浏览 23
已采纳

使用implode插入多行

I am having a hard time getting my code to work. I grasp the basic concept but can't get it to work. I have a form with multiple rows:

<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>

I then have a script to process the form that I wish to insert multiple rows into my mysql database. My script looks like this:

<?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);
}
?>

I know there is something wrong because when i "print" the query i get this:

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) 

Can someone please look over my form and script and tell me what I am doing wrong and why it is not inserting the mutliple rows into my database using the implode method. Thank you.

  • 写回答

3条回答 默认 最新

  • dtr87341 2011-10-06 19:50
    关注

    Your $_POST superglobal actually looks like this:

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

    When you do foreach( $_POST as $row ) you are looping through each field instead of each row of fields as you expected. As such, you get mostly blanks (I'm guessing you submitted a blank form to test it) and then the 'S' from Submit.

    You can do it the way you want to, but you need to transpose your $_POST values first. This answer has a very simple transpose function. (He calls it "flipDiagonally", but it's a typical transpostion).

    Using the function from the linked answer, you could do like this:

    $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'].")";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应