我需要插入带有选定子类别的多个商店,将商店数据存储在一个表中,将子类别存储在第二个子类别中。当我用“forach”循环执行'for'循环时,相同的子类别数据被插入n次(取决于有多少商店)。当我把foreach循环放在外面循环时,$ md5c唯一id只插入第一个。如何解决?
$input_count = count($_POST['npm']);
for ($i = 0; $i < $input_count; $i++) {
$a = uniqid(about);
$b = uniqid (about, true);
$c = uniqid (rand(), true);
$md5c = md5($c);
$n = mysql_real_escape_string($_POST['npm'][$i]);
$a = mysql_real_escape_string($_POST['apm'][$i]);
$c = mysql_real_escape_string($_POST['mipm'][$i]);
$wh = mysql_real_escape_string($_POST['rv'][$i]);
$t = mysql_real_escape_string($_POST['ktf'][$i]);
$mob = mysql_real_escape_string($_POST['ktm'][$i]);
$mail = mysql_real_escape_string($_POST['kea'][$i]);
$con = mysql_real_escape_string($_POST['ipko'][$i]);
$query2 = "INSERT INTO shops (id, n, a, c, wh, tel, mob, mail, con) VALUES ('$md5c', '$n','$a','$c','$wh','$t','$mob','$mail','$con')";
$rs2=mysql_query($query2) or die(mysql_error());
foreach ( $_POST['subcat'] as $key=>$value ) {
$values = mysql_real_escape_string($value);
$query3="INSERT INTO sc (subcat_id,shop_id) VALUES ($values,'$md5c')";
$rs3=mysql_query($query3) or die(mysql_error());
}
}
答案 0 :(得分:0)
您可以尝试将$值存储在新数组中
所以在两个循环之前你必须定义$ md5cArray = [],然后启动第一个循环,将每个值存储在数组中,如$ md5cArray [] = md5c;
在具有相同结构的第一个循环之外启动第二个循环,并且由于数组已经排序,您可以这样做:
在第二个循环中:
for ($i = 0; $i < sizeof($_POST['subcat']); $i++) {
$values = mysql_real_escape_string($value);
$query3="INSERT INTO sc (subcat_id,shop_id) VALUES ({$_POST['subcat'][inner][second-inner]}, {$md5c[$i]})";
$rs3 = mysql_query($query3) or die(mysql_error());
}
您必须将值中的第一个元素编辑为您拥有的正确结构。
你的最终代码应该是这样的
$input_count = count($_POST['npm']);
$md5cArray = [];
for ($i = 0; $i < $input_count; $i++) {
$a = uniqid(about);
$b = uniqid (about, true);
$c = uniqid (rand(), true);
$md5c = md5($c);
$md5cArray[] = md5($c); // new array
$n = mysql_real_escape_string($_POST['npm'][$i]);
$a = mysql_real_escape_string($_POST['apm'][$i]);
$c = mysql_real_escape_string($_POST['mipm'][$i]);
$wh = mysql_real_escape_string($_POST['rv'][$i]);
$t = mysql_real_escape_string($_POST['ktf'][$i]);
$mob = mysql_real_escape_string($_POST['ktm'][$i]);
$mail = mysql_real_escape_string($_POST['kea'][$i]);
$con = mysql_real_escape_string($_POST['ipko'][$i]);
$query2 = "INSERT INTO shops (id, n, a, c, wh, tel, mob, mail, con) VALUES ('$md5c', '$n','$a','$c','$wh','$t','$mob','$mail','$con')";
$rs2=mysql_query($query2) or die(mysql_error());
}
for ($i = 0; $i < sizeof($_POST['subcat']); $i++) {
$values = mysql_real_escape_string($value);
$query3="INSERT INTO sc (subcat_id, shop_id) VALUES ("{$_POST['subcat'][$i]}","{$md5c[$i]}")";
$rs3=mysql_query($query3) or die(mysql_error());}
答案 1 :(得分:0)
使用 foreach($ _POST [&#39; subcat&#39;] [$ i]作为$ key =&gt; $ value){
$input_count = count($_POST['npm']);
for ($i = 0; $i < $input_count; $i++) {
$a = uniqid(about);
$b = uniqid (about, true);
$c = uniqid (rand(), true);
$md5c = md5($c);
$n = mysql_real_escape_string($_POST['npm'][$i]);
$a = mysql_real_escape_string($_POST['apm'][$i]);
$c = mysql_real_escape_string($_POST['mipm'][$i]);
$wh = mysql_real_escape_string($_POST['rv'][$i]);
$t = mysql_real_escape_string($_POST['ktf'][$i]);
$mob = mysql_real_escape_string($_POST['ktm'][$i]);
$mail = mysql_real_escape_string($_POST['kea'][$i]);
$con = mysql_real_escape_string($_POST['ipko'][$i]);
$query2 = "INSERT INTO shops (id, n, a, c, wh, tel, mob, mail, con) VALUES ('$md5c', '$n','$a','$c','$wh','$t','$mob','$mail','$con')";
$rs2=mysql_query($query2) or die(mysql_error());
foreach ( $_POST['subcat'][$i] as $key => $value ) {
$values = mysql_real_escape_string($value);
$query3="INSERT INTO sc (subcat_id,shop_id) VALUES ($values,'$md5c')";
$rs3=mysql_query($query3) or die(mysql_error());
}
}