Jump to content

Recommended Posts

I am working on a migration project; i am not an expert in PHP though !

 

I have a class and an object of the class is used to display the values in the form controls and on submit of the form the object is updated with new values from the form and then a method in the class is invoked which performs the update query.

 

$field=$this -> $this_field;

 

The above code works perfect in PHP 4 but after moving the new machine with PHP 5 it doesnot update.

Any help would be greatly appreciated.

Link to comment
https://forums.phpfreaks.com/topic/236718-php-4-to-5-migration-need-help/
Share on other sites

This is the code of the that manipulates the data from the form

-----------------------------------------------------------------------------------------------------------------------------------------

if($empl = new tel_employee('en',$dbh,$company_id)){

 

//get employee info

$empl->get_employee_info($employee_id,$company_id);

 

//change the info for that employee_id in the db

foreach($employee_fields as $key => $field){

$post_field=$field;

if($field == 'id'){

$field = 'employee_id';

}

 

$value = $$field;

 

$new_field = 'employee_'.$field;

 

$empl -> set_value($new_field,$value);

 

}

 

#change the info for employee

if(!($empl -> change_employee_info($employee_id))){

error('change employee '.$empl -> error);

}else{

echo 'Success';

}

}

}

 

-----------------------------------------------------------------------------------------------------------------------------------------

This is code which forms the query for update

 

-----------------------------------------------------------------------------------------------------------------------------------------

function do_query($query,$update = 0){

$query .= "employee SET ";

//look if any of the fields have a value and change them if so

foreach($this -> employee_fields as $key => $field){

$this_field = 'employee_'.$field;

 

if($this -> $this_field != ''){ 

$query .= "$field='".$this -> $this_field."',";

}

}

if($update){

//we are updating an existing employee

$query .= " WHERE id='$update'";

}

else{

//adding a new employee, set the initial date

$query .= ",first_entry=now(),split_plan_new_status = '1'";

}

 

if(mysql_query($query)){

$this -> employee_id = $update;

return $update;

}

else{

$this -> error .= 'something wrong with the query : '.$query.'<br>'.mysql_error();

$this -> error_nr = '';

return FALSE;

}

}

-----------------------------------------------------------------------------------------------------------------------------------------

Please provide the entire class file so that we can help you better. Also, try changing the following in the code:

 

From:

$this_field = 'employee_'.$field;

if($this -> $this_field != ''){ 
    $query .= "$field='".$this -> $this_field."',";
}

 

To:

$this->this_field = 'employee_'.$field;

if($this ->this_field != ''){ 
    $query .= "$field='".$this->this_field."',";
}

 

Hope it helps!

The class is a very huge one -i have included the member variable declaration and the methods used in my code....here

 


 

class teleworking_employee{

 

var $lang = 'nl';

var $error = '';

var $error_nr = '';

var $dbh = '';

var $employee_id = '';

var $employee_name = '';

var $employee_firstname = '';

var $company_id = '';

var $project_id = '';

        var $employee_fields = array(

'id',

'last_change','first_entry',

'company_id','project_id',

'name','firstname',

'phone',

'comment',

'updated');

 

/**

* constructor:

* set the language

* @param language, database handle, company_id[,project_id]

* @return void

**/

 

function teleworking_employee($lang,$dbh,$company_id,$project_id=''){

$this -> lang = $lang;

$this -> dbh = $dbh;

$this -> company_id = $company_id;

if($project_id != ''){

$this -> project_id = $project_id;

}

}

 

function get_employee_info($employee_id,$company_id = ''){

if(!$company_id){

$company_id = $this -> company_id;

}

$query = "SELECT

id,

last_change,

first_entry,

company_id,

project_id,

name,

firstname,

phone,

deleted,

comment,

updated,

billed

  FROM employee WHERE company_id='$company_id' AND id='$employee_id'";

if($this -> project_id != ''){

$query .= " AND project_id='".$this -> project_id."'";

}

 

//print $query;

#$get = mysql_query($query,$this -> dbh);

$get = mysql_query($query);

if(mysql_num_rows($get)){

$result = mysql_fetch_object($get);

foreach($this -> employee_fields as $key => $field){

if($field == 'id'){

$field = 'project_'.$field;

}

$this_field = 'employee_'.$field;

$this -> $this_field = stripslashes($result -> $field);

}

return TRUE;

}else{

$this -> error .= 'no employee found';

$this -> error_nr = '';

return FALSE;

}

}

 

function change_employee_info($employee_id){

$query = 'UPDATE ';

return $this -> do_query($query,$employee_id);

}

 

function set_value($what,$value){

if($what == 'employee_phone'){

$value = preg_replace('/[^\d]/','',$value);

}

$this -> what = addslashes($value);

 

}

}

}


Sorry -here is teh code....

 

class teleworking_employee{

   var $lang            = 'nl';
   var $error            = '';
   var $error_nr            = '';
   var $dbh            = '';
   var $employee_id         = '';
   var $employee_name         = '';
   var $employee_firstname         = '';
   var $company_id            = '';
   var $project_id            = '';
        var $employee_fields = array(
         'id',
         'last_change','first_entry',
         'company_id','project_id',
         'name','firstname',
         'phone',
         'comment',
         'updated');

/**
   * constructor:
   * set the language
   * @param language, database handle, company_id[,project_id]
   * @return void
   **/
   
