Jump to content

Recommended Posts

Database Table: contacts

 

id | name |        email          |    c1    |  c2      |    c3    |    notify

1 |  Suzie | sue@email.com  |  a girl  |            |            |    Y

2 |  John  | john@email.com  |          |  a boy  |            |    Y

3 |  Uni    | uni@email.com    |          |            |  none  |    Y

 

form:

 

Send email to this type of people:

 

<input type=checkbox name=c1 value="a girl">

<input type=checkbox name=c2 value="none">

<input type=checkbox name=c3 value="a boy">

 

PHP:

 

$sql = "SELECT email FROM contacts WHERE notify = 'Y' AND (checkbox is checked);

 

 

How can I specify the fields that are checked?

Link to comment
https://forums.phpfreaks.com/topic/165872-php-question-about-checkboxes/
Share on other sites

I'm not sure I'm understanding.  If the data is already in the database ie notify field is Y meaning they wish to be notified then when sending emails or at least generating the email list you should just use

 

$sql = "SELECT email FROM contacts WHERE notify = 'Y' "

Why not remove C1 C2 and C3 and call it sex. Then you can have "male" or "female"

 

Then change your options.

<form action="email-form.php" method="post">
<input type="checkbox" name="sex" value="female">Female
<input type="checkbox" name="sex" value="male">Male
</form>

 

$sql = "SELECT email FROM contacts WHERE notify = 'Y'  AND sex = 'male'";

Gotta restructure your database:

 

id | name |        email          |  sex    |    notify

1 |  Suzie | sue@email.com  |  girl  |      1

2 |  John  | john@email.com  |  boy  |      1

3 |  Uni    | uni@email.com    |          |      1

 

If you want to get the girls who want to be notified:

 

"SELECT email FROM contacts WHERE sex = 'girl' AND notify = 1"

 

If you want boys who want to be notified:

 

"SELECT email FROM contacts WHERE sex = 'boy' AND notify = 1"

 

If you want both sexes, you use

 

"SELECT email FROM contacts WHERE(sex = 'boy' OR sex = 'girl') AND notify = 1"

If you want both sexes, you use

 

SELECT email FROM contacts WHERE(sex = 'boy' OR sex = 'girl') AND notify = 1

 

I've included a shorter version of the query because it's useful shorthand, especially when you have more then two OR alternatives:

 

SELECT email FROM contacts WHERE sex in ('boy','girl') AND notify = 1

 

You might also consider making sex a char(1) field and limit it to M or F ;)

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.