Author Topic: Best way to structure query based on form  (Read 373 times)

0 Members and 1 Guest are viewing this topic.

Offline aleX_hillTopic starter

  • Enthusiast
  • Posts: 139
    • View Profile
Best way to structure query based on form
« on: March 11, 2010, 08:59:27 PM »
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:
Code: [Select]
<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?
« Last Edit: March 11, 2010, 09:01:04 PM by aleX_hill »

Offline nafetski

  • Enthusiast
  • Posts: 270
    • View Profile
Re: Best way to structure query based on form
« Reply #1 on: March 11, 2010, 09:28:49 PM »
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 :)


Dev Environment: Mac - OSX Snow Leopard / Eclipse / Kohana PHP Framework
Job: Sr Software Developer: (Large scale enterprise)
Notice:  Most of my forum posts I write on my iPhone while taking a dump.  This means that I don't test most of my code, and I might sound like I'm impatient...really I'm just busy punching a grumpy
Also: I've hit Google so many times it's asking for a divorce

Offline aleX_hillTopic starter

  • Enthusiast
  • Posts: 139
    • View Profile
Re: Best way to structure query based on form
« Reply #2 on: March 11, 2010, 09:35:34 PM »
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.

Offline ababmxking

  • Irregular
  • Posts: 49
    • View Profile
Re: Best way to structure query based on form
« Reply #3 on: March 11, 2010, 10:17:50 PM »
Code: [Select]
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.

Offline aleX_hillTopic starter

  • Enthusiast
  • Posts: 139
    • View Profile
Re: Best way to structure query based on form
« Reply #4 on: March 11, 2010, 10:22:42 PM »
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

Offline ababmxking

  • Irregular
  • Posts: 49
    • View Profile
Re: Best way to structure query based on form
« Reply #5 on: March 11, 2010, 10:49:37 PM »
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