   function teleworking_employee($lang,$dbh,$company_id,$project_id=''){
      $this -> lang    = $lang;
      $this -> dbh   = $dbh;
      $this -> company_id = $company_id;
      if($project_id != ''){
         $this -> project_id = $project_id;
      }   
   }   

function get_employee_info($employee_id,$company_id = ''){
      if(!$company_id){
         $company_id = $this -> company_id;
      }   
      $query = "SELECT
            id,
            last_change,
            first_entry,
            company_id,
            project_id,
            name,
            firstname,
            phone,
            deleted,
            comment,
            updated,
            billed
           FROM employee WHERE company_id='$company_id' AND id='$employee_id'";
      if($this -> project_id != ''){
         $query .= " AND project_id='".$this -> project_id."'";
      }   
      
      //print $query;
      #$get = mysql_query($query,$this -> dbh);
      $get = mysql_query($query);
      if(mysql_num_rows($get)){
         $result = mysql_fetch_object($get);
         foreach($this -> employee_fields as $key => $field){
            if($field == 'id'){
               $field = 'project_'.$field;
            }
            $this_field = 'employee_'.$field;
            $this -> $this_field = stripslashes($result -> $field);
         }   
         return TRUE;
      }else{
         $this -> error .= 'no employee found';
         $this -> error_nr = '';
         return FALSE;
      }
   }

function change_employee_info($employee_id){
      $query = 'UPDATE ';
      return $this -> do_query($query,$employee_id);
}

function set_value($what,$value){
      if($what == 'employee_phone'){
         $value = preg_replace('/[^\d]/','',$value);
      }   
      $this -> what = addslashes($value);
      
   }   
}
}

Thanks for sharing the code. Per the code, I have changed some parts of the code and commented. You can also put visibility of the members for the class, i.e. instead of "var", use "public", "private" etc.

 

Try:

<?php
class teleworking_employee{
  var $lang                 = 'nl';
  var $error                = '';
  var $error_nr             = '';
  var $dbh                  = '';
  var $employee_id          = '';
  var $employee_name        = '';
  var $employee_firstname   = '';
  var $company_id           = '';
  var $project_id           = '';
  var $employee_fields = array(
         'id',
         'last_change','first_entry',
         'company_id','project_id',
         'name','firstname',
         'phone',
         'comment',
         'updated');

  /**
   * constructor:
   * set the language
   * @param language, database handle, company_id[,project_id]
   * @return void
   **/
  public function __construct($lang,$dbh,$company_id,$project_id=''){ // changed to magic constructor. still the old constructor will work
    $this -> lang    = $lang;
    $this -> dbh   = $dbh;
    $this -> company_id = $company_id;
    if($project_id != ''){
      $this -> project_id = $project_id;
    }
  }

  public function get_employee_info($employee_id,$company_id = ''){
    if(!$company_id){
      $company_id = $this -> company_id;
    }
    $query = "SELECT
            id,
            last_change,
            first_entry,
            company_id,
            project_id,
            name,
            firstname,
            phone,
            deleted,
            comment,
            updated,
            billed
           FROM employee WHERE company_id='$company_id' AND id='$employee_id'";
    if($this -> project_id != ''){
      $query .= " AND project_id='".$this -> project_id."'";
    }

    //print $query;
    #$get = mysql_query($query,$this -> dbh);
    $get = mysql_query($query);
    if(mysql_num_rows($get)){
      $result = mysql_fetch_object($get);
      foreach($this -> employee_fields as $key => $field){
        if($field == 'id'){
          $field = 'project_'.$field;
        }
        $this_field = 'employee_'.$field;
        $this -> this_field = stripslashes($result -> field); // changed
      }
      return TRUE;
    }else{
      $this -> error .= 'no employee found';
      $this -> error_nr = '';
      return FALSE;
    }
  }

  public function change_employee_info($employee_id){
    $query = 'UPDATE ';
    return $this -> do_query($query,$employee_id);
  }

  public function set_value($what,$value){
    if($what == 'employee_phone'){
      $value = preg_replace('/[^\d]/','',$value);
    }
    $this -> what = addslashes($value);

  }
}
?>

Exactly what symptom or error are you getting that leads you to believe that the code you have been posting is where the problem is? It is more likely you have a database table or connection problem that is preventing the update query from working.

I added this code and I see a list of NOTICE items displayed.

 

When i try to hardcode the Update query, it works without any problem and even with this dynamically generated query the only problem is that the variable values are not populated; the Update query gets executed just with the initial query.

Without the full code that reproduces the problem, no one can directly help you. This could as likely be a problem with your form as with the parts of the php code you have posted (the 'full' class you posted wasn't the whole class and obviously isn't all the code involved in the problem.)

 

You are asking someone who is not standing right beside you to tell you what your code is doing, without having that code.

 

If you were to echo the query and post what it actually is, that would help.

Hi - thanks for the reply...

 

I am almost sure that the code i have placed here is causing the problem since this is the flow - i tryed to insert manual breakpoints and figured out the flow..

 

The query should actually be like this, i.e i have changed the value of the employee_firstname in the form

 

UPDATE employee SET last_change=now(),employee_firstname = 'newvalue',updated='0' WHERE id='143525'

 

where as it is like this ---- the values from the form are placed into the object and then the object is looped to frame the query..

 

UPDATE employee SET last_change=now(),updated='0' WHERE id='143525'

 

I'm guessing that some of your NOTICE error messages were probably pointing to that line of code. Code should not normally produce any errors, warnings, or notice messages and you should always look to find what is causing each one and fix the problem.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.