使用多维数组从多个字段插入数据 [英] Insert data using multidimensional array from multiple fields
问题描述
我有一个实际上是表格的表格,表格(表格)是动态的,因此表格可以包含10到300行,
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,
表单表:
以下是输入表单表的代码:
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>\n
<td><input type='checkbox' checked='checked' name='scaffreq_id[]' value='$scaffreq_id' /></td>\n
<td><center>$scaffreq_id</center></td>\n
<td><center>$level m</center></td>\n
<td><center><input type='checkbox' checked='checked' name='insp1[]' value='y' /></center></td>\n
<td><center><input type='checkbox' checked='checked' name='insp2[]' value='y' /></center></td>\n
<td><center><input type='checkbox' checked='checked' name='insp3[]' value='y' /></center></td>\n
<td><center><input type='checkbox' checked='checked' name='insp4[]' value='y' /></center></td>\n
<td><center><input type='checkbox' checked='checked' name='insp5[]' value='y' /></center></td>\n
<td><center><input type='checkbox' checked='checked' name='insp6[]' value='y' /></center></td>\n
<td><center><input type='checkbox' checked='checked' name='insp7[]' value='y' /></center></td>\n
<td><center><input type='checkbox' checked='checked' name='insp8[]' value='y' /></center></td>\n
<td><center><input type='checkbox' checked='checked' name='insp9[]' value='y' /></center></td>\n
<td><center><input type='checkbox' checked='checked' name='insp10[]' value='y' /></center></td>\n
<td><center><input type='checkbox' checked='checked' name='insp11[]' value='y' /></center></td>\n
<td><center><input type='checkbox' checked='checked' name='insp12[]' value='y' /></center></td>\n
<td><center><p><input type='text' name='insp_reason[]' maxlength='255' size='45' value='$insp_reason'></p></center></td>\n
</tr>\n";
}
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表示类型为'='的输入类型复选框和ID代表其ID密钥。
提交时,我想将数据写入数据库。
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.
我尝试了一些循环选项,但是我需要从PHP运行循环而不是循环遍历我的查询(这花了太长时间),尤其是选择了300行。
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.
php代码的基本开销:
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);
但是,回显查询只给我('0',0','0 ','0','0','0','0','0'),('0'...,但作为选择的ID字段,确实有数据量(括号内)。 / p>
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.
推荐答案
更新
(哇, (这很困难):您将HTML表单中的所有输入字段都定义为数组。可以删除使它们成为数组的 []
,也可以使用 name = insp []
在您的 insp
复选框上。然后使用 var_dump($ _POST)
,然后您将了解如何解析该数组。
(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>\n
<td><input type='checkbox' checked='checked' name='items[{$scaffreq_id}][id]' /></td>\n
<td><center>{$scaffreq_id}</center></td>\n
<td><center>{$level} m</center></td>\n
<td><center><input type='checkbox' checked='checked' name='items[{$scaffreq_id}][insp][a]' /></center></td>\n
<td><center><input type='checkbox' checked='checked' name='items[{$scaffreq_id}][insp][b]' /></center></td>\n
<td><center><input type='checkbox' checked='checked' name='items[{$scaffreq_id}][insp][c]' /></center></td>\n
<td><center><input type='checkbox' checked='checked' name='items[{$scaffreq_id}][insp][d]' /></center></td>\n
<td><center><input type='checkbox' checked='checked' name='items[{$scaffreq_id}][insp][e]' /></center></td>\n
<td><center><input type='checkbox' checked='checked' name='items[{$scaffreq_id}][insp][f]' /></center></td>\n
<td><center><input type='checkbox' checked='checked' name='items[{$scaffreq_id}][insp][g]' /></center></td>\n
<td><center><input type='checkbox' checked='checked' name='items[{$scaffreq_id}][insp][h]' /></center></td>\n
<td><center><input type='checkbox' checked='checked' name='items[{$scaffreq_id}][insp][i]' /></center></td>\n
<td><center><input type='checkbox' checked='checked' name='items[{$scaffreq_id}][insp][j]' /></center></td>\n
<td><center><input type='checkbox' checked='checked' name='items[{$scaffreq_id}][insp][k]' /></center></td>\n
<td><center><input type='checkbox' checked='checked' name='items[{$scaffreq_id}][insp][l]' /></center></td>\n
<td><center><p><input type='text' name='items[{$scaffreq_id}][reason]' maxlength='255' size='45' value='Reason{$insp_reason}'></p></center></td>\n
</tr>\n";
}
帮助并不容易没有看到HTML表单代码。但是在我看来, $ ID
很有可能不是数组。因此,使用 foreach($ ID as ...)
不好。而是像这样构建您的字符串:
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})";
警告:
请注意,您的代码对SQL注入不安全且容易受伤害(请参阅此SO Q&A )。使用 PDO准备的语句代替 mysql( )
函数!
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!
这篇关于使用多维数组从多个字段插入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!