venturemc Posted August 26, 2009 Share Posted August 26, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/171964-phpmysql-looping-through-variables/ Share on other sites More sharing options...
alexdemers Posted August 26, 2009 Share Posted August 26, 2009 $_POST('fld1') should be in brackets: $_POST['fld1'] Quote Link to comment https://forums.phpfreaks.com/topic/171964-phpmysql-looping-through-variables/#findComment-906725 Share on other sites More sharing options...
ignace Posted August 26, 2009 Share Posted August 26, 2009 Am I on the wrong track here - either in my attempts to create it or believing it already exists? Neither. Quote Link to comment https://forums.phpfreaks.com/topic/171964-phpmysql-looping-through-variables/#findComment-906729 Share on other sites More sharing options...
venturemc Posted August 26, 2009 Author Share Posted August 26, 2009 Ok, thanks. That was onone of those minor syntax issues I was refering to in the beginning. That's not my question.... Quote Link to comment https://forums.phpfreaks.com/topic/171964-phpmysql-looping-through-variables/#findComment-906730 Share on other sites More sharing options...
venturemc Posted August 26, 2009 Author Share Posted August 26, 2009 Am I on the wrong track here - either in my attempts to create it or believing it already exists? Neither. OK. Clue? Quote Link to comment https://forums.phpfreaks.com/topic/171964-phpmysql-looping-through-variables/#findComment-906733 Share on other sites More sharing options...
ignace Posted August 26, 2009 Share Posted August 26, 2009 http://framework.zend.com/manual/en/zend.form.html and http://framework.zend.com/manual/en/zend.validate.html Quote Link to comment https://forums.phpfreaks.com/topic/171964-phpmysql-looping-through-variables/#findComment-906751 Share on other sites More sharing options...
venturemc Posted August 26, 2009 Author Share Posted August 26, 2009 http://framework.zend.com/manual/en/zend.form.html and http://framework.zend.com/manual/en/zend.validate.html Thanks! Is this a tool already in PHP5 or something I need to download and install? Quote Link to comment https://forums.phpfreaks.com/topic/171964-phpmysql-looping-through-variables/#findComment-906761 Share on other sites More sharing options...
ignace Posted August 26, 2009 Share Posted August 26, 2009 http://framework.zend.com/manual/en/zend.form.html and http://framework.zend.com/manual/en/zend.validate.html 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. Quote Link to comment https://forums.phpfreaks.com/topic/171964-phpmysql-looping-through-variables/#findComment-906764 Share on other sites More sharing options...
venturemc Posted August 26, 2009 Author Share Posted August 26, 2009 Thanks Much! Quote Link to comment https://forums.phpfreaks.com/topic/171964-phpmysql-looping-through-variables/#findComment-906778 Share on other sites More sharing options...
venturemc Posted August 26, 2009 Author Share Posted August 26, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/171964-phpmysql-looping-through-variables/#findComment-907128 Share on other sites More sharing options...
ignace Posted August 27, 2009 Share Posted August 27, 2009 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())) { //.. } Quote Link to comment https://forums.phpfreaks.com/topic/171964-phpmysql-looping-through-variables/#findComment-907351 Share on other sites More sharing options...
venturemc Posted August 27, 2009 Author Share Posted August 27, 2009 THanks for the info. PHP is a very big world isn't it? Quote Link to comment https://forums.phpfreaks.com/topic/171964-phpmysql-looping-through-variables/#findComment-907521 Share on other sites More sharing options...
ignace Posted August 27, 2009 Share Posted August 27, 2009 PHP is a very big world isn't it? You have no idea Quote Link to comment https://forums.phpfreaks.com/topic/171964-phpmysql-looping-through-variables/#findComment-907577 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.