Jump to content

Best way to structure query based on form


aleX_hill

Recommended Posts

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?

Link to comment
Share on other sites

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 :)

 

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.