Hello everyone, I need a PHP expression to validate a field value against a list predefined in mysql database So that anything different from that in the database will not be submitted to the database. Looking forward to your help
It shouldnt be too hard to do at all; Display form On submit; --> Fetch allowed values --> If submitted value is not in predefined list, throw error --> else accept value Something like; <?php # Get your allowed list from the database $allowed = array("yes", "no", "maybe"); # Your value is from the form $varToCheck = "yes"; if(valueIsAllowed($allowed, $varToCheck) === FALSE) print "This value is not allowed\n"; else print "This value is allowed\n"; function valueIsAllowed($allowed, $check) { if(!in_array($check, $allowed)) return FALSE; return TRUE; } ?> PHP: You can also do this using ajax with prototype/jquery.
can you share examples of such validation? based on that we can get more idea of exactly what you are talking about and can give you suggestions.
Hello Guys, Good to know someone cares, I have a Dolphin 7.2 software that i am customizing There is a box where am allowed to place my php expressions to perform this validation action. This is what i did but was unsuccessful: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx // Database connection details. $username = "cardinet_admin01"; $password = "code###"; $database = "cardinet_codelist"; // Connect to the database. $conn = @mysql_connect('localhost', $username, $password) or die("Unable to connect to MySQL"); @mysql_select_db($database, $conn) or die( "Unable to select database"); //field value name is ScratchCode //Predefined list name in MYSQL is Scode $formScratchCode=$_POST['ScratchCode']; $selectQuery = db_query("SELECT Scode FROM CodeList WHERE Scode='$formScratchCode'"); // see if there are any matches if (db_numrows($selectQuery) == 1) // if a match occurs, then allow form submission return true; { else { echo 'Your scratch code is not valid. Please insert a valid code.'; } mysql_close($conn); } xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx Please I am a baby in php and willing to learn Cheers.
You could use the ENUM type in MySQL. Then you can set: enum('option1', 'option2', 'option3') When you submit one of the options, it uses that option. When you submit an option that's not one of those, it submits NULL. Otherwise go with lukeg32's approach and fetch the data then loop or use a function to check if the data is valid.