ravinath
ravinath

Reputation: 33

print message on data insertion in database in oop

I inserting a form data in mysql database using oop php in a pre made project.all work is coorrect but after submit any success and failed message is not showing. my form sample code is:

performance.php->

   <h1>Employee Review Form</h1>
 <form name="product"  action="" method="post" id="customForm">
     <table>
          <tr>
         <td>Review Employee Id</td>
         <td><input type="text" name="rcode" id="rcode"  class="genInputBox" /></td>
     </tr>
         <tr>
             <td>Domain Knowledge(Subject Matter)</td>
             <td><select name="dk" id="dk">
                     <option value="">Plz Select Ratting </option>
                     <option value="5">5</option>
                     <option value="4">4</option>
                     <option value="3">3</option>
                     <option value="2">2</option>
                     <option value="1">1</option>
                 </select></td>
         </tr>and more...............
          <input type="submit" value="register" id="submit" name="form_submit"/>
                    </td></tr>
                        </table>
                    </form>

and form submit process

     <?php

      if(isset($_POST) && isset ($_POST["form_submit"])){
     $valArr=array("review_emp_id"=>$_POST['rcode'],"subject_matter"=>$_POST['dk']);
    $per_obj = new Performance();
    $per_obj->addPreformance($valArr, "employee_performance");


    }
     ?>

and my class performance page is:

             <?php 
   require_once("admin/includes/class_common_lib.php");
    require_once("class.userinfo.inc.php");

    class Performance extends DataAccess{

var $db_obj = null;
public function  __construct() {     //for database connectivity
    $this->db_obj = new DataAccess();
}

public function  addPreformance($key_values = array(), $table = null) {
    $this->db_obj->table_name = $table;
      $this->db_obj->addRecord($key_values);


   }
  }

   ?>

and in class common lib add record function code is:

                    // Function for Add Record
    function addRecord($key_values)
    {

        $cols="";
        $vals="";
        foreach($key_values as $key=>$value)
        {
            if ($key!="submit" and $key!="PHPSESSID" and $key!="image_name" and $key!="submit_button" and $key!="ext" and $key!="ext2" and $key!="img_name" and $key!="mode" and $value!="" and $key!="gpl" and $key!="ip1" and $key!="ip2" and $key!="ip3" and $key!="ip4"){
                $cols .= "`".$key."`,";
                is_string($value)? $vals .= "'".addslashes($value)."'," : $vals .= "'".$value."',";

            }
        }

        $cols = substr($cols, 0, -1);
        $vals = substr($vals, 0, -1);

        $insert_qry="insert into ". $this->table_name ."(". $cols .") values(". $vals .")";

        $r=mysql_query($insert_qry);
        $this->msg = (!$r) ? f_add_msg : s_add_msg;
        return $r;
    }

Its a very big project and I do all the process which is done previous by other person.

now I want to show msz of data is submit in database or not (success or failed)after submission of form.

and I found these line in config file

      define ("s_add_msg", "Record successfully added.");

     define ("f_add_msg", "Record not added. Please try again.&error=1");

I know its lengthy code, but I need to show what happens. all function Is working correctly,but how can I show success or failed message in performance.php page?please help

Upvotes: 0

Views: 639

Answers (1)

liquorvicar
liquorvicar

Reputation: 6106

It's common in programming to use the Post/Redirect/Get pattern in situations like this. Without seeing your entire codebase a quick fix for this might be to change your form submit like this:

<?php

  if(isset($_POST) && isset ($_POST["form_submit"])){
     $valArr=array("review_emp_id"=>$_POST['rcode'],"subject_matter"=>$_POST['dk']);
     $per_obj = new Performance();
     $per_obj->addPreformance($valArr, "employee_performance");

     $redirectLink = "someOtherUrl.php?m=" . urlencode( $per_obj->db_obj->msg );
     header( 'Location: ' . $redirectLink );
     die();
}
 ?>

This will redirect to the new URL you have specified where you should be able to retrieve the status message using $_GET['m'].

Eventually you'd want to handle this in a much cleaner way across the system but if you're just trying to fix legacy code that will at least get you started.

Upvotes: 1

Related Questions