validate a field value against a list predefined in mysql database

Discussion in 'PHP' started by aydot22, Oct 12, 2010.

  1. #1
    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
     
    aydot22, Oct 12, 2010 IP
  2. lukeg32

    lukeg32 Peon

    Messages:
    645
    Likes Received:
    19
    Best Answers:
    1
    Trophy Points:
    0
    #2
    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.
     
    lukeg32, Oct 13, 2010 IP
  3. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #3
    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.
     
    mastermunj, Oct 13, 2010 IP
  4. aydot22

    aydot22 Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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.
     
    aydot22, Oct 13, 2010 IP
  5. JoelLarson

    JoelLarson Peon

    Messages:
    61
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    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.
     
    JoelLarson, Oct 13, 2010 IP