pioneerx01 Posted January 7, 2011 Share Posted January 7, 2011 Ok, I am not sure if I can explain it correctly, but here it goes. I have a form where user is asked to enter up to three name. Let's say that the fields are called [name1], [name2] and [name3] and they are called the came in the table. Now I have a code which allows me to compare what names are in the database versus the name that was entered by the user. $query = "SELECT * FROM Project_Registrations WHERE name1= '$_POST[name1]' "; $result2 = mysql_query($query); if (mysql_numrows($result2) > 0) { while($row = mysql_fetch_array($result2)) {echo" ...error code... ";} } else { ...ok code... }; This works well for one name field but I have three in the form and three in the table. How do I define it so all three name fields in the form are compared to all three in the table and if just on matches I can give them the ...error code...? Oh, and they can enter up to three names in the form, but it could be two or one. Can I code it such: $query = "SELECT * FROM Project_Registrations WHERE name1= '$_POST[name1]' OR name1= '$_POST[name2] OR name1= '$_POST[name2]' OR ...'"; Thanks Link to comment https://forums.phpfreaks.com/topic/223653-custom-select-from-where-statement/ Share on other sites More sharing options...
Zurev Posted January 7, 2011 Share Posted January 7, 2011 Use the IN clause in my opinion. $QUERY = "SELECT * FROM Project_Registrations WHERE name1 IN('value1', 'value2', 'value3')"; More on that: http://www.w3schools.com/sql/sql_in.asp Link to comment https://forums.phpfreaks.com/topic/223653-custom-select-from-where-statement/#findComment-1156110 Share on other sites More sharing options...
pioneerx01 Posted January 7, 2011 Author Share Posted January 7, 2011 So in the entire array I can do this?: $QUERY = "SELECT * FROM Project_Registrations WHERE name1 IN('value1', 'value2', 'value3') OR name2 IN('value1', 'value2', 'value3') OR name3 IN('value1', 'value2', 'value3')"; Link to comment https://forums.phpfreaks.com/topic/223653-custom-select-from-where-statement/#findComment-1156113 Share on other sites More sharing options...
pioneerx01 Posted January 7, 2011 Author Share Posted January 7, 2011 Ok, I got it working. Thanks Link to comment https://forums.phpfreaks.com/topic/223653-custom-select-from-where-statement/#findComment-1156133 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.