1

I have a page when user enters multiple lines in a 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

Problem is if the first row is a success. It only enters that first row into database. If the other rows don't meet previous if statements it prints out correct number of Errors.

I'm pretty sure i'm missing one big point to foreach loops but i cannot find it.

Help would be appreciated.

Edited code so it's whole.

Edit: The code itself works. Problem is it only adds first row of the textarea.

9
  • You have defined $systemName, right? It isn't defined anywhere in your "posted" code. Commented Jan 20, 2015 at 20:33
  • Yes everything is defined when user comes to the site Commented Jan 20, 2015 at 20:39
  • What do you mean by "It only enters that first row into database."? Are you wanting to do an INSERT? Commented Jan 20, 2015 at 20:42
  • 2
    I don't mean to be rude but using proper indents would really help to solve this issue faster. Commented Jan 20, 2015 at 20:44
  • @Fred-ii- Yes i'm wanting to do insert if the row data is not a duplicate. If it's a duplicate then just display error message. Commented Jan 20, 2015 at 20:49

1 Answer 1

1

I'm counting 13 open brackets "{" and 9 close brackets "}". If the problem is really the foreach loop thats not a good thing.

Thank you for fixing the bracket imbalance and also for formatting the code so that it is easier to read.

I don't have your test data set so I can't be sure, but in a quick test

mysqli_close($con);

prevented me from making additional queries against the connection. Once you hit this line I think all additional queries will fail. Is error reporting on? I got a screenful of errors on my subsequent queries.

Sign up to request clarification or add additional context in comments.

3 Comments

Problem aren't close brackets. What i posted is just a snippet of the code. The code itself works. Problem is just it only works for the first row in texarea
@ user1863470 Pinging you to see if my additions to the above answer solved your problem.
Oh god that was it. Thank you so much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.