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 assistance.

diafol commented: I look forward to you supplying any information -1

Recommended Answers

All 3 Replies

Please let me know how the table is set up so that I can help out with the code.

Is the list in different rows in a table?
Is the list in the same record?
Are the elements in comma separated values?

Such questions need to be answered before I can give you an answer to your question.

Thanks so much.
These are the details:

There are 2 separate databases

  1. The database for the predefined codes. This database contains one table with only the code with the serial ID.
  2. The profile database into which the validated details will be submitted.

Value in the code field will be validated against the first database before being submitted into the second.

The submission process into the second database is working fine.

The expression i used for validation without success is as follows:
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

// Database connection details.
$username = "dinet_admin01";
$password = "me11##";
$database = "dinet_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);

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

I hope this is what you asked for.
Cheers

Ayo

Everything looks ok....

First a few observations

  1. I believe it is easier to use tables in the same db rather than using 2 different databases unless you have a specific reason to use 2 databases. It saves the hassle of trying to connect to both at the same time.
  2. I normally dont use the db_query, prefer it to mysql_query while working with a MySQL DB.
  3. I think you might have a syntax error with the db_numrows. Should'nt it be db_num_rows? It is mysql_num_rows for a mySQL DB. Check this.
  4. Try putting the validation list into a <select> element so the users can only select the code from the list that you provide and no other.

If you still have a problem try the following.

Instead of: $selectQuery = db_query("SELECT Scode FROM CodeList WHERE Scode='$formScratchCode'"); use

$selectQuery = "SELECT Scode FROM CodeList WHERE Scode='$formScratchCode'";
echo $selectQuery;
$selectResult=db_query($selectQuery);

This should print out your sql statement on the page. Copy it into your DB management system (I hope your using phpMyAdmin). And check if the result is showing up correctly. If it is correct then the only other statement that could be wrong is the numrows. Else check if the variable you are getting through the post is correct.

If you still have a problem please let me know.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.