douyi0902 2015-07-20 14:21
浏览 117

使用多个字段中的多维数组插入数据

I have a form which is actually a table, the table (form) is dynamic, thus the table can consist of anything from 10 to 300 rows,

The form table:

enter image description here

Following is the code for the input form table:

include("x.xxx");
$cxn = mysqli_connect($host,$user,$password,$dbname);
$query = "SELECT * FROM `inspjc` ORDER BY `scaffreq_id`";
$result = mysqli_query($cxn,$query)
    or die ("Couldn't execute query.");     

echo "<table align='center'><br>
<tr bgcolor='#8DB4E3'>
 <th>X</th>
 <th>Scaffold Req No</th>
 <th>Elevation</th>
 <th class='vertical'>Foundations</th>
 <th class='vertical'>Ledgers</th>
 <th class='vertical'>Face Brace</th>
 <th class='vertical'>Plan Brace</th>
 <th class='vertical'>Platforms</th>
 <th class='vertical'>Mobiles</th>
 <th class='vertical'>Uprights</th>
 <th class='vertical'>Transoms</th>
 <th class='vertical'>Transverse Braces</th>
 <th class='vertical'>Ties</th>
 <th class='vertical'>Safe Access</th>
 <th class='vertical'>Signs</th>
 <th>If Not Inspected<br>Supply a Reason</th>
</tr>";
while($row = mysqli_fetch_assoc($result))
{   
    extract($row);

    echo "<tr>

    <td><input type='checkbox' checked='checked' name='scaffreq_id[]' value='$scaffreq_id' /></td>

        <td><center>$scaffreq_id</center></td>

        <td><center>$level m</center></td>

        <td><center><input type='checkbox' checked='checked' name='insp1[]' value='y' /></center></td>
            
        <td><center><input type='checkbox' checked='checked' name='insp2[]' value='y' /></center></td>

        <td><center><input type='checkbox' checked='checked' name='insp3[]' value='y' /></center></td>

        <td><center><input type='checkbox' checked='checked' name='insp4[]' value='y' /></center></td>

        <td><center><input type='checkbox' checked='checked' name='insp5[]' value='y' /></center></td>

        <td><center><input type='checkbox' checked='checked' name='insp6[]' value='y' /></center></td>

        <td><center><input type='checkbox' checked='checked' name='insp7[]' value='y' /></center></td>

        <td><center><input type='checkbox' checked='checked' name='insp8[]' value='y' /></center></td>

        <td><center><input type='checkbox' checked='checked' name='insp9[]' value='y' /></center></td>

        <td><center><input type='checkbox' checked='checked' name='insp10[]' value='y' /></center></td>

        <td><center><input type='checkbox' checked='checked' name='insp11[]' value='y' /></center></td>

        <td><center><input type='checkbox' checked='checked' name='insp12[]' value='y' /></center></td>

        <td><center><p><input type='text' name='insp_reason[]' maxlength='255' size='45' value='$insp_reason'></p></center></td>

        </tr>
";
}
echo "</table><br>";
?>

The desired output looks like this (simplified):

x   ID    a    b    c    d    e    f    g
---------------------------------------------------
1   365   1    1    0    0    0    1    North Bay
1   211   1    0    1    1    1    1    South Bay
0   237   0    1    1    1    1    0    Boiler

x represents an input type of type='checkbox' and ID represents its ID key. On submit, I would like to write the data to a database.

I have tried a few loop option, but I need the loop to run from php and not loop through my query (this takes too long), especially with 300 rows selected.

The basic outlay of the php code:

$ID = $_POST[ID];
$a = $_POST[a];
$b = $_POST[b];
$c = $_POST[c];
$d = $_POST[d];
$e = $_POST[e];
$f = $_POST[f];
$g = $_POST[g];

$userData = array();

foreach ($ID as $newid)
    {
    $userData[] = "('" . $newid['ID'] . "', 
        '" . $newid['a'] . "', 
        '" . $newid['b'] . "', 
        '" . $newid['c'] . "', 
        '" . $newid['d'] . "', 
        '" . $newid['e'] . "', 
        '" . $newid['f'] . "', 
        '" . $newid['g'] . "')";
    }

$query = "INSERT INTO `inspect` (`ID`,`a`,`b`,`c`,`d`,`e`,`f`,`g`) VALUES ";
$query .= implode(',',$userData);

But, echo'ing the query only gives me "('0',0','0','0','0','0','0','0'),('0'..." yet there is exactly the quantity of data (within brackets) as the selected ID fields.

  • 写回答

1条回答 默认 最新

  • drt41563 2015-07-22 09:13
    关注

    UPDATE

    (Wow, that's a tough one): You're defining all your input fields in your HTML form as arrays. You can either remove those [] which makes them arrays or you can use name=insp[] on your insp-checkboxes. Then output your submission with var_dump($_POST) and you see how you can parse the array.

    Or create a nested array. Something like this will help:

    while($row = mysqli_fetch_assoc($result)) {
    
        extract($row);
    
        echo "<tr>
    
        <td><input type='checkbox' checked='checked' name='items[{$scaffreq_id}][id]' /></td>
    
            <td><center>{$scaffreq_id}</center></td>
    
            <td><center>{$level} m</center></td>
    
            <td><center><input type='checkbox' checked='checked' name='items[{$scaffreq_id}][insp][a]' /></center></td>
                
            <td><center><input type='checkbox' checked='checked' name='items[{$scaffreq_id}][insp][b]' /></center></td>
    
            <td><center><input type='checkbox' checked='checked' name='items[{$scaffreq_id}][insp][c]' /></center></td>
    
            <td><center><input type='checkbox' checked='checked' name='items[{$scaffreq_id}][insp][d]' /></center></td>
    
            <td><center><input type='checkbox' checked='checked' name='items[{$scaffreq_id}][insp][e]' /></center></td>
    
            <td><center><input type='checkbox' checked='checked' name='items[{$scaffreq_id}][insp][f]' /></center></td>
    
            <td><center><input type='checkbox' checked='checked' name='items[{$scaffreq_id}][insp][g]' /></center></td>
    
            <td><center><input type='checkbox' checked='checked' name='items[{$scaffreq_id}][insp][h]' /></center></td>
    
            <td><center><input type='checkbox' checked='checked' name='items[{$scaffreq_id}][insp][i]' /></center></td>
    
            <td><center><input type='checkbox' checked='checked' name='items[{$scaffreq_id}][insp][j]' /></center></td>
    
            <td><center><input type='checkbox' checked='checked' name='items[{$scaffreq_id}][insp][k]' /></center></td>
    
            <td><center><input type='checkbox' checked='checked' name='items[{$scaffreq_id}][insp][l]' /></center></td>
    
            <td><center><p><input type='text' name='items[{$scaffreq_id}][reason]' maxlength='255' size='45' value='Reason{$insp_reason}'></p></center></td>
    
            </tr>
    ";
    }
    

    It's not easy to help without seeing the HTML form code. But as it seems to me, $ID ist most likely not an array. So using foreach($ID as ...) is not good. Instead, build your string like this:

    $query = "INSERT INTO `inspect` (`ID`,`a`,`b`,`c`,`d`,`e`,`f`,`g`) VALUES ";
    $query .= "({$ID}, {$a}, {$b}, {$c}, {$d}, {$e}, {$f}, {$g})";
    

    Warning:
    Be aware that your code is insecure and vulnurable to SQL injections (see this SO Q&A). Use PDO prepared statements instead of mysql() functions!

    评论

报告相同问题?

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算