anuradhu Posted May 18, 2011 Share Posted May 18, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/236718-php-4-to-5-migration-need-help/ Share on other sites More sharing options...
anupamsaha Posted May 18, 2011 Share Posted May 18, 2011 Can you please paste the code here? Quote Link to comment https://forums.phpfreaks.com/topic/236718-php-4-to-5-migration-need-help/#findComment-1216838 Share on other sites More sharing options...
jonsjava Posted May 18, 2011 Share Posted May 18, 2011 try changing it from $field=$this->$this_field; to $field=$this->this_field; I'm sure there are other issues at play, so code would be a good thing to post if you want further help. Quote Link to comment https://forums.phpfreaks.com/topic/236718-php-4-to-5-migration-need-help/#findComment-1216843 Share on other sites More sharing options...
anuradhu Posted May 18, 2011 Author Share Posted May 18, 2011 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; } } ----------------------------------------------------------------------------------------------------------------------------------------- Quote Link to comment https://forums.phpfreaks.com/topic/236718-php-4-to-5-migration-need-help/#findComment-1216844 Share on other sites More sharing options...
anuradhu Posted May 18, 2011 Author Share Posted May 18, 2011 Thank you all... i have tried changing this -- $field=$this->this_field; this returns me empty string. Quote Link to comment https://forums.phpfreaks.com/topic/236718-php-4-to-5-migration-need-help/#findComment-1216846 Share on other sites More sharing options...
jonsjava Posted May 18, 2011 Share Posted May 18, 2011 we need to see the variables. Preferably the whole class, if possible. Quote Link to comment https://forums.phpfreaks.com/topic/236718-php-4-to-5-migration-need-help/#findComment-1216850 Share on other sites More sharing options...
anupamsaha Posted May 18, 2011 Share Posted May 18, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/236718-php-4-to-5-migration-need-help/#findComment-1216853 Share on other sites More sharing options...
anuradhu Posted May 18, 2011 Author Share Posted May 18, 2011 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); } } } Quote Link to comment https://forums.phpfreaks.com/topic/236718-php-4-to-5-migration-need-help/#findComment-1216856 Share on other sites More sharing options...
anupamsaha Posted May 18, 2011 Share Posted May 18, 2011 When you add code here, please enclose the code within "code" tags (click on # button in the editor toolbar section). It will improve the readability of other users in this forum. BTW, did you able to see my last note that I suggested? Quote Link to comment https://forums.phpfreaks.com/topic/236718-php-4-to-5-migration-need-help/#findComment-1216860 Share on other sites More sharing options...
anuradhu Posted May 18, 2011 Author Share Posted May 18, 2011 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); } } } Quote Link to comment https://forums.phpfreaks.com/topic/236718-php-4-to-5-migration-need-help/#findComment-1216865 Share on other sites More sharing options...
anuradhu Posted May 18, 2011 Author Share Posted May 18, 2011 Hi anupamsaha i tried your change - but eventually it just assigns the variable name to the $this->this_field; Quote Link to comment https://forums.phpfreaks.com/topic/236718-php-4-to-5-migration-need-help/#findComment-1216867 Share on other sites More sharing options...
anupamsaha Posted May 18, 2011 Share Posted May 18, 2011 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); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/236718-php-4-to-5-migration-need-help/#findComment-1216869 Share on other sites More sharing options...
anuradhu Posted May 18, 2011 Author Share Posted May 18, 2011 Ok- i have made the changes you suggested - but still my problem is not yet resolved Quote Link to comment https://forums.phpfreaks.com/topic/236718-php-4-to-5-migration-need-help/#findComment-1216894 Share on other sites More sharing options...
anupamsaha Posted May 18, 2011 Share Posted May 18, 2011 Sorry. Then I think, the information provided here is limited to access the issue. Quote Link to comment https://forums.phpfreaks.com/topic/236718-php-4-to-5-migration-need-help/#findComment-1216901 Share on other sites More sharing options...
anuradhu Posted May 18, 2011 Author Share Posted May 18, 2011 Ah - I missed to mention this I am using the below code to generate the values into a local scope from the FORM POST - to cope with the migration to PHP 5. foreach ($_REQUEST as $field => $value) { $$field = $value; } Quote Link to comment https://forums.phpfreaks.com/topic/236718-php-4-to-5-migration-need-help/#findComment-1216905 Share on other sites More sharing options...
anupamsaha Posted May 18, 2011 Share Posted May 18, 2011 Try using extract($_POST); or extract($_GET); Instead of your way. Quote Link to comment https://forums.phpfreaks.com/topic/236718-php-4-to-5-migration-need-help/#findComment-1216915 Share on other sites More sharing options...
anuradhu Posted May 19, 2011 Author Share Posted May 19, 2011 thank you i did this.... but - do you still have any other clue for my problem? pls... Quote Link to comment https://forums.phpfreaks.com/topic/236718-php-4-to-5-migration-need-help/#findComment-1217470 Share on other sites More sharing options...
anupamsaha Posted May 19, 2011 Share Posted May 19, 2011 Put the following at the top of the code and let me know what you see in the output: error_reporting(E_ALL); ini_set('display_errors', 1); Quote Link to comment https://forums.phpfreaks.com/topic/236718-php-4-to-5-migration-need-help/#findComment-1217472 Share on other sites More sharing options...
PFMaBiSmAd Posted May 19, 2011 Share Posted May 19, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/236718-php-4-to-5-migration-need-help/#findComment-1217477 Share on other sites More sharing options...
anuradhu Posted May 19, 2011 Author Share Posted May 19, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/236718-php-4-to-5-migration-need-help/#findComment-1217501 Share on other sites More sharing options...
PFMaBiSmAd Posted May 19, 2011 Share Posted May 19, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/236718-php-4-to-5-migration-need-help/#findComment-1217502 Share on other sites More sharing options...
anuradhu Posted May 19, 2011 Author Share Posted May 19, 2011 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' Quote Link to comment https://forums.phpfreaks.com/topic/236718-php-4-to-5-migration-need-help/#findComment-1217504 Share on other sites More sharing options...
PFMaBiSmAd Posted May 19, 2011 Share Posted May 19, 2011 The following line in your set_value() method is missing the $ in front of the what and never was working as posted - $this -> what = addslashes($value); Quote Link to comment https://forums.phpfreaks.com/topic/236718-php-4-to-5-migration-need-help/#findComment-1217508 Share on other sites More sharing options...
PFMaBiSmAd Posted May 19, 2011 Share Posted May 19, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/236718-php-4-to-5-migration-need-help/#findComment-1217512 Share on other sites More sharing options...
anuradhu Posted May 19, 2011 Author Share Posted May 19, 2011 When i make the code like this $this -> $what = addslashes($value); I receive the following error: Fatal error: Cannot access empty property in /export/web/application/engine/employee.class on line 223 Quote Link to comment https://forums.phpfreaks.com/topic/236718-php-4-to-5-migration-need-help/#findComment-1217529 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.