0

I have table having the following structure:

  1. table_id (Primary key, not null)
  2. user_id
  3. foreign_key1
  4. foreign_key2

The fields 3 and 4 are foreign keys from other tables.

And sometimes, some of them it needs to be filled but there is no value for the other one.

I tried to add some data using PHP, and I always get the following:

Illegal string offset

I don't know if the reason is that a foreign key cannot be null, or I am trying to insert a string inside an INT foreign key.

var_dump gives me:

"SQLSTATE[HY000]: General error: 1366 Incorrect integer value: '' for column 'foreign_key2' at row 1"

I search and I found that we can insert null values for foreign key

Scripts:

As the angular 6 script, on button click:

saveUnitData(){
    let unit_type = '';
    let location_type_id = '';
    let number_of_hh = '';
    let unit_status = '';

    let arrayOfActualities;

    let add_date = '';
    let user_id = localStorage.getItem('user_id');
    unit_type = this.formGroup.controls['unit_type'].value;
    location_id = this.formGroup.controls['location_type_id'].value;
    number_h = this.formGroup.controls['number_of_hh'].value;
    unit_status = this.formGroup.controls['unit_status'].value;
    add_date = this.formGroup.controls['add_date'].value;

    arrayOfActualities = this.arrayOfLegalProtection;
    //console.log(arrayOfActualities.length);
    console.log(add_date);
    //Sending data
    this.auth.saveUnitData(
      unit_type, 
      location_type_id,
      user_id, 
      number_of_hh, 
      unit_status, 
      add_date, 
      arrayOfActualities)
      .subscribe(

      )
  }

arrayOfLegalProtection array push method:

add_protection_legal(){
    let foreign_key1= '';
    let foreign_key2= '';
    let foreign_key3= '';
    let add_date = '';
    foreign_key1= this.formGroup.controls['foreign_key1'].value;

    foreign_key2= this.formGroup.controls['foreign_key2'].value;
    add_date = this.formGroup.controls['add_date'].value;
    this.showSubHeader++;
    this.arrayOfLegalProtection.push({foreign_key1: foreign_key1, 
      foreign_key2: foreign_key2,
      foreign_key3: foreign_key3, 
      addDate: add_date})
    console.log(this.arrayOfLegalProtection);
  }

PHP Script:

if(sizeof($arrayOfActualities)>0)
            {
                foreach($arrayOfActualities as $array)
                {
                    if($array['situationId']!='')
                    {
                        $situation_id='';
                    }
                    else{
                        $situation_id = $array['situationId'];
                    }
                    if($array['foreign_key1']!='')
                    {
                        $foreign_key1='';
                    }
                    else{
                        $foreign_key1= $array['foreign_key1'];
                    }
                    if($array['foreign_key2']!='')
                    {
                        $foreign_key2='';
                    }
                    else{
                        $foreign_key2= $array['foreign_key2'];
                    }

                    $status = "Active";

                    try{
                        $sqlActualities = "INSERT into unit_situation_legal_protection( 
                        unit_situation_legal_protection_status, 
                        unit_situation_legal_protection_date_added, 
                        foreign_key2, foreign_key1, protection_id, user_id)
                                           VALUES(:actStatus, :dateAdded, :legal,
                                                  :situation, :protection, :userId)";
                        $sqlActExec = $this->conn->prepare($sqlActualities);
                        $sqlActExec->bindValue('actStatus', $status);
                        $sqlActExec->bindValue('dateAdded', $dateAdded);
                        $sqlActExec->bindValue('legal', $foreign_key1);
                        $sqlActExec->bindValue('situation', $foreign_key2);
                        $sqlActExec->bindValue('protection', $protection_id);
                        $sqlActExec->bindValue('userId', $user_id);
                        $sqlActExec->execute();

                        return "success";
                    }
                    catch(PDOException $e)
                    {
                        return $e->getMessage();
                    }
                }        
            }

Please note that, the foreign key constraint is set to accept null values of foreign_key1 and foreign_key2.

Ahmed Numaan
  • 1,034
  • 1
  • 10
  • 26
alim1990
  • 4,656
  • 12
  • 67
  • 130

0 Answers0