1

I am stuck. I have spent two days looking thru all the references I can find and I can’t figure out why this will not work! I get the error: "Creating default object from empty value." Bellow is my SQL statement and my parameters array.

    $sql_insert = "
    INSERT INTO vrm_vrd_submission_tbl (vrm_vrd_nmbr_id, vrm_vrd_sub_type_id, vrm_vrd_sub_date, vrm_vrd_min_form_date, vrm_vrd_sub_quantity, county_id, pers_emp_pre_id, election_general_info_id ,vrm_vrd_sub_submitter_name, vrm_vrd_compliance_rules_id)
    VALUES(:vrm_vrd_nmbr_id,
   :vrm_vrd_sub_type_id,
   :vrm_vrd_sub_date,
   :vrm_vrd_min_form_date,
   :vrm_vrd_sub_quantity,
   :county_id,
   :pers_emp_pre_id,
   :election_general_info_id,
   :vrm_vrd_sub_submitter_name,
   :vrm_vrd_compliance_rules_id)
   ";

    $sql_parms=array(":vrm_vrd_nmbr_id"=>$vrm_vrd_nmbr_id, ":vrm_vrd_sub_type_id 
    "=>$data['vrm_vrd_sub_type_id'], 
    ":vrm_vrd_sub_date"=>trim($data['vrm_vrd_sub_date']), 
    ":vrm_vrd_min_form_date"=>trim($data['vrm_vrd_min_form_date']), 
    ":vrm_vrd_sub_quantity"=>trim($data['vrm_vrd_sub_quantity']), ":county_id 
    "=>$data['county_id'],":pers_emp_pre_id "=>$data['pers_emp_pre_id'], 
    ":election_general_info_id"=>$election_general_info_id, 
    ":vrm_vrd_sub_submitter_name"=>$vrm_vrd_sub_submitter_name, 
    ":vrm_vrd_compliance_rules_id"=> $vrm_vrd_compliance_rules_id);

    $ret_val=$db->db_bound_query($sql_insert, $sql_parms);

Method being called in my database class:

    public function db_bound_query($qry_str, $parms_array){
            $log = new error_log_class;
            $db_conn = self::_connect();

            if(!$exec_str= $db_conn->prepare($qry_str)){    
            $log->save_to_log($qry_str,__LINE__,__FILE__,"Failed to perpare.");     
            }

            $val="";
            foreach($parms_array as $parm ->$val){
               $exec_str->bindParam($parm,$val);
            }       

            $res=$exec_str->execute();

            $results= $exec_str->fetchAll(PDO::FETCH_ASSOC);    
      }

EDIT: I changed this method to the following as suggensted by @iamsleepy and @MrCode. But I am getting the error I was originally chasing which is "Invalid Parameter number".

    public function db_bound_query($qry_str, $parms_array){
            $log = new error_log_class;
            $db_conn = self::_connect();

            if(!$exec_str= $db_conn->prepare($qry_str)){    
                $log->save_to_log($qry_str,__LINE__,__FILE__,"Failed to perpare.");     
            }

            $res=$exec_str->execute($parms_array );

            $results= $exec_str->fetchAll(PDO::FETCH_ASSOC);    

            return $results;
    }
Rick Savoy
  • 321
  • 4
  • 11
  • See also [How to squeeze an error messge out of PDO](http://stackoverflow.com/questions/3726505/how-to-squeeze-error-message-out-of-pdo) to learn how to get PDO to always throw exceptions, from which you can find out why your `prepare()/execute()` fails. – Michael Berkowski Apr 16 '14 at 19:37
  • 1
    And, you don't need the `foreach` loop to bind when you already have the `$sql_params` like you have. Just add it to `execute($parms_array)`. – iamsleepy Apr 16 '14 at 19:38
  • Makes sense Michael, but I am testing for that with 'if(!$exec_str= $db_conn->prepare($qry_str)){' It is not returning false. – Rick Savoy Apr 16 '14 at 19:41
  • Thanks iamsleepy, I tried that and I'm getting "Invadid parameter number: Parameter was not defined." error. – Rick Savoy Apr 16 '14 at 19:45
  • @RickSavoy If it fails, you log the error, but you don't return from the `db_bound_query()` -- execution continues with the faulty object – Michael Berkowski Apr 16 '14 at 19:46
  • 3
    I think the `$parm ->$val` is intepreted as calling property `$val` from object `$parm`. Try changing it to `$parm => $val`. – iamsleepy Apr 16 '14 at 19:48
  • @iamsleepy Ah, good find. That should be `=>` and indeed _that_ is the source of the `default object from empty value` error, not the `prepare()` call as I suspected. – Michael Berkowski Apr 16 '14 at 19:49
  • 2
    That whole foreach can be removed and just pass `$parms_array` to `execute($parms_array)`. – MrCode Apr 16 '14 at 19:49
  • @MichaelBerkowski I changed this method as suggensted by iamsleepy and MrCode. But I am getting the error I was originally chasing which is "Invalid Parameter number". Should I open a new question for that error? – Rick Savoy Apr 16 '14 at 20:21
  • Are you _still_ getting an _Invalid Parameter number_ error? – Michael Berkowski Apr 16 '14 at 20:23
  • Yes, I have reviewed to see if any are missing and they are all there. – Rick Savoy Apr 16 '14 at 20:26

1 Answers1

1

You have a space at the end of this parameter name:

":pers_emp_pre_id "=>$data['pers_emp_pre_id']
                 ^ here

Should be:

":pers_emp_pre_id"=>$data['pers_emp_pre_id']
MrCode
  • 63,975
  • 10
  • 90
  • 112