Skip to main content
Known Participant
December 13, 2009
Question

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

  • December 13, 2009
  • 2 replies
  • 335 views

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.

This topic has been closed for replies.

2 replies

David_Powers
Inspiring
December 14, 2009

Marking this thread as assumed answered.

David_Powers
Inspiring
December 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.

Rick4209Author
Known Participant
December 13, 2009

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