Array_push($errors

eight/12

5 Updating Data

In this chapter, you extend the Anyco 60 minutes awarding with forms that enable you to insert, update, and delete an employee tape.

  • Edifice the Basic Employees folio

  • Extending the Bones Employees Folio

  • Combining Departments and Employees

  • Adding Error Recovery

  • Further Error Handling

Building the Basic Employees page

In this section, you will extend your application to include a basic employees page.

To display employee records, perform the following tasks:

  1. Create the chap5 directory, re-create the application files from chap4, and change to the newly created directory:

    On Windows:

    mkdir c:\program files\Apache Group\Apache2\htdocs\chap5 cd c:\plan files\Apache Grouping\Apache2\htdocs\chap5 re-create ..\chap4\* .              

    On Linux:

    mkdir $HOME/public_html/chap5 cd $HOME/public_html/chap5 cp ../chap4/* .              
  2. Edit the anyco.php file. Add a construct_employees() role. This office constructs the employee query, calls the db_do_query() function to execute the query, and prints the results using the ui_print_employees() role:

                    function construct_employees()                {                                  $query =                                  "SELECT employee_id,                                  substr(first_name,1,1) || '.  '|| last_name equally employee_name,                                  hire_date,                                  to_char(salary, '9999G999D99') as salary,                                  nvl(commission_pct,0) as commission_pct                                  FROM   employees                                  ORDER BY employee_id asc";                                  $conn = db_connect();                                  $emp = db_do_query($conn, $query);                                  ui_print_header('Employees');                                  ui_print_employees($emp);                                  ui_print_footer(engagement('Y-yard-d H:i:southward'));                }              

    In that location is no need to laissez passer a $bindargs parameter to the db_do_query() phone call considering this query does not use bind variables. The db_do_query() declaration will provide a default value of an empty array automatically. PHP allows functions to have variable numbers of parameters.

  3. Edit the anyco.php file. Supersede the call to construct_departments() with a phone call to construct_employees():

    <?php // File: anyco.php  require('anyco_cn.inc'); require('anyco_db.inc'); require('anyco_ui.inc');  session_start();                construct_employees();                ... ?>              
  4. Edit the anyco_ui.inc file. Implement the presentation of employee data in an HTML table by calculation a ui_print_employees() function:

                    role ui_print_employees($employeerecords)                {                                  if (!$employeerecords) {                                  echo '<p>No Employee found</p>';                                  }                                  else {                                  echo <<<END                                  <table>                                  <tr>                                  <thursday>Employee<br>ID</th>                                  <th>Employee<br>Proper noun</th>                                  <th>Hiredate</th>                                  <thursday>Salary</th>                                  <th>Committee<br>(%)</th>                                  </tr>                Finish;                                  // Write one row per employee                                  foreach ($employeerecords as $emp) {                                  echo '<tr>';                                  repeat '<td align="right">'.                                  htmlentities($emp['EMPLOYEE_ID']).'</td>';                                  echo '<td>'.htmlentities($emp['EMPLOYEE_NAME']).'</td>';                                  echo '<td>'.htmlentities($emp['HIRE_DATE']).'</td>';                                  echo '<td align="correct">'.                                  htmlentities($emp['Salary']).'</td>';                                  repeat '<td align="right">'.                                  htmlentities($emp['COMMISSION_PCT']).'</td>';                                  echo '</tr>';                                  }                                  echo <<<END                                  </table>                Stop;                                  }                }              
  5. Save the changes to the anyco.php and anyco_ui.inc files. Test the event of these changes past entering the post-obit URL in your Spider web browser:

    On Windows:

    http://localhost/chap5/anyco.php              

    On Linux:

    http://localhost/~<username>/chap5/anyco.php              

    Examine the result page, and scroll down to view all the employee records displayed in the page:

    Description of chap5_basic_emp_001.gif follows
    Description of the illustration chap5_basic_emp_001.gif

Extending the Basic Employees Page

In this section, you will extend the bones employees page to include the ability to manipulate employee records.

To enable employee records to be manipulated, perform the following tasks:

  1. Edit the anyco.php file. Replace the construct_employees() call with the course handler control logic to manage the requests for showing, inserting, updating, and deleting employee records:

    <?php // File: anyco.php  require('anyco_cn.inc'); require('anyco_db.inc'); crave('anyco_ui.inc');  session_start();                // Start form handler code                if (isset($_POST['insertemp'])) {                                  construct_insert_emp();                }                elseif (isset($_POST['saveinsertemp'])) {                                  insert_new_emp();                }                elseif (isset($_POST['modifyemp'])) {                                  construct_modify_emp();                }                elseif (isset($_POST['savemodifiedemp'])) {                                  modify_emp();                }                elseif (isset($_POST['deleteemp'])) {                                  delete_emp();                }                else {                                  construct_employees();                }                ...              
  2. Edit the anyco.php file. Add together the construct_insert_emp() function:

                    office construct_insert_emp()                {                                  $conn = db_connect();                                  $query = "SELECT job_id, job_title                                  FROM jobs                                  Gild BY job_title ASC";                                  $jobs = db_do_query($conn, $query,                                  OCI_FETCHSTATEMENT_BY_COLUMN);                                  $query = "SELECT sysdate FROM dual";                                  $date = db_do_query($conn, $query,                                  OCI_FETCHSTATEMENT_BY_COLUMN);                                  $emp = array(                                  'DEPARTMENT_ID' => 10,      // Default to department x                                  'HIRE_DATE' => $date['SYSDATE'][0],                                  'ALLJOBIDS' => $jobs['JOB_ID'],                                  'ALLJOBTITLES' => $jobs['JOB_TITLE']                                  );                                  ui_print_header('Insert New Employee');                                  ui_print_insert_employee($emp, $_SERVER['SCRIPT_NAME']);                                  // Note: The two kinds of date used:                                  // i) SYSDATE for current date of the database system, and                                  // 2) The PHP engagement for display in the footer of each page                                  ui_print_footer(engagement('Y-yard-d H:i:s'));                }              

    The construct_insert_emp() function executes two queries to obtain default information to be used to populate the Insert New Employee class, which is displayed past the ui_print_insert_employee() function.

    The $query of the JOBS table obtains a list of all the existing chore IDs and their descriptions in order to build a list for selecting a job type in the HTML course generated by the ui_print_insert_employee() function.

    The $query using SYSDATE obtains the current database date and time for setting the default hire date of the new employee.

    There are two kinds of date used in the application code, the PHP date() function for printing the date and time in the page footer, and the Oracle SYSDATE office to obtain the default date and time for displaying in the hire date field of the Employees page and to ensure that text is entered in the correct database format.

    The two db_do_query() function calls provide an additional parameter value OCI_FETCHSTATEMENT_BY_COLUMN to specify that the return type for the query is an array of column values.

  3. Edit the anyco.php file. Add the insert_new_emp() function to insert an employee record into the EMPLOYEES tabular array:

                    part insert_new_emp()                {                                  $newemp = $_POST;                                  $statement =                                  "INSERT INTO employees                                  (employee_id, first_name, last_name, e-mail, hire_date,                                  job_id, salary, commission_pct, department_id)                                  VALUES (employees_seq.nextval, :fnm, :lnm, :eml, :hdt, :jid,                                  :sal, :cpt, :did)";                                  $conn = db_connect();                                  $emailid = $newemp['firstname'].$newemp['lastname'];                                  $bindargs = array();                                  array_push($bindargs, array('FNM', $newemp['firstname'], -one));                                  array_push($bindargs, array('LNM', $newemp['lastname'], -1));                                  array_push($bindargs, array('EML', $emailid, -1));                                  array_push($bindargs, array('HDT', $newemp['hiredate'], -1));                                  array_push($bindargs, array('JID', $newemp['jobid'], -i));                                  array_push($bindargs, array('SAL', $newemp['salary'], -i));                                  array_push($bindargs, assortment('CPT', $newemp['commpct'], -1));                                  array_push($bindargs, assortment('DID', $newemp['deptid'], -1));                                  $r = db_execute_statement($conn, $argument, $bindargs);                                  construct_employees();                }              

    The return value from the db_execute_statement() office is ignored and non even assigned to a variable, because no action is performed on its outcome.

  4. Edit the anyco.php file. Add the construct_modify_emp() function to build the HTML grade for updating an employee tape.

                    role construct_modify_emp()                {                                  $empid = $_POST['emprec'];                                  $query =                                  "SELECT employee_id, first_name, last_name, email, hire_date,                                  salary, nvl(commission_pct,0) as commission_pct                                  FROM   employees                                  WHERE  employee_id = :empid";                                  $conn = db_connect();                                  $bindargs = array();                                  array_push($bindargs, array('EMPID', $empid, -1));                                  $emp = db_do_query($conn, $query, OCI_FETCHSTATEMENT_BY_ROW,                                                  $bindargs);                                  ui_print_header('Modify Employee ');                                  ui_print_modify_employee($emp[0], $_SERVER['SCRIPT_NAME']);                                  ui_print_footer(date('Y-m-d H:i:s'));                }              
  5. Edit the anyco.php file. Add the modify_emp() function to update the employee tape in the EMPLOYEES tabular array, using the update form field values:

                    function modify_emp()                {                                  $newemp = $_POST;                                  $statement =                                  "UPDATE employees                                  SET   first_name = :fnm, last_name = :lnm, email = :eml,                                  salary = :sal, commission_pct = :cpt                                  WHERE employee_id = :eid";                                  $conn = db_connect();                                  $bindargs = array();                                  array_push($bindargs, array('EID', $newemp['empid'], -1));                                  array_push($bindargs, array('FNM', $newemp['firstname'], -1));                                  array_push($bindargs, array('LNM', $newemp['lastname'], -1));                                                  array_push($bindargs, array('EML', $newemp['email'], -one));                                  array_push($bindargs, array('SAL', $newemp['salary'], -1));                                  array_push($bindargs, array('CPT', $newemp['commpct'], -1));                                  $r = db_execute_statement($conn, $statement, $bindargs);                                  construct_employees();                }              
  6. Edit the anyco.php file. Add the delete_emp() function to delete an employee record from the EMPLOYEES tabular array:

                    role delete_emp()                {                                  $empid = $_POST['emprec'];                                  $statement = "DELETE FROM employees                                  WHERE employee_id = :empid";                                  $conn = db_connect();                                  $bindargs = array();                                  array_push($bindargs, array('EMPID', $empid, 10));                                  $r = db_execute_statement($conn, $statement, $bindargs);                                  construct_employees();                }              
  7. Edit the anyco.php file. In the construct_employees() function, modify the db_do_query() call to supply OCI_FETCHSTATEMENT_BY_ROW equally the last parameter, and provide $_SERVER['SCRIPT_NAME'] as second parameter in the ui_print_employees() call. The part becomes:

    function construct_employees() {   $query =   "SELECT employee_id,     substr(first_name,one,one) || '.  '|| last_name as employee_name,     hire_date,     to_char(salary, '9999G999D99') every bit salary,     nvl(commission_pct,0) every bit commission_pct    FROM   employees    Society Past employee_id asc";    $conn = db_connect();   $emp = db_do_query($conn, $query, OCI_FETCHSTATEMENT_BY_ROW);    ui_print_header('Employees');   ui_print_employees($emp, $_SERVER['SCRIPT_NAME']);   ui_print_footer(date('Y-k-d H:i:s')); }              
  8. Edit the anyco_db.inc file. Add $resulttype as a 3rd parameter to the db_do_query() function. Supplant the final parameter value, OCI_FETCHSTATEMENT_BY_ROW, in the oci_fetch_all() phone call with a variable, and so that callers can cull the output type.

    role db_do_query($conn, $statement, $resulttype,                      $bindvars = array()) {   $stid = oci_parse($conn, $statement);    ...    $r = oci_fetch_all($stid, $results, null, zip, $resulttype);   return($results); }              
  9. Edit the anyco_db.inc file. Within the db_get_page_data() office, insert OCI_FETCHSTATEMENT_BY_ROW as the third parameter value in the db_do_query() call:

    office db_get_page_data($conn, $q1, $current = one,                           $rowsperpage = 1, $bindvars = assortment()) {     ...    $r = db_do_query($conn, $query,                OCI_FETCHSTATEMENT_BY_ROW,                $bindvars);   render($r); }              
  10. Edit the anyco_db.inc file. Add a db_execute_statement() role to execute data manipulation statements such every bit INSERT statements:

                    function db_execute_statement($conn, $argument,                                  $bindvars = assortment())                {                                  $stid = oci_parse($conn, $argument);                                  if (!$stid) {                                  db_error($conn, __FILE__, __LINE__);                                  }                                  // Demark parameters                                  foreach ($bindvars as $b) {                                  // create local variable with caller specified bind value                                                  $$b[0] = $b[one];                                  $r = oci_bind_by_name($stid, ":$b[0]", $$b[0], $b[2]);                                  if (!$r) {                                  db_error($stid, __FILE__, __LINE__);                                  }                                  }                                  $r = oci_execute($stid);                                  if (!$r) {                                  db_error($stid, __FILE__, __LINE__);                                  }                                  return($r);                }              
  11. Edit the anyco_ui.inc file. Alter the ui_print_employees() part to produce an HTML form containing the employee rows. The function becomes:

    function ui_print_employees($employeerecords, $posturl) {   if (!$employeerecords) {     echo '<p>No Employee plant</p>';   }   else {     echo <<<END                                  <grade method="post" action="$posturl">                <table>   <tr>                                  <th>&nbsp;</th>                <th>Employee<br>ID</th>     <th>Employee<br>Proper name</thursday>     <th>Hiredate</th>     <th>Salary</th>     <thursday>Commission<br>(%)</thursday>   </tr> Terminate;     // Write 1 row per employee     foreach ($employeerecords as $emp) {       repeat '<tr>';                                  echo '<td><input type="radio" name="emprec" value="'.                                  htmlentities($emp['EMPLOYEE_ID']).'"></td>';                echo '<td marshal="correct">'.                 htmlentities($emp['EMPLOYEE_ID']).'</td>';       echo '<td>'.htmlentities($emp['EMPLOYEE_NAME']).'</td>';       echo '<td>'.htmlentities($emp['HIRE_DATE']).'</td>';       echo '<td align="right">'.                 htmlentities($emp['Bacon']).'</td>';       echo '<td align="right">'.                 htmlentities($emp['COMMISSION_PCT']).'</td>';       echo '</tr>';     }     echo <<<END   </table>                                  <input blazon="submit" value="Alter" name="modifyemp">                                  <input type="submit" value="Delete" name="deleteemp">                                  &nbsp;&nbsp;                                  <input type="submit" value="Insert new employee"                                  name="insertemp">                                  </class>                END;   } }              

    A radio push button is displayed in the first cavalcade of each row to enable you to select the tape to be modified or deleted.

  12. Edit the anyco_ui.inc file. Add together the ui_print_insert_employee() function to generate the course to input new employee information:

                    function ui_print_insert_employee($emp, $posturl)                {                                  if (!$emp) {                                  echo "<p>No employee details plant</p>";                                  }                                  else {                                  $deptid = htmlentities($emp['DEPARTMENT_ID']);                                  $hiredate = htmlentities($emp['HIRE_DATE']);                                  repeat <<<Terminate                                  <class method="post" activeness="$posturl">                                  <table>                                  <tr>                                  <td>Department ID</td>                                  <td><input type="text" name="deptid" value="$deptid"                                                  size="20"></td>                                  </tr>                                  <tr>                                  <td>Kickoff Proper name</td>                                  <td><input blazon="text" name="firstname" size="20"></td>                                  </tr>                                  <tr>                                  <td>Final Name</td>                                  <td><input type="text" name="lastname" size="20"></td>                                  </tr>                                  <tr>                                  <td>Hiredate</td>                                  <td><input type="text" proper name="hiredate" value="$hiredate"                                                  size="20"></td>                                  </tr>                                  <tr>                                  <td>Job</td>                                  <td><select name="jobid">                Terminate;                                  // Write the list of jobs                                  for ($i = 0; $i < count($emp['ALLJOBIDS']); $i++)                                  {                                  repeat '<option                                  label="'.htmlentities($emp['ALLJOBTITLES'][$i]).'"'.                                  ' value="'.htmlentities($emp['ALLJOBIDS'][$i]).'">'.                                  htmlentities($emp['ALLJOBTITLES'][$i]).'</option>';                                  }                                  echo <<<END                                  </select>                                  </td>                                  </tr>                                  <tr>                                  <td>Bacon</td>                                  <td><input type="text" name="salary" value="one"                                                  size="20"></td>                                  </tr>                                  <tr>                                  <td>Commission (%)</td>                                  <td><input type="text" name="commpct" value="0"                                                  size="20"></td>                                  </tr>                                  </tabular array>                                  <input type="submit" value="Salvage" name="saveinsertemp">                                  <input type="submit" value="Cancel" name="cancel">                                  </form>                End;                                  }                }              
  13. Edit the anyco_ui.inc file. Add the ui_print_modify_employee() function to generate the grade to update an employee record:

                    function ui_print_modify_employee($empdetails, $posturl)                {                                  if (!$empdetails) {                                  repeat '<p>No Employee record selected</p>';                                  }                                  else {                                  $fnm = htmlentities($empdetails['FIRST_NAME']);                                  $lnm = htmlentities($empdetails['LAST_NAME']);                                  $eml = htmlentities($empdetails['EMAIL']);                                  $sal = htmlentities($empdetails['Salary']);                                  $cpt = htmlentities($empdetails['COMMISSION_PCT']);                                  $eid = htmlentities($empdetails['EMPLOYEE_ID']);                                  repeat <<<END                                  <class method="postal service" action="$posturl">                                  <tabular array>                                  <tr>                                  <td>Employee ID</td>                                  <td>$eid</td></tr>                                  <tr>                                  <td>Outset Proper name</td>                                  <td><input type="text" name="firstname" value="$fnm"></td>                                  </tr>                                  <tr>                                  <td>Terminal Name</td>                                  <td><input type="text" name="lastname" value="$lnm"></td>                                  </tr>                                  <tr>                                  <td>Email Address</td>                                  <td><input type="text" proper noun="email" value="$eml"></td>                                  </tr>                                  <tr>                                  <td>Bacon</td>                                  <td><input blazon="text" name="bacon" value="$sal"></td>                                  </tr>                                  <tr>                                  <td>Commission (%)</td>                                  <td><input blazon="text" name="commpct" value="$cpt"></td>                                  </tr>                                  </table>                                  <input type="hidden" value="{$empdetails['EMPLOYEE_ID']}"                                                  proper name="empid">                                  <input type="submit" value="Salvage" proper noun="savemodifiedemp">                                  <input type="submit" value="Cancel" name="cancel">                                  </form>                Cease;                                  }                }              
  14. Save the changes to your Anyco awarding files, and exam the changes past entering the following URL in your Spider web browser:

    On Windows:

    http://localhost/chap5/anyco.php              

    On Linux:

    http://localhost/~<username>/chap5/anyco.php              

    The list of all employees is displayed with a radio button in each row.

    Description of chap5_test_emp_001.gif follows
    Description of the illustration chap5_test_emp_001.gif

    Coil to the lesser of the Employees page to view the Modify, Delete, and Insert new employee buttons:

    Description of chap5_test_emp_002.gif follows
    Description of the illustration chap5_test_emp_002.gif
  15. To insert a new employee record, click Insert new employee:

    Description of chap5_test_emp_003.gif follows
    Clarification of the illustration chap5_test_emp_003.gif

    When you create or modify employee records, you will see that the database definitions require the salary to be greater than zero, and the commission to exist less than 1. The commission will be rounded to ii decimal places. In the Insert New Employee page, the Department ID field contains 10 (the default), Hiredate contains the current date (in default database appointment format), Salary contains one, and Commission (%) contains 0. Enter the post-obit field values:

    Commencement Proper name: James

    Last Name: Bail

    Job: Select Developer from the list.

    Salary: supplant the 1 with 7000

    Click Save.

    Description of chap5_test_emp_004.gif follows
    Description of the illustration chap5_test_emp_004.gif
  16. When the new employee record is successfully inserted, the Web folio is refreshed with the grade listing all employees. Scroll the Spider web page to the last tape and check that the new employee record is present. The employee ID assigned to the new record on your system may exist dissimilar than the one shown in the following example:

    Description of chap5_test_emp_005.gif follows
    Clarification of the illustration chap5_test_emp_005.gif
  17. To modify the new employee tape, select the radio button next to the new employee tape, and click Modify:

    Description of chap5_test_emp_006.gif follows
    Clarification of the analogy chap5_test_emp_006.gif
  18. In the Change Employee folio, modify the E-mail Address field to JBOND, increase the Salary to 7100, and click Save:

    Description of chap5_test_emp_007.gif follows
    Description of the illustration chap5_test_emp_007.gif
  19. Successfully updating the employee tape causes the Employees folio to be redisplayed. Scroll to the last employee record and confirm that the bacon for James Bond is at present 7,100:

    Description of chap5_test_emp_008.gif follows
    Description of the analogy chap5_test_emp_008.gif
  20. To remove the new employee record, select the radio button for the new employee tape, and click Delete:

    Description of chap5_test_emp_009.gif follows
    Clarification of the illustration chap5_test_emp_009.gif

    On successful deletion, the deleted row does not appear in the listing of employee records redisplayed in the Employees page:

    Description of chap5_test_emp_010.gif follows
    Description of the illustration chap5_test_emp_010.gif

Combining Departments and Employees

In this section, you will change your application to enable access to both Employees and Departments pages.

To combine the Departments and Employees pages, perform the following tasks:

  1. Edit the anyco.php file. Modify the query in the construct_employees() function to include a WHERE clause to compare the department_id with a value in a bind variable chosen :did. This makes the page display employees in one department at a fourth dimension. Get the deptid session parameter value to populate the bind variable:

    $query =  "SELECT employee_id,           substr(first_name,1,1) || '.  '|| last_name equally employee_name,          hire_date,          to_char(salary, '9999G999D99') as salary,           nvl(commission_pct,0) as commission_pct   FROM   employees                                  WHERE  department_id = :did                Club Past employee_id asc";                $deptid = $_SESSION['deptid'];              
  2. Edit the anyco.php file. In the construct_employees() office, update the call to the db_do_query() function to pass the bind information:

    $conn = db_connect();                $bindargs = assortment();                array_push($bindargs, array('DID', $deptid, -1));                $emp = db_do_query($conn, $query, OCI_FETCHSTATEMENT_BY_ROW, $bindargs);              
  3. Edit the anyco.php file. In the construct_departments() function, save the section identifier in a session parameter:

    $_SESSION['currentdept'] = $current;                $_SESSION['deptid'] = $deptid;              

    This saves the current department identifier from the Departments page as a session parameter, which is used in the Employees page.

  4. Edit the anyco.php file. Create a role get_dept_name() to query the department name for printing in the Departments and Employees page titles:

                    function get_dept_name($conn, $deptid)                {                                  $query =                                  'SELECT department_name                                  FROM   departments                                  WHERE  department_id = :did';                                  $conn = db_connect();                                  $bindargs = array();                                  array_push($bindargs, array('DID', $deptid, -one));                                  $dn = db_do_query($conn, $query,OCI_FETCHSTATEMENT_BY_COLUMN,                                  $bindargs);                                  return($dn['DEPARTMENT_NAME'][0]);                }              
  5. Edit the anyco.php file. Modify the construct_employees() role to impress the department proper name in the Employees page heading:

                    $deptname = get_dept_name($conn, $deptid);                ui_print_header('Employees: '.$deptname);              
  6. Edit the anyco.php file. Modify the construct_departments() part to print the section name in the Departments folio heading:

                    $deptname = get_dept_name($conn, $deptid);                ui_print_header('Department: '.$deptname);              
  7. Edit the anyco.php file. Change the construct_insert_emp() part so that the default section is obtained from the session parameter passed in the $emp assortment to the ui_print_insert_employee() role. The office becomes:

    office construct_insert_emp() {                                  $deptid = $_SESSION['deptid'];                $conn = db_connect();   $query = "SELECT job_id, job_title FROM jobs Lodge BY job_title ASC";   $jobs = db_do_query($conn, $query, OCI_FETCHSTATEMENT_BY_COLUMN);   $query = "SELECT sysdate FROM dual";   $date = db_do_query($conn, $query, OCI_FETCHSTATEMENT_BY_COLUMN);   $emp = array(     'DEPARTMENT_ID' =>                $deptid,                'HIRE_DATE' => $engagement['SYSDATE'][0],     'ALLJOBIDS' => $jobs['JOB_ID'],     'ALLJOBTITLES' => $jobs['JOB_TITLE']     );   ui_print_header('Insert New Employee');   ui_print_insert_employee($emp, $_SERVER['SCRIPT_NAME']);   ui_print_footer(date('Y-m-d H:i:south')); }              
  8. Edit the anyco.php file. Alter the final else statement in the HTML form handler. The handler becomes:

    // Kickoff form handler code if (isset($_POST['insertemp'])) {   construct_insert_emp(); } elseif (isset($_POST['saveinsertemp'])) {   insert_new_emp(); } elseif (isset($_POST['modifyemp'])) {   construct_modify_emp(); } elseif (isset($_POST['savemodifiedemp'])) {   modify_emp(); } elseif (isset($_POST['deleteemp'])) {   delete_emp(); }                elseif (   isset($_POST['showemp'])) {                                  construct_employees();                }                elseif (   isset($_POST['nextdept'])                                  || isset($_POST['prevdept'])                                  || isset($_POST['firstdept'])                                  || isset($_POST['showdept'])) {                                  construct_departments();                }                else {                                  construct_departments();                }              
  9. Edit the anyco_ui.inc file. In the ui_print_department() function, change the HTML grade to enable it to call the Employees page:

    ... <form method="post" action="$posturl"> <input type="submit" value="First" name="firstdept"> <input type="submit" value="< Previous" proper noun="prevdept"> <input type="submit" value="Next >" name="nextdept">                &nbsp;&nbsp;&nbsp;                <input type="submit" value="Testify Employees" proper name="showemp">                </course> ...              
  10. Edit the anyco_ui.inc file. In the ui_print_employees() function, alter the HTML course to enable it to call the Departments page:

    ... </table> <input type="submit" value="Alter" proper noun="modifyemp"> <input type="submit" value="Delete" name="deleteemp"> &nbsp;&nbsp; <input type="submit" value="Insert new employee" name="insertemp">                &nbsp;&nbsp;                <input type="submit" value="Return to Departments" proper name="showdept">                </form> ...              
  11. Salvage the changes to your PHP files. In your browser, test the changes by entering the post-obit URL:

    On Windows:

    http://localhost/chap5/anyco.php              

    On Linux:

    http://localhost/~<username>/chap5/anyco.php              

    The Departments folio is displayed.

    Description of chap5_combine_deptemp_001.gif follows
    Description of the illustration chap5_combine_deptemp_001.gif

    To brandish a list of employees in the department, click the Show Employees push button.

    Description of chap5_combine_deptemp_002.gif follows
    Description of the illustration chap5_combine_deptemp_002.gif

    You can return to the Departments page by clicking the Return to Departments button. Experiment by navigating to another department and list its employees to show the procedure of switching betwixt the Departments and Employees pages.

Adding Fault Recovery

Fault direction is ever a meaning blueprint conclusion. In production systems, you might want to classify errors and handle them in unlike ways. Fatal errors could exist redirected to a standard "site not bachelor" page or home page. Data errors for new record creation might return to the appropriate class with invalid fields highlighted.

In near production systems, you would ready the display_errors configuration option in the php.ini file to off, and the log_errors configuration pick to on.

You can utilize the PHP output buffering functionality to trap error text when a function is executing. Using ob_start() prevents text from displaying on the screen. If an mistake occurs, the ob_get_contents() function allows the previously generated error messages to be stored in a string for later brandish or analysis.

Now you change the application to display fault messages and database errors on a new page using a custom error handling function. Errors are now returned from the db* functions keeping them silent.

  1. Edit the anyco_db.inc file. Change the db_error() function to render the mistake data in an array structure, instead of printing and quitting. The function becomes:

    function db_error($r = false, $file, $line) {   $err =  $r ? oci_error($r) : oci_error();    if (isset($err['message'])) {     $m = htmlentities($err['bulletin']);                                  $c = $err['code'];                }   else {     $thousand = 'Unknown DB error';                                  $c = null;                }                                  $rc = array(                                  'Bulletin' => $g,                                  'Lawmaking'    => $c,                                  'FILE'    => $file,                                  'LINE'    => $line                                  );                                  return $rc;                }              
  2. Edit the anyco_db.inc file. For every call to the db_error() role, assign the return value to a variable called $e and add a return faux; statement later each call:

                    if (<fault test>)                {                $eastward = db_error(<handle>, __FILE__, __LINE__);                                  render false;                }              

    Make sure to keep the <error examination> and <handle> parameters the same equally they are currently specified for each call. Recall that the __FILE__ and __LINE__ constants help to pinpoint the location of the failure during development. This is useful information to log for fatal errors in a production deployment of an awarding.

  3. Edit the anyco_db.inc file. Add a $due east parameter to every role to enable the return of error information. Utilise the & reference prefix to ensure that results are returned to the calling function. Each function declaration becomes:

    office db_connect(&$e) {...}  role db_get_page_data($conn, $q1, $currrownum = 1, $rowsperpage = i,                &$e,                $bindvars = assortment()) {...}  function db_do_query($conn, $statement, $resulttype,                &$due east,                $bindvars = array()) {...}  function db_execute_statement($conn, $statement,                &$e,                $bindvars = array()) {...}              
  4. Edit the anyco_db.inc file. In the db_get_page_data() function, change the telephone call to the db_do_query() function to laissez passer down the mistake parameter $due east:

    $r = db_do_query($conn, $query, OCI_FETCHSTATEMENT_BY_ROW,                $e,                $bindvars);              
  5. Edit the anyco_db.inc file. Add an @ prefix to all oci_* part calls. For instance:

    @ $r = @oci_execute($stid);              

    The @ prefix prevents errors from displaying because each return result is tested. Preventing errors from displaying tin hide incorrect parameter usage, which may hinder testing the changes in this section. You do not need to add @ prefixes, but it can effect future results when errors are displayed.

  6. Edit the anyco.php file. Create a office to handle the mistake information:

                    function handle_error($message, $err)                {                                  ui_print_header($message);                                  ui_print_error($err, $_SERVER['SCRIPT_NAME']);                                  ui_print_footer(date('Y-m-d H:i:s'));                }              
  7. Edit the anyco.php file. Change all calls to db_* functions to include the additional error parameter:

    Steps 8 to fifteen show the complete new functions, and so the code changes in this step can be skipped.

    • Alter all db_connect() calls to db_connect($err).

    • Change all db_do_query() calls and insert a $err parameter as the quaternary parameter. For instance, the call in construct_employees() becomes:

      $emp = db_do_query($conn, $query,                     OCI_FETCHSTATEMENT_BY_ROW, $err, $bindargs);                  

      Change the other four db_do_query() calls in anyco.php remembering to keep the existing parameter values of each specific call.

    • Modify the db_get_page_data() phone call and insert a $err parameter equally the fifth parameter:

      $dept = db_get_page_data($conn, $query, $current, 1, $err);                  
    • Change the db_execute_statement() calls and insert a $err parameter as the third parameter, for example:

      $r = db_execute_statement($conn, $statement, $err, $bindargs);                  
  8. Edit the anyco.php file. Modify the construct_departments() function to handle errors returned. The function becomes:

    office construct_departments() {   if (isset($_SESSION['currentdept']) && isset($_POST['prevdept']) &&              $_SESSION['currentdept'] > 1)      $electric current = $_SESSION['currentdept'] - ane;   elseif (isset($_SESSION['currentdept']) && isset($_POST['nextdept']))      $current = $_SESSION['currentdept'] + one;   elseif (isset($_POST['showdept']) && isset($_SESSION['currentdept']))      $electric current = $_SESSION['currentdept'];   else      $current = 1;    $query =     "SELECT d.department_id, d.department_name,             substr(due east.first_name,1,i)||'. '|| e.last_name every bit manager_name,             c.country_name, count(e2.employee_id) as number_of_employees      FROM   departments d, employees e, locations l,              countries c, employees e2      WHERE  d.manager_id    = e.employee_id      AND    d.location_id   = 50.location_id      AND    d.department_id = e2.department_id      AND    50.country_id    = c.country_id      GROUP BY d.department_id, d.department_name,               substr(due east.first_name,i,one)||'. '||e.last_name, c.country_name      Order BY d.department_id ASC";    $conn = db_connect($err);                                  if (!$conn) {                                  handle_error('Connectedness Mistake', $err);                                  }                                  else {                $dept = db_get_page_data($conn, $query, $current, 1,                $err);                                  if ($dept === false) {                                                  // Apply === so empty array at stop of fetch is not matched                                  handle_error('Cannot fetch Departments', $err);                                  } else {                                  if (!isset($dept[0]['DEPARTMENT_ID']) && $electric current > 1) {                                // no more records so go back one                                  $current--;                                  $dept = db_get_page_data($conn, $query, $electric current, 1, $err);                                  }                $deptid = $dept[0]['DEPARTMENT_ID'];        $_SESSION['deptid'] = $deptid;       $_SESSION['currentdept'] = $electric current;        $deptname = get_dept_name($conn, $deptid);       ui_print_header('Department: '.$deptname);       ui_print_department($dept[0], $_SERVER['SCRIPT_NAME']);       ui_print_footer(date('Y-yard-d H:i:s'));                                  }                                  }                }              
  9. Edit the anyco.php file. Alter the construct_employees() function to handle errors. The function becomes:

    role construct_employees() {   $query =     "SELECT employee_id,             substr(first_name,ane,1) || '.  '|| last_name as employee_name,             hire_date,             to_char(bacon, '9999G999D99') as salary,             nvl(commission_pct,0) as commission_pct      FROM   employees      WHERE  department_id = :did      ORDER By employee_id asc";    $deptid = $_SESSION['deptid'];    $conn = db_connect($err);                                  if (!$conn) {                                  handle_error('Connectedness Error', $err);                                  }                                  else {                $bindargs = array();     array_push($bindargs, array('DID', $deptid, -1));     $emp = db_do_query($conn, $query, OCI_FETCHSTATEMENT_BY_ROW,                $err,                $bindargs);                                  if (!$emp) {                                  handle_error('Cannot fetch Employees', $err);                                  }                                  else {                $deptname = get_dept_name($conn, $deptid);       ui_print_header('Employees: '.$deptname);       ui_print_employees($emp, $_SERVER['SCRIPT_NAME']);       ui_print_footer(appointment('Y-yard-d H:i:s'));                                  }                                  }                }              
  10. Edit the anyco.php file. Modify the construct_insert_emp() function to handle errors. The part becomes:

    function construct_insert_emp() {   $deptid = $_SESSION['deptid'];   $conn = db_connect($err);                                  if (!$conn) {                                  handle_error('Connexion Mistake', $err);                                  }                                  else {                $query = "SELECT job_id, job_title FROM jobs ORDER BY job_title ASC";     $jobs = db_do_query($conn, $query, OCI_FETCHSTATEMENT_BY_COLUMN, $err);     $query = "SELECT sysdate FROM dual";     $date = db_do_query($conn, $query, OCI_FETCHSTATEMENT_BY_COLUMN, $err);      $emp = array(       'DEPARTMENT_ID' => $deptid,       'HIRE_DATE' => $engagement['SYSDATE'][0],       'ALLJOBIDS' => $jobs['JOB_ID'],       'ALLJOBTITLES' => $jobs['JOB_TITLE']       );      ui_print_header('Insert New Employee');     ui_print_insert_employee($emp, $_SERVER['SCRIPT_NAME']);     ui_print_footer(date('Y-k-d H:i:s'));                                  }                }              
  11. Edit the anyco.php file. Alter the insert_new_emp() function to handle errors. The function becomes:

    function insert_new_emp() {   $argument =     'INSERT INTO employees                  (employee_id, first_name, last_name, electronic mail, hire_date,                  job_id, salary, commission_pct, department_id)      VALUES (employees_seq.nextval, :fnm, :lnm, :eml, :hdt,             :jid, :sal, :cpt, :did)';    $newemp = $_POST;    $conn = db_connect($err);                                  if (!$conn) {                                  handle_error('Connect Error', $err);                                  }                                  else {                $emailid = $newemp['firstname'].$newemp['lastname'];      $bindargs = assortment();     array_push($bindargs, array('FNM', $newemp['firstname'], -one));     array_push($bindargs, array('LNM', $newemp['lastname'], -1));     array_push($bindargs, assortment('EML', $emailid, -1));     array_push($bindargs, array('HDT', $newemp['hiredate'], -ane));     array_push($bindargs, array('JID', $newemp['jobid'], -1));     array_push($bindargs, assortment('SAL', $newemp['salary'], -1));     array_push($bindargs, array('CPT', $newemp['commpct'], -1));     array_push($bindargs, array('DID', $newemp['deptid'], -one));      $r = db_execute_statement($conn, $statement,                $err,                $bindargs);                                  if ($r) {                construct_employees();                                  }                                  else {                                  handle_error('Cannot insert employee', $err);                                  }                } }              
  12. Edit the anyco.php function. Alter the construct_modify_emp() function to handle errors. The function becomes:

    role construct_modify_emp() {                                  if (!isset($_POST['emprec'])) { // User did not select a record                                  construct_employees();                                  }                                  else {                $empid = $_POST['emprec'];      $query =       "SELECT employee_id, first_name, last_name, email, hire_date,               bacon, nvl(commission_pct,0) every bit commission_pct        FROM   employees        WHERE  employee_id = :empid";      $conn = db_connect($err);                                  if (!$conn) {                                  handle_error('Connect Error', $err);                                  }                                  else {                $bindargs = assortment();       array_push($bindargs, array('EMPID', $empid, -1));        $emp = db_do_query($conn, $query, OCI_FETCHSTATEMENT_BY_ROW,                $err,                $bindargs);                                  if (!$emp) {                                  handle_error('Cannot find details for employee '.$empid, $err);                                  }                                  else {                ui_print_header('Alter Employee ');         ui_print_modify_employee($emp[0], $_SERVER['SCRIPT_NAME']);         ui_print_footer(date('Y-m-d H:i:s'));                                  }                                  }                                  }                }              
  13. Edit the anyco.php file. Change the modify_emp() function to handle errors. The function becomes:

    role modify_emp() {   $newemp = $_POST;    $statement =     "UPDATE employees      Set up    first_name = :fnm, last_name = :lnm, e-mail = :eml,             bacon = :sal, commission_pct = :cpt      WHERE  employee_id = :eid";    $conn = db_connect($err);                                  if (!$conn) {                                  handle_error('Connect Error', $err);                                  }                                  else {                $bindargs = assortment();     array_push($bindargs, array('EID', $newemp['empid'], -ane));     array_push($bindargs, array('FNM', $newemp['firstname'], -1));     array_push($bindargs, array('LNM', $newemp['lastname'], -i));     array_push($bindargs, array('EML', $newemp['e-mail'], -1));     array_push($bindargs, array('SAL', $newemp['salary'], -ane));     array_push($bindargs, array('CPT', $newemp['commpct'], -1));      $r = db_execute_statement($conn, $argument,                $err,                $bindargs);                                  if (!$r) {                                  handle_error('Cannot update employee '.$newemp['empid'], $err);                                  }                                  else {                construct_employees();                                  }                                  }                }              
  14. Edit the anyco.php file. Modify the delete_emp() function to handle errors. The function becomes:

    role delete_emp() {                                  if (!isset($_POST['emprec'])) { // User did not select a record                                  construct_employees();                                  }                                  else {                $empid = $_POST['emprec'];      $conn = db_connect($err);                                  if (!$conn) {                                  handle_error('Connection Error', $err);                                  }                                  else {                $statement = "DELETE FROM employees WHERE employee_id = :empid";       $bindargs = array();       array_push($bindargs, array('EMPID', $empid, -1));       $r = db_execute_statement($conn, $statement,                $err,                $bindargs);                                  if (!$r) {                                  handle_error("Error deleting employee $empid", $err);                                  }                                  else {                construct_employees();                                  }                                  }                                  }                }              
  15. Edit the anyco.php file. Modify the get_dept_name() office to handle errors. The function becomes:

    function get_dept_name($conn, $deptid) {   $query =     'SELECT department_name      FROM   departments      WHERE  department_id = :did';    $conn = db_connect($err);                                  if (!$conn) {                                  return ('Unknown');                                  }                                  else {                $bindargs = assortment();     array_push($bindargs, array('DID', $deptid, -one));     $dn = db_do_query($conn, $query, OCI_FETCHSTATEMENT_BY_COLUMN,                $err,                $bindargs);     if ($dn == imitation)       return ('Unknown');     else       return($dn['DEPARTMENT_NAME'][0]);                                  }                }              
  16. Edit the anyco_ui.inc file. Add a new function ui_print_error():

                    part ui_print_error($message, $posturl)                {                                  if (!$message) {                                  echo '<p>Unknown mistake</p>';                                  }                                  else {                                  repeat "<p>Error at line {$message['LINE']} of "                                  ."{$bulletin['FILE']}</p>";  // Uncomment for debugging                                  echo "<p>{$message['Bulletin']}</p>";                                  }                                  repeat <<<Cease                                  <form method="post" activeness="$posturl">                                  <input type="submit" value="Render to Departments" name="showdept">                Stop;                }              

    Remember not to put leading spaces in the END; line. Leading spaces in the END;line cause the residuum of the document to be treated as part of the text to be printed.

  17. Relieve the changes to your application files. Examination the changes by inbound the following URL in your browser:

    On Windows:

    http://localhost/chap5/anyco.php              

    On Linux:

    http://localhost/~<username>/chap5/anyco.php              

    The Departments folio is displayed:

    Description of chap5_err_handling_001.gif follows
    Description of the illustration chap5_err_handling_001.gif
  18. Click Next to navigate to the last section tape, the Accounting department with ID 110. Try to navigate by the last section record past clicking Side by side.

    Description of chap5_err_handling_002.gif follows
    Description of the illustration chap5_err_handling_002.gif

    The error treatment prevents navigation past the last section record.

  19. If a new employee is inserted with a bacon of 0, or the section ID is changed to one that does not be, the new error page is shown with the heading "Cannot insert employee".

Further Error Handling

Specific Oracle errors can be handled individually. For example, if a new employee record is created past clicking the Insert new employee button on the Employees folio, and the Department ID is changed to a department that does not exist, y'all tin trap this error and display a more meaningful bulletin:

  1. Edit the anyco.php file. Alter the error handling in the insert_new_emp() office:

                    $r = db_execute_statement($conn, $argument, $err, $bindargs);     if ($r) {       construct_employees();     }     else {                                  if ($err['CODE'] == 2291) {  // Foreign key violated                                  handle_error("Section {$newemp['deptid']} does non yet exist",                                  $err);                                  }                                                  else {                handle_error('Cannot insert employee', $err);                                  }                }              
  2. Relieve the changes to your application files. Exam the changes past inbound the post-obit URL:

    On Windows:

    http://localhost/chap5/anyco.php              

    On Linux:

    http://localhost/~<username>/chap5/anyco.php              
  3. In the Departments page, click Show Employees.

    Description of chap5_err_handling_003.gif follows
    Description of the analogy chap5_err_handling_003.gif
  4. In the Employees page, click Insert new employee.

    Description of chap5_err_handling_004.gif follows
    Description of the illustration chap5_err_handling_004.gif
  5. In the Insert New Employee folio, enter employee details equally shown, setting the Department ID to 99, and click Save.

    Description of chap5_err_handling_005.gif follows
    Description of the illustration chap5_err_handling_005.gif

    The following error page is displayed:

    Description of chap5_err_handling_006.gif follows
    Clarification of the illustration chap5_err_handling_006.gif

    You lot can click Render to Departments to return to the Departments page and and then click Show Employees to verify that the new employee record has not been added to the Administration department.

glydehadel1969.blogspot.com

Source: https://docs.oracle.com/cd/E11882_01/appdev.112/e10811/ch_five.htm

0 Response to "Array_push($errors"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel