Note that there are some explanatory texts on larger screens.

plurals
  1. POWhats wrong with this PHP IF statement?
    primarykey
    data
    text
    <p>I've got a PHP IF statement to save different SQL results in a PHP variable ($sql) based on the IF conditions, but it keeps returning SQL results based on just one condition (the first one), regardless of what the user enters in the POST'd values. </p> <p>All of the SQL statements work as expected when entered into phpMyAdmin individually (whilst changing the $row3 and $row4 to actual values that exist), just not with the PHP IF statement. </p> <p>Can anyone see what I'm doing wrong here, and if possible advise what I need to do differently? I know I'm no PHP / MySQL expert, but I'm stumped :(</p> <p>Any help or suggestions is greatly appreciated. Thanks in advance. </p> <pre><code>$row3 = $_POST['groups']; $row4 = $_POST['othercode-all']; IF ($row3='-all-' &amp;&amp; ($row4='-all-')) { $sql ="SELECT email, accountgroup, othercode FROM (SELECT email AS email, accountgroup, othercode FROM accounts UNION ALL SELECT email2 AS email, accountgroup, othercode FROM accounts UNION ALL SELECT email3 AS email, accountgroup, othercode FROM accounts) account WHERE email LIKE '%@%'"; } ELSEif ($row3!='-all-' &amp;&amp; ($row4='-all-')) { $sql ="SELECT email, accountgroup, othercode FROM (SELECT email AS email, accountgroup, othercode FROM accounts UNION ALL SELECT email2 AS email, accountgroup, othercode FROM accounts UNION ALL SELECT email3 AS email, accountgroup, othercode FROM accounts) account WHERE email LIKE '%@%' AND accountgroup = '$row3'"; } ELSEIF ($row4 != '-all-' &amp;&amp; ($row3 = '-all-')) { $sql ="SELECT email, accountgroup, othercode FROM (SELECT email AS email, accountgroup, othercode FROM accounts UNION ALL SELECT email2 AS email, accountgroup, othercode FROM accounts UNION ALL SELECT email3 AS email, accountgroup, othercode FROM accounts) account WHERE email LIKE '%@%' AND othercode = '$row4'"; } ELSE { $sql ="SELECT email, accountgroup, othercode FROM (SELECT email AS email, accountgroup, othercode FROM accounts UNION ALL SELECT email2 AS email, accountgroup, othercode FROM accounts UNION ALL SELECT email3 AS email, accountgroup, othercode FROM accounts) account WHERE email LIKE '%@%' AND accountgroup = '$row3' AND othercode = '$row4'"; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload