Jump to content

Making a piece of code more generic


jeff5656

Recommended Posts

Whenever I create a new form where a user adds the record to the database, I use the code below to add the fields into the database.  I have many different tables and I use the same format but only the names of the variables change.

 

 

Is there any way to make this code generic so I can just use it for all the forms to add the variables?

$rm_loc_pre = $_POST['rm_loc'];
$rm_loc = ereg_replace("[^A-Za-z0-9]", "", $rm_loc_pre);
$patient = mysql_real_escape_string($_POST['patient']);
$mrn = mysql_real_escape_string($_POST['mrn']);
$age = mysql_real_escape_string($_POST['age']);
$race = mysql_real_escape_string($_POST['race']);
$gender = $_POST['gender'];
$service = $_POST['service'];
$hospitalist = mysql_real_escape_string($_POST['hospitalist']);
$rcf_date = $_POST['rcf_date'];
$rcf_date2 = date("Y-m-d", strtotime($_POST['rcf_date']));
$dx = mysql_real_escape_string($_POST['dx']);
$pmhx = mysql_real_escape_string($_POST['pmhx']);
$problist = mysql_real_escape_string($_POST['problist']);
$comments = mysql_real_escape_string($_POST['comments']);
$comments_date = date("Y-m-d H:i:s");
$code = $_POST['code'];
$allergy = $_POST['allergy'];


$query = "INSERT INTO hosp (id_incr, rm_loc, service, hospitalist, patient, mrn, age, race, gender, rcf_date, rcf_date2, dx, pmhx, problist, comments, comments_date, code, allergy, signoff_status)

VALUES('$id_incr','$rm_loc', '$service', '$hospitalist','$patient', '$mrn', '$age', '$race', '$gender', '$rcf_date', '$rcf_date2', '$dx', '$pmhx', '$problist', '$comments', '$comments_date','$code', '$allergy','a')";

mysql_query($query) or die(mysql_error());
header("Location: displayactive.php");

 

So the variable names change and the number of variables change, and the name of the table changes, *but* I always add all the POSTed variables.

Link to comment
Share on other sites

You could create a function that accepts an array of all form field names and returns an array with all form values which can then be entered into the database.

 

You need to add mysql_real_escape_string to all your values though in order to make the query safe. You have left out several field values.

Link to comment
Share on other sites

You could create a function that accepts an array of all form field names and returns an array with all form values which can then be entered into the database.

 

You need to add mysql_real_escape_string to all your values though in order to make the query safe. You have left out several field values.

 

Hmm I am not sure if I could figure out how to do that - my php skill is very very newbie-level.  Can you give me an example of the code that uses an array and function so I can try to work with it? thanks.

Link to comment
Share on other sites

You could create a function that accepts an array of all form field names and returns an array with all form values which can then be entered into the database.

 

You need to add mysql_real_escape_string to all your values though in order to make the query safe. You have left out several field values.

 

Hmm I am not sure if I could figure out how to do that - my php skill is very very newbie-level.  Can you give me an example of the code that uses an array and function so I can try to work with it? thanks.

 

I wouldn't recommend using the described method. You are generalising your script to any given input. You would perform an mysql_real_escape_string() on an e-mail address field without really knowing that it actually is an e-mail address.

Link to comment
Share on other sites

I wouldn't recommend using the described method. You are generalising your script to any given input.

 

So I guess I have to keep typing in every single variable?  I thought I could do an array or soemthing (don't know much about arrays) so I don't have to do it like my long clunky code.

How could I do it if we ignore the date fields for now?  Maybe I could mae MOST of it generic nd do the dates separately. 

Link to comment
Share on other sites

I wouldn't recommend using the described method. You are generalising your script to any given input.

 

So I guess I have to keep typing in every single variable?  I thought I could do an array or soemthing (don't know much about arrays) so I don't have to do it like my long clunky code.

How could I do it if we ignore the date fields for now?  Maybe I could mae MOST of it generic nd do the dates separately. 

 

Or you could use a form framework (like http://framework.zend.com/manual/en/zend.form.html). This allows you to easily create and validate form input. If you extend it you can add a model (like an ActiveRecord) to let it - if valid - add it automagically to the database (http://www.zendcasts.com/may-to-many-with-zend_db-and-zend_form/2009/05/). Alternatively you can look at patForms (http://trac.php-tools.net/patForms).

 

Quick intro in using Zend Form:

class MyLoginForm extends Zend_Form {
    const ELEMENT_USERNAME = 'username';
    const ELEMENT_PASSWORD = 'password';
    const ELEMENT_SUBMIT = 'login';
    public function init() {
        $e = $this->createElement('Text', self::ELEMENT_USERNAME);
        $this->addElement($e);
        $e = $this->createElement('Text', self::ELEMENT_PASSWORD);
        $this->addElement($e);
        $e = $this->createElement('Submit', self::ELEMENT_SUBMIT);
        $this->addElement($e);
    }
}

 

A username and a password ofcourse is used more then once in your application. You probably also have some rules for them (username must be 8 characters long, password must be 8 characters long and have upper- and lowercase letters ..).

 

class My_Form_Element_Username extends Zend_Form_Element_Text {
    public function init() {
        $this->addValidator('Alpha');
        $this->addValidator('StringLength', false, array(8, 16));
        $this->setLabel('Username');
    }
}

class My_Form_Element_Password extends Zend_Form_Element_Password {
    public function init() {
        $this->addValidator('StringLength', false, array(8, 16));
        $this->addValidator('Regex', false, array('/..some..regex../'));
    }
}

 

Then if you reference them you can use them within your form:

class MyLoginForm extends Zend_Form {
    const ELEMENT_USERNAME = 'username';
    const ELEMENT_PASSWORD = 'password';
    const ELEMENT_SUBMIT = 'login';
    public function init() {
        $this->addPrefixPath('My_Form_Element', 'My/Form/Element', 'element');
        $e = $this->createElement('Username', self::ELEMENT_USERNAME);
        $this->addElement($e);
        $e = $this->createElement('Password', self::ELEMENT_PASSWORD);
        $this->addElement($e);
        $e = $this->createElement('Submit', self::ELEMENT_SUBMIT);
        $this->addElement($e);
    }
}

Link to comment
Share on other sites

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.