DanielHardy Posted February 24, 2009 Share Posted February 24, 2009 Hi I am trying to apply some simple validation rules to a form. Here is the code of the "register" file that adds details to a mysql database: <? // Connect to DB server $connection = mysql_connect("localhost", "0604274", "ah3482"); if (!$connection) die("Cannot connect to DB"); // Select database mysql_select_db("db0604274", $connection) or die("Can not find DB"); // Retrieve values from form $username = $_POST['username']; $password = $_POST['password']; $name = $_POST['name']; $add1 = $_POST['add1']; $add2 = $_POST['add2']; $town = $_POST['town']; $county = $_POST['county']; $pcode = $_POST['pcode']; $email = $_POST['email']; $dob = $_POST['dob']; // Validate user input include "email.class"; include "validation.class"; $class1 = &New Class1; $class1->Email = $_POST['email']; $check_email = $class1->check_email(); if(!$check_email){ echo 'The email address is not valid! <p>Please <a href="form.html">Try Again</a></p>'; } $class2 = &New Class2; $class2->Check = $_POST['username']; $check_username = $class2->check_username(); if(!$check_username){ echo 'Please enter a username <p>Please <a href="form.html">Try Again</a></p>'; } else { // Build SQL statement $sql = "INSERT INTO users (username, password, name, add1, add2, town, county, pcode, email, dob) VALUES ('$username', '$password', '$name','$add1','$add2','$town','$county','$pcode', '$email', '$dob')"; // Run SQL statement mysql_query($sql, $connection); print('Succesfully Registered. <p>Please <a href="form2.html">Login</a></p>'); } ?> The email.class itself is working, as is the corresponding code. In that users will not get added to the database unless the email is in the correct format. For anyone interested here is the email.class file: <?php class class1 { var $Email; function check_email(){ if(ereg("^.+@.+\..+$", $this->Email)) return (true); else return (false); } } ?> Now, the problem. I am trying to create various classes to validate the other fields (username) etc. They simply have to have values in them for the time being. The class I have came up with(validation.class) is: <?php class class2 { var $Check; function check_username(){ if(ereg("", $this->Check)) return (false); else return (true); } } ?> I get various errors no matter what I mess around with. I think the validation.class is not set up right, i.e the ereg part. Any help greatly appreciated. Thanks in advance Dan Quote Link to comment https://forums.phpfreaks.com/topic/146690-solved-using-classes-within-if-statement/ Share on other sites More sharing options...
JonnoTheDev Posted February 24, 2009 Share Posted February 24, 2009 What I want to know is why you would create an individual class to validate one piece of information. You are creating a single function wrapper. Bad programming. These would be better off in a simple functions file: function check_email() function check_username() etc Makes no sense to instantiate an object to check an email address. Quote Link to comment https://forums.phpfreaks.com/topic/146690-solved-using-classes-within-if-statement/#findComment-770181 Share on other sites More sharing options...
DanielHardy Posted February 24, 2009 Author Share Posted February 24, 2009 Without sounding rude I knew I would get this type of response. I know exactly how to do it the way you have described. I am experimenting with using classes and the OO approach. Perhaps I should of made this clear. Thanks all the same Quote Link to comment https://forums.phpfreaks.com/topic/146690-solved-using-classes-within-if-statement/#findComment-770295 Share on other sites More sharing options...
premiso Posted February 24, 2009 Share Posted February 24, 2009 Without sounding rude I knew I would get this type of response. I know exactly how to do it the way you have described. I am experimenting with using classes and the OO approach. Perhaps I should of made this clear. Thanks all the same Honestly, about 80% are thrown off from this as you did not use the tags to surround your code. It makes it tedious and hard to read code without them. As far as why that ereg is not working, A ereg should not be used it is being depreciated. B you are just checking if the string is empty? Why not use empty. C, that ereg always returns true cause it has nothing to check for, thus it is true. Hope that makes sense. Quote Link to comment https://forums.phpfreaks.com/topic/146690-solved-using-classes-within-if-statement/#findComment-770301 Share on other sites More sharing options...
DanielHardy Posted February 24, 2009 Author Share Posted February 24, 2009 Yes working perfectly thankyou. I'll bare your advice in mind for future posts. Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/146690-solved-using-classes-within-if-statement/#findComment-770346 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.