-3

I have the following PHP method:

public function addIndividual($conn, $hhid,
                                $marital_status,
                                $family_relation,
                                $ind_id,
                                $ind_id,
                                $head_of_hh,
                                $ind_first_name_ar,
                                $ind_last_name_ar,
                                $ind_first_name_en,
                                $ind_last_name_en,
                                $date_added,
                                $gender,
                                $dob,
                                $ind_status,
                                $arrayOfLegalProtection,
                                $user_id
                                )
{
    $res = $this->checkNumberOfIndividual($conn, $household_id);

    if($res=="enableAddInd")
    {
        try
        {

            $add = "INSERT INTO individual(hhid, 
                                            family_relation_id, 
                                            marital_status_id, 
                                            ind_id, ind_id, 
                                            head_of_hh, ind_first_name_ar, ind_last_name_ar,
                                            ind_first_name_en, ind_last_name_en, 
                                            dob, ind_status, 
                                            ind_date_added, user_id) 
                                            VALUES (:hhid, 
                                            :family_relation_id, 
                                            :marital_status_id, 
                                            :ind_id, :ind_id, 
                                            :head_of_hh, :ind_first_name_ar, :ind_last_name_ar,
                                            :ind_first_name_en, :ind_last_name_en, 
                                            :dob, :ind_status, 
                                            :ind_date_added, :user_id);
            $execAdd = $this->conn->prepare($add);
            $execAdd->bindValue('hhid', $hhid);
            $execAdd->bindValue('family_relation_id', $family_relation);
            $execAdd->bindValue('marital_status_id', $marital_status);
            $execAdd->bindValue('ind_id', $ind_id);
            $execAdd->bindValue('ind_id', $ind_id);
            $execAdd->bindValue('head_of_hh', $head_of_household);
            $execAdd->bindValue('ind_first_name_ar', $ind_first_name_ar);
            $execAdd->bindValue('ind_last_name_ar', $ind_last_name_ar);
            $execAdd->bindValue('ind_first_name_en', $ind_first_name_en);
            $execAdd->bindValue('ind_last_name_en', $ind_last_name_en);
            $execAdd->bindValue('dob', $dob);
            $execAdd->bindValue('ind_status', $ind_status);
            $execAdd->bindValue('user_id', $user_id);
            $execAdd->execute();

            $id = $conn->lastInsertId();
            $ind_action = 'HH First Time added';
            //Add into HH history
            $this->addHHHistory($conn, $unit_id, $id, $hh_id, $last_name_en,
            $last_name_ar, $un_id, $num_ind_of_hh,
            $gov_id_type, $gov_id_number, $phone_number, 
            $address_in_origin_country, $hh_status, $date_added,
            $hh_action,
            $user_id);
            $arr = json_decode($arrayOfLegalProtection, true);
            //Add Array of protection, situation and legals
            if(sizeof($arr)>0)
            {
                foreach($arr as $array)
                {
                    if($array['situationId']=='')
                    {
                        $situation_id=null;
                    }
                    else{
                        $situation_id = $array['situationId'];

                    }
                    if($array['legalId']==='')
                    {
                        $legal_id=null;
                    }
                    else{
                        $legal_id = $array['legalId'];
                    }
                    if($array['protectionId']==='')
                    {
                        $protection_id=null;
                    }
                    else{
                        $protection_id = $array['protectionId'];
                    }

                    if($array['willing_to_go_back']==='')
                    {
                        $willing_to_go_back='';
                    }
                    else{
                        $willing_to_go_back = $array['willing_to_go_back'];
                    }
                    $status = 'Active';

                    try
                    {
                        $sqlActualities = 'INSERT into household_protection_legal(
                        hhid,
                        unit_id, 
                        protection_id, 
                        legal_id, 
                        situation_id,
                        willing_to_go_back, unit_household_protection_status, unit_household_protection_date_added, user_id)
                                            VALUES(:hhid,
                        :unit_id, 
                        :protection_id, 
                        :legal_id, 
                        :situation_id,
                        :willing_to_go_back, 
                        :unit_hh_protection_status, 
                        :unit_hh_protection_date_added, 
                        :userId)';
                        $sqlActExec = $this->conn->prepare($sqlActualities);
                        $sqlActExec->bindValue('household_id', $id);
                        $sqlActExec->bindValue('unit_id', $unit_id);
                        $sqlActExec->bindValue('protection_id', $protection_id);
                        $sqlActExec->bindValue('legal_id', $legal_id);
                        $sqlActExec->bindValue('situation_id', $situation_id);
                        $sqlActExec->bindValue('willing_to_go_back', $willing_to_go_back);
                        $sqlActExec->bindValue('unit_household_protection_status', $status);
                        $sqlActExec->bindValue('unit_household_protection_date_added', $date_added);
                        $sqlActExec->bindValue('userId', $user_id);
                        $sqlActExec->execute();

                        //return $res;
                    }
                    catch(PDOException $e)
                    {
                        return $e->getMessage();
                    }

                    }

                }        
            }

        }
        catch(PDOException $e)
        {
            return $e->getMessage();
        }
        return $id;
    }
    else{
        return $res;
    }

}

I am getting the following error on load of the component:

Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in C:\wamp64\www\dev\api.php on line 1005

The line 1005 is:

if($array['situationId']=='')

I read about the problem in this documentation but I have never been able to solve my problem.

I tried to search whites spaces or even missing colons or commas, but I didn't find anything wrong.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
alim1990
  • 4,656
  • 12
  • 67
  • 130
  • There are a huge amount of problems with this code. Paste it into any editor with decent syntax highlighting - you've got undefined variables, unclosed strings, unmatched braces, and a duplicate parameter in the function definition. And those are just the obvious ones. – iainn Sep 24 '18 at 10:24
  • Why downvoting ? I only got missed a double quotes. Anyway, I am using vs code and it can't format php script. – alim1990 Sep 24 '18 at 10:32

1 Answers1

1

Error is clearly visible even in your SO question I am now answering. Thanks to syntax highlighting you can see there's too much lines highlighted as string, which usually is because of non-terminated strings. This type of error usually are reported imprecisely by lexer, as it cannot really tell for sure if you really wanted to put your code in string on purpose or not. If you did that on purpose then it's pretty valid, so it will complain on first line that no makes syntax error. I.e. when text no longer could be considered string content, as there would be " from other code line that, from lexer perspective terminates your "culprit"-string. But then the remaining part of that code starts to make no sense and that's why it complains pointing to that line instead of string declaration/variable assignemtn.

You need to close your SQL query string ($add) as it lacks closing ". Also get yourself a editor or IDE with working code highlighting.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141