MySQL
  Home arrow MySQL arrow Page 3 - The Perfect Job (part 2)
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
MYSQL

The Perfect Job (part 2)
By: icarus, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 2
    2001-07-11

    Table of Contents:
  • The Perfect Job (part 2)
  • Administrator Ahoy!
  • Adding To The Mix
  • Changing Things Around
  • Building Blocks
  • Handling The Gray Areas
  • Endgame

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    The Perfect Job (part 2) - Adding To The Mix


    (Page 3 of 7 )

    The script "add.php" display a form which allows an administrator to add a new job listing to the system. The script is divided into two sections - one section displays a form, while the other section processes the data entered into it. The $submit variable is used to decide which section of the script to execute.

    <? // form not yet submitted if (!$submit) { // open connection to database $connection = mysql_connect($hostname, $user, $pass) or die ("Unable to connect!"); ?> <table border="0" cellspacing="5" cellpadding="2"> <form action="<? echo $PHP_SELF; ?>" method="POST"> <!-- input job details --> <tr> <td>Job code<font color="red">*</font></td> </tr> <tr> <td><input type="text" name="jcode" size="10" maxlength="10"></td> </tr> <tr> <td>Designation<font color="red">*</font></td> <td width=30> </td> <td>Department<font color="red">*</font></td> </tr> <tr> <td><input type="text" name="dsg" size="25"></td> <td width=30> </td> <td><select name="dpt"> <? // get department list $query = "SELECT id, department from department"; $result = mysql_db_query($database, $query, $connection) or die ("Error in query: $query. " . mysql_error()); while (list($id, $department) = mysql_fetch_row($result)) { echo "<option value=$id>$department</option>"; } mysql_free_result($result); ?> </select></td> </tr> <tr> <td>Location<font color="red">*</font></td> <td width=30> </td> <td>Salary<font color="red">*</font></td> </tr> <tr> <td><select name="loc"> <? // get location list $query = "SELECT id, location from location"; $result = mysql_db_query($database, $query, $connection) or die ("Error in query: $query. " . mysql_error()); while (list($id, $location) = mysql_fetch_row($result)) { echo "<option value=$id>$location</option>"; } mysql_free_result($result); ?> </select></td> <td width=30> </td> <td><select name="sal"> <? // get salary list $query = "SELECT id, salary from salary"; $result = mysql_db_query($database, $query, $connection) or die ("Error in query: $query. " . mysql_error()); while (list($id, $salary) = mysql_fetch_row($result)) { echo "<option value=$id>$salary</option>"; } mysql_free_result($result); mysql_close($connection); ?> </select></td> </tr> <tr> <td>Responsibilities<font color="red">*</font></td> <td width=30> </td> <td>Qualifications<font color="red">*</font></td> </tr> <tr> <td><textarea name="rsp" cols="40" rows="8"></textarea></td> <td width=30> </td> <td><textarea name="qlf" cols="40" rows="8"></textarea></td> </tr> <tr> <td>Contact person<font color="red">*</font></td> <td width=30> </td> <td>Email address<font color="red">*</font></td> </tr> <tr> <td><input type="text" name="cname" size="25"></td> <td width=30> </td> <td><input type="text" name="cmail" size="25"></td> </tr> <tr> <td align=center colspan=3><input type=submit name=submit value="Add Listing"></td> </tr> </table> </form> <? } else { // form submitted, process it } ?>
    Here's what it looks like.



    Nothing too complicated here - this is simply a form, with text fields for some elements of the job listing, and drop-down boxes for the remainder. You will notice that the items in the drop-downs are generated from the database; you probably remember this from last time.

    Notice also the ACTION attribute of the <FORM> tag, which is pointing to a PHP variable called $PHP_SELF. This is a built-in PHP variable which always holds the name of the currently-executing script; by including it here, I am ensuring that the same script is also called to process the form.

    Once the form is submitted, the same script is called again; however, since the $submit variable will now exist, the second half of the script springs into action.

    <? <? // form not yet submitted if (!$submit) { // generate form } else { // form submitted, process it // set up error list array $errorList = array(); $count = 0; // validate text input fields if (empty($jcode)) { $errorList[$count] = "Invalid entry: Job code"; $count++; } if (empty($dsg)) { $errorList[$count] = "Invalid entry: Designation"; $count++; } if (empty($rsp)) { $errorList[$count] = "Invalid entry: Responsibilities"; $count++; } if (empty($qlf)) { $errorList[$count] = "Invalid entry: Qualifications"; $count++; } if (empty($cname)) { $errorList[$count] = "Invalid entry: Contact name"; $count++; } if (empty($cmail) || isEmailInvalid($cmail)) { $errorList[$count] = "Invalid entry: Email address"; $count++; } if (sizeof($errorList) == 0) { // open connection to database $connection = mysql_connect($hostname, $user, $pass) or die ("Unable to connect!"); // insert data $query = "INSERT INTO listing (jcode, designation, responsibilities, qualifications, cname, cmail, posted, fk_department, fk_location, fk_salary) VALUES ('$jcode', '$dsg', '$rsp', '$qlf', '$cname', '$cmail', NOW(), '$dpt', '$loc', '$sal')"; $result = mysql_db_query($database, $query, $connection) or die ("Error in query: $query. " . mysql_error()); // clean up mysql_close($connection); echo "Entry successfully added.<p><a href=$PHP_SELF>Add another entry</a>, or <a href=job_list.php>return to job listings</a>"; } else { listErrors(); } } ?>
    The error-checking mechanism used here should be familiar to you from the job application form in the previous article...although the validation routines here are a little simpler, since this form is far less complex.

    Once the data has been validated and found to be acceptable, an INSERT query takes care of saving the data to the "listing" table, so that it immediately appears on the job listing page.

    This article copyright Melonfire 2001. All rights reserved.

    More MySQL Articles
    More By icarus, (c) Melonfire


     

       

    MYSQL ARTICLES

    - MySQL Server Tuning Tips and Tricks
    - MySQL Query Optimizations and Schema Design
    - MySQL Benchmarking Tools and Utilities
    - MySQL Benchmarking Concepts and Strategies
    - Take Some Load off MySQL with MemCached
    - MySQL Table Prefix Changer Tool in PHP
    - Using the SIGNAL Statement for Error Handling
    - Error Handling Examples
    - Error Handling
    - Completing a Search Engine with MySQL and PH...
    - Paginating Result Sets for a Search Engine B...
    - Building a Search Engine with MySQL and PHP 5
    - Using Boolean Operators for Full Text and Bo...
    - PHP, MySQL and the PEAR Database
    - Working with PHP and MySQL





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
    Stay green...Green IT