本文介绍了使用数组中的值for for循环for循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有下面的代码。我有一个表单设置,有10个输入字段设置,5列1和5列2。 try { $ count = count($ _ POST ['column1']); $ sql ='INSERT INTO tablename(column1,column2)VALUES'; $ b $($ i = 0; $ i { $ sql2 ='('。$ _ POST ['column1'] [$ i ]。','。$ _POST ['column2'] [$ i]。')'; if($ i $ sql2。=','; } echo$ sql2indside'< br>; } 在< br>之外回显$ sql2 $ sql3 =$ sql $ sql2; $ pdo-> exec($ sql3); } catch(PDOException $ e) { $ output ='错误:'。 $ E->的getMessage(); exit(); } 如果我输入1,2,3,4,5,6,7 ,8,9,10到我的10个输入字段,并提交我得到 (1,6),'indside'(2,7),'indside'(3,8),'indside'(4,9),'indside'(5,10)'indside' (5,10)'outside' 5插入到第1列和第10列插入到我的数据库的第2列。 我真正想要的是插入5个独立的行,所以1.6在第1,2,7行1,2等。但是当我在for循环中的时候,我不知道如何获取$ sql2的值。这可能吗?或者是他们的另一种方法,我可以采取这种做法?解决方案这看起来像一个简单的错误; 行 $ sql2 ='('。$ _ POST ['column1'] [$ i]。','。$ _POST ['column2'] [$ i]。')'; 正在重置$ sql2 - 当它应该附加到它时,也就是 $ sql2。='('。$ _ POST ['column1'] [$ i]。','。$ _POST ['column2'] [$一世] 。 ')'; 结果是: pre (1,6),(2,7),(3,8),(4,9),(5,10) code> INSERT INTO tablename(column1,column2) / code> 包含修复SQL注入的整个固定片段,如@Thrustmaster所示 try { $ count = count($ _ POST ['column1']); $ sql ='INSERT INTO tablename(column1,column2)VALUES'; $ sql2 =''; ($ i = 0; $ i { if($ sql2!='')$ sql2。=,; $ b $ sql2 ='('.mysql_real_escape_string($ _ POST ['column1'] [$ i])。','。mysql_real_escape_string($ _ POST ['column2'] [$ i ])。')'; echo$ sql2indside'< br>; } 在< br>之外回显$ sql2 $ sql3 =$ sql $ sql2; $ pdo-> exec($ sql3); } catch(PDOException $ e) { $ output ='错误:'。 $ E->的getMessage(); exit(); } I have the below code. I have a form setup that has 10 input fields set up, 5 for column1 and 5 for column2. try{ $count = count($_POST['column1']); $sql = 'INSERT INTO tablename (column1, column2) VALUES '; for ($i=0; $i< $count; $i++) { $sql2 = '(' .$_POST['column1'][$i] . ', ' . $_POST['column2'][$i] . ')'; if ($i <$count - 1) { $sql2 .= ','; } echo "$sql2 'indside'<br>"; } echo "$sql2 'outside'<br>"; $sql3 = "$sql $sql2"; $pdo->exec($sql3);}catch (PDOException $e){ $output = 'Error: ' . $e->getMessage(); exit();}If I input 1,2,3,4,5,6,7,8,9,10 into my 10 input fields and hit submit I get(1, 6), 'indside'(2, 7), 'indside'(3, 8), 'indside'(4, 9), 'indside'(5, 10) 'indside'(5, 10) 'outside'And 5 is inserted into column 1 and 10 is inserted into column 2 of my database.What I really want is 5 seperate rows inserted so 1,6 in row 1, 2,7 in row 2 etc.But I can't work out how to take the value of $sql2 when it is in the for loop. Is this possible? Or is their another approach I can take to acheive this? 解决方案 This looks like a simple bug; the line $sql2 = '(' .$_POST['column1'][$i] . ', ' . $_POST['column2'][$i] . ')';is resetting $sql2 - when it should be appending to it, i.e. $sql2 .= '(' .$_POST['column1'][$i] . ', ' . $_POST['column2'][$i] . ')';The result is:INSERT INTO tablename (column1, column2) VALUES (1, 6),(2, 7),(3, 8),(4, 9),(5, 10)entire fixed fragment including fixes SQL injection as noted by @Thrustmastertry{ $count = count($_POST['column1']); $sql = 'INSERT INTO tablename (column1, column2) VALUES '; $sql2 = ''; for ($i=0; $i< $count; $i++) { if ($sql2 != '') $sql2 .= ","; $sql2 = '(' .mysql_real_escape_string($_POST['column1'][$i]) . ', ' . mysql_real_escape_string($_POST['column2'][$i]) . ')'; echo "$sql2 'indside'<br>"; } echo "$sql2 'outside'<br>"; $sql3 = "$sql $sql2"; $pdo->exec($sql3);}catch (PDOException $e){ $output = 'Error: ' . $e->getMessage(); exit();} 这篇关于使用数组中的值for for循环for循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-27 03:11