0

I'm currently working on a mail system and I've hit a stump.

I want to make it so once I select a checkbox such as "Mail To Alphas", it mails it to all of the Alphas. And if I were to select "Mail to Alphas" AND "Mail to Betas", it should send it to Alphas and Betas, and so on... Unfortunately, it's only sending the e-mail to the last selected checkbox, which would be Deltas.

I've attempted to use the ".=" multi-query feature, but I didn't know exactly how to tame it with this structure of code.

Here's my code:

if ($mailAllCadets) {
$result = mysqli_query($conn, "SELECT email,first_name,last_name FROM cadets ORDER BY id ASC");
}

if ($mailOneCadets) {
  $result = mysqli_query($conn, "SELECT email,first_name,last_name FROM cadets WHERE msl = '1' ORDER BY id ASC");
}

if ($mailTwoCadets) {
  $result = mysqli_query($conn, "SELECT email,first_name,last_name FROM cadets WHERE msl = '2' ORDER BY id ASC");
}

if ($mailThreeCadets) {
  $result = mysqli_query($conn, "SELECT email,first_name,last_name FROM cadets WHERE msl = '3' ORDER BY id ASC");
}

if ($mailFourCadets) {
  $result = mysqli_query($conn, "SELECT email,first_name,last_name FROM cadets WHERE msl = '4' ORDER BY id ASC");
}

if ($mailFiveCadets) {
  $result = mysqli_query($conn, "SELECT email,first_name,last_name FROM cadets WHERE msl = '5' ORDER BY id ASC");
}

$mailList = array(); // Create array containing recipients' e-mails.
$nameList = array(); // Create array containing the names of the recipients.

while($row = mysqli_fetch_array($result)) {
...
}

In plain text, this reads:

If "Mail All Cadets" is selected, gather information on all cadets.

If "Mail One Cadets" is selected, gather information on Level One cadets.

...

If "Mail Five Cadets" is selected, gather information on Level Five cadets.

Obviously, the logic is severely failing here, as it only follows the last-given command. How can I make it so it concatenates these queries?

Bellator
  • 352
  • 1
  • 3
  • 11
  • My advice: don't do it. Restructure your script to grab everything that you need in one trip to the db. If your momma told you to run to the shop and buy a dozen eggs and you made 12 trips to the shop -- she would speak to the neighbours about you being "different". – mickmackusa Jul 07 '20 at 02:28
  • First of all, you can significantly reduce your code by wrapping from **$mailOneCadets** to **$mailFiveCadets** in one single line. `if ($mailAllCadets) { // do something } else { // here put your query for mailing cadets from one to five $result = mysqli_query($conn, "SELECT email,first_name,last_name FROM cadets WHERE msl = '${noOfCadets}' ORDER BY id ASC") }` – Tushar Jul 07 '20 at 02:30
  • @mickmackusa I've thought about this approach. The only way I can think of doing it is, one `$result` query and an outside variable that concatenates a phrase such as "WHERE msl = ..." then echoing that variable into the one `$result` query. Just not sure how to execute it or if it would even work. I'll give it a go right now. – Bellator Jul 07 '20 at 02:30
  • A conditional `WHERE msl IN ()` would be all that it takes. You need to be using a prepared statement with bound parameters. https://stackoverflow.com/a/51036322/2943403 – mickmackusa Jul 07 '20 at 02:32
  • @mickmackusa That worked perfectly. I'll post my solution. If you'd like to make this a reply, I'll note you as answered. – Bellator Jul 07 '20 at 02:43
  • 1
    No one can post an answer as I have closed this question as a duplicate. Stack Overflow wants to minimize the amount of redundant content on the site (despite the actions of many users). I am happy that I could direct you to a working resolution. I do not need any unicorn points. You may elect to retract your question since it will likely be purged by the system in a few days anyhow. Please do not edit your question to include your working solution -- this would push your question into the review queue and cause unnecessary work for volunteers. – mickmackusa Jul 07 '20 at 02:44

0 Answers0