在检查重复项时,Php foreach循环只在mysql中添加第一行

时间:2015-01-20 20:31:37

标签: php mysql foreach

当用户在textarea中输入多行时,我有一个页面。

$systemName = "MySystem";
// scandata is a textarea element
if(isset($_POST['submit'])) {
////////////
$rows = preg_split('/\n/',$_POST['scandata']);
foreach($rows as $row){
    $data = preg_split('/\t/',$row);
    if(count($data) == 6){
        $sigID = $data[0];
        $sigGroup = $data[1];
        $sigType = $data[2];
        $sigName = $data[3];
        $sigScanStrenght = $data[4];
        if (preg_match('/\b[A-Z][A-Z][A-Z]-[0-9][0-9][0-9]\b/', $sigID)){
            if ($sigGroup == "22") { 
                 if ($sigType == "11") {
                    if ($sigName == "") {
                        $add_action[] = "<div class=\"alert alert-danger fade in alert_me\">$button_x_close <b>ERROR:</b> \t Sorry, but signature ID <u>$sigID</u> is not scanned down enough. Please scan it to higher percentage.</div>\n";
                    }else { 

                    //function VALIDATE
                        $validateSQL= "SELECT `id` FROM `signature` WHERE `sigName` = '$sigName' AND `systemName` = '$systemName' AND `sigID` = '$sigID' AND `already_run` = 'no'";

                        if ($validate_result=mysqli_query($con,$validateSQL))
                          {
                          // Return the number of rows in result set
                          $validate_rowcount=mysqli_num_rows($validate_result);
                          if ($validate_rowcount > 0){
                          $sql_to_add = false;
                          $add_action[] = "<div class=\"alert alert-warning fade in alert_me\">$button_x_close Sorry, but signature ID <u>$sigID</u> you entered is a duplicate</div>";}
                          else{
                          $sql_to_add = true;
                          $to_add_action = "<div class=\"alert alert-success fade in alert_me\">$button_x_close Succes signature ID <u>$sigID</u> has been added</div>";}

                    if($sql_to_add) {$sql = "INSERT INTO `signature` (`systemName`, `sigName`, `sigGroup`, `sigType`, `sigScanStrenght`, `reporter`, `sigID`)
                        VALUES ('$systemName', '$sigName', '$sigGroup', '$sigType', '$sigScanStrenght', '$reporter', '$sigID')";
                        if (!mysqli_query($con,$sql)) {   die('Error: ' . mysqli_error($con));      }
                        }
                        else {$sql = "";}

                        $add_action[] = $to_add_action;
                        // Free result set
                        mysqli_free_result($validate_result);
                        mysqli_close($con); 
                        }
                }//else sigName close

                }//if sigtype close
                else {$add_action[] = "<div class=\"alert alert-danger fade in alert_me\">$button_x_close <b>ERROR:</b> \t Sorry, but signature type <u><b>$sigType</b></u> is not added</div> \n";}

            }// if sigroup close
            else {$add_action[] = "<div class=\"alert alert-danger fade in alert_me\">$button_x_close <b>ERROR:</b> \t Sorry, but signature ID <u>$sigID</u> has invalid signature group (<i> $sigGroup </i>)</div> \n";}

        }//if pregmatch sigid close
        else {$add_action[] = "<div class=\"alert alert-danger fade in alert_me\">$button_x_close <b>ERROR:</b> \t Sorry, but you entered invalid signature ID (<i> $sigID </i>)</div> \n";}

    }//if count close 
    else {$add_action[] = "<div class=\"alert alert-danger fade in alert_me\">$button_x_close <b>ERROR:</b> \t Bad data input. Make sure you pasted the result right.</div> \n";}

}//foreach close
}//main function close

问题是第一行是否成功。它只将第一行输入数据库。如果其他行不符合先前的if语句,则会打印出正确的错误数。

我很确定我错过了foreach循环的一个重点,但我找不到它。

帮助将不胜感激。

编辑代码,所以它是完整的。

编辑:代码本身有效。问题是它只添加了textarea的第一行。

1 个答案:

答案 0 :(得分:1)

我计算13个开括号&#34; {&#34;和9个小括号&#34;}&#34;。如果问题确实是foreach循环,这不是一件好事。

感谢您修复括号不平衡以及格式化代码,以便更容易阅读。

我没有测试数据集,所以我无法确定,但是快速测试

mysqli_close($con);

阻止我对连接进行其他查询。一旦你点击这一行,我认为所有其他查询都将失败。是否有错误报告?我在后续查询中遇到了一些错误。