Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

PHP - check $_POST against 4 separate fields in MySQL database table

New Here ,
Dec 13, 2009 Dec 13, 2009

What I have is a user input field that I want to check against 4 separate fields in the database.  I know the code for errors on the one field by making it unique, but I need to check all 4 fields and not allow it be sent the database if the value in the user field matches any of the 4 fields in the database.  I hope that makes sense...can anyone help me with this?  FYI - they are picture fields so I don't want someone to have matching names of pictures.

TOPICS
Server side applications
336
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 13, 2009 Dec 13, 2009

It sounds as though your database structure is probably at fault, but if you want to check a single value in four fields, create a recordset like this:

SELECT id FROM mytable

WHERE field1 = myval OR field2 = myval

OR field3 = myval OR field4 = myval

In the Variables section of the Advanced Recordset dialog box, set Name to myval, Type to text, and Runtime Value to $_POST['input_name'] (obviously, change the various names to match your setup).

Let's say you call the recordset "check", you can then use $totalRows_check to see if there are any matches. If there aren't, let the insert go ahead.

if ($totalRows_check == 0) {

  // the insert record server behavior code goes here

} else {

  // redisplay the form with an error message

}

Dreamweaver always puts the code for recordsets immediately above the DOCTYPE declaration, so you will need to move the code around yourself to check whether there are exisiting records before allowing the insert to go ahead.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 13, 2009 Dec 13, 2009

Thanks, I didn't think to do it that way. It works perfect!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 14, 2009 Dec 14, 2009
LATEST

Marking this thread as assumed answered.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines