Jump to content

Recommended Posts

New to PHP, I'm sure I'll be a pest here. ;) I have a pretty solid programming and database background, just new to thses languages. I have very little, seldom-used server side processing experience (JavaScript, PERL, ASP) and it's been many years since I practiced what little I did.

 

I'm working through some tutorials to get to know the syntax and methods. I've noticed that getting a value prepared for insert into a table can be cumbersome and I'm hoping to create a generic process that will allow me to loop through all the variables that I've used to collect form input. I've tried to do several things, none of which seem to work completley. Here's some sample code to represent what I'm trying to do.

 

The str_replace statement will eventually be worked into a function that I'm going to create to process and cleanse data before proceeding to the insert stage. (The actuall code below was entered by hand, not psted from a tested script. Treat it more as psuedo-code if there's minor syntax issues.)

 

<?php

...

//example 1

 

$var1 = $_POST('fld1');

$var2 = $_POST('fld2');

$var3 = $_POST('fld3');

 

foreach ($_POST as $i) {

  if (strpos ($i, " ' ") > 0) {

      $i = str_replace(" ' "," \' ",$i);

      $_POST[$i] = $i;

  }

}

 

$q = "INSERT INTO tbl1 (field1, field2, field3) VALUES ('$var1', '$var2', '$var3')";

 

//This actually works as far as replacing the values in $_POST. BUt of course, it didn;t change the values

// in var1, var2, var3. (Obvious once I saw what was happening, justone of them newb discoveries!) So I

// did manage to figure out how to access $_POST, but it didn't affect the data being inserted.

 

 

// ###### Example 2

$var[1] = $_POST('fld1');

$var[2] = $_POST('fld2');

$var[3] = $_POST('fld3');

 

foreach ($var as $i) {

  if (strpos ($var[$i], "'") > 0) {

      $var[$i] = str_replace("'","\'",$var[$i]);

  }

}

 

$q = "INSERT INTO tbl1 (field1, field2, field3) VALUES ('$var[1]', '$var[2]', '$var[3]')";

// Although very undesirable coding, this worked (the values that needed processing got processed) - right up to the insert statement where it didn't like me using an array to submit the data. 

//also tried:

//$q = "INSERT INTO tbl1 (field1, field2, field3) VALUES ($var[1], $var[2], $var[3])";

 

//by association I'm guessing the this wouldn' work either (assuming I did the processing as in example 1:

//$q = "INSERT INTO tbl1 (field1, field2, field3) VALUES ('$_POST[1]', '$_POST[2]', '$_POST[3]')";

 

?>

 

I just have the feeling that I'm trying to invent the wheel here; there has got to be somebody smarter than me who has developed a way to loop through the variables and run them through a cleansing/validation process without having to address them staticly by name.

 

Am I on the wrong track here - either in my attempts to create it or believing it already exists?

Link to comment
https://forums.phpfreaks.com/topic/171964-phpmysql-looping-through-variables/
Share on other sites

 

Thanks!

Is this a tool already in PHP5 or something I need to download and install?

 

This is written in PHP5 but does not come with the default php installation. So you need to download the package and add it to your script and afterwards call it more information can be found in the provided manual.

I guess I jumped the gun on the issue being solved.

 

My issue isn't really with a validation script. Not that I won't look the product over (I think I actually have it already via the XAMPP install), but I can write a validation script.

 

I'm looking for a looping mechanism so I don't have to issue each variable, one at a time, through a process.

I guess I jumped the gun on the issue being solved.

 

My issue isn't really with a validation script. Not that I won't look the product over (I think I actually have it already via the XAMPP install), but I can write a validation script.

 

I'm looking for a looping mechanism so I don't have to issue each variable, one at a time, through a process.

 

Zend_Form: I generally use it like this create my form for some page and add custom element's like Username, EmailAddress, etc..

 

class App_Form_Login extends Zend_Form {
    const ELEMENT_USERNAME = 'username';
    const ELEMENT_PASSWORD = 'password';
    const ELEMENT_EMAIL_ADDRESS = 'email_address';
    
    public function init() {
        $e = $this->createElement('Username', self::ELEMENT_USERNAME);
        $this->addElement($e);

        $e = $this->createElement('Password', self::ELEMENT_PASSWORD);
        $this->addElement($e);

        $e = $this->createElement('Password', self::ELEMENT_PASSWORD_CONFIRM);
        $this->addElement($e);

        $e = $this->createElement('EmailAddress', self::ELEMENT_EMAIL_ADDRESS);
        $this->addElement($e);

        $e = $this->createElement('Submit', 'submit');
        $this->addElement($e);
    }
    
    public function isValid($data) {
        $isValid = false;
        if ($data[self::ELEMENT_PASSWORD] !== $data[self::ELEMENT_PASSWORD_CONFIRM]) {
            $this->getElement(self::ELEMENT_PASSWORD_CONFIRM)->addError('Password\'s do not match.');
        }
        return parent::isValid($data) && $isValid;
    }
}

 

These custom elements are usefull because you generally use them at multiple sections in your application, for example: the element Username must have the same validation rules for both the login page as the registration page. You can't let a user register at the registration page and allow him up to 8 or more characters and only allow 6 at the login page.

 

class App_Form_Element_Username extends Zend_Form_Element {
    public function init() {
        //label, validation rules, ..
    }
}

 

In your application you use 'em like:

 

$form = new App_Form_Login();
if (!empty($_POST) && $form->isValid($_POST)) {
    //..
}

 

Or if you use the MVC equivalent:

 

$form = new App_Form_Login();
if ($this->getRequest()->isPost() && $form->isValid($this->getRequest()->getPost())) {
    //..
}

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.