aleX_hill Posted March 12, 2010 Share Posted March 12, 2010 I have a mailing list which I am developing which allows users to sign up to any (or multiple) of three lists. The send email form looks like this: <h2>SEND EMAIL TO LIST</h2> <form action="/mailingList/submitMail/" method="post"> Which Lists:<br /> <input type="checkbox" id="list1" name="list1" value="1" /> List 1<br /> <input type="checkbox" id="list2" name="list2" value="1" /> List 2<br /> <input type="checkbox" id="list3" name="list3" value="1" /> List 3<br /> Enter Email:<br /> <textarea name="email" cols="50" rows="5"></textarea> <button type="submit"> Send Email </button> </form> Now My database is a very simple structure with the following fields: id - int Name - varchar Email - varchar list1 - bool list2 - bool list3 - bool What I am stuck on is the best way to develop my SQL query based on the $_POST data. I dont want to use a whole bunch of if/switch statements and write a different query for each, and I am sure there is an easier way to do it then that. Any suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/194970-best-way-to-structure-query-based-on-form/ Share on other sites More sharing options...
nafetski Posted March 12, 2010 Share Posted March 12, 2010 Few ways to make your life easier. For the checkbox, you can have all of them save into one array...ex <input type="checkbox" name=list[1] value="true" /> <input type="checkbox" name=list[2] value="true" /> <input type="checkbox" name=list[3] value="true" /> Then loop through that array, set your values to what you want (if it wasn't clicked or true, then set it as false) - and then run your db query Quote Link to comment https://forums.phpfreaks.com/topic/194970-best-way-to-structure-query-based-on-form/#findComment-1025034 Share on other sites More sharing options...
aleX_hill Posted March 12, 2010 Author Share Posted March 12, 2010 The problem that I have is structuring the query. Would it be (based on my original code, will make your suggested change when I get time) $query = "SELECT * FROM mailingList WHERE list1='$_POST[list1]' OR list2='$_POST[list2]' OR list3='$_POST[list3]' This didnt seem to work for me. Quote Link to comment https://forums.phpfreaks.com/topic/194970-best-way-to-structure-query-based-on-form/#findComment-1025036 Share on other sites More sharing options...
ababmxking Posted March 12, 2010 Share Posted March 12, 2010 mysql_query("INSER INTO tableNameHere (name, email, list1, list2, list3) VALUES ('$name', '$email', '$list[1]', '$list[2]', '$list[3]') "); idk if that was what you're looking for or not. Quote Link to comment https://forums.phpfreaks.com/topic/194970-best-way-to-structure-query-based-on-form/#findComment-1025045 Share on other sites More sharing options...
aleX_hill Posted March 12, 2010 Author Share Posted March 12, 2010 Nope. I want to select the users from the table which have list1='1' if the list1 checkbox was checked, or list2='1' if the second was checked. So if the user selects list1 and list3, and user who has list1='1' OR list3='1' will be selected (ie this email is relevant to people who are interested in the stuff list 1 or list 3 were set up for. If they ONLY subscribe to list 2, ignore them) I have done the insert bit somewhere else Quote Link to comment https://forums.phpfreaks.com/topic/194970-best-way-to-structure-query-based-on-form/#findComment-1025046 Share on other sites More sharing options...
ababmxking Posted March 12, 2010 Share Posted March 12, 2010 well only way i can think of doing it is, having 3 different query's. or setting up a loop with arrays., idk much about arrays though Quote Link to comment https://forums.phpfreaks.com/topic/194970-best-way-to-structure-query-based-on-form/#findComment-1025054 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.