Jump to content

more complicated "select from where and or" statement


pioneerx01

Recommended Posts

Why is this not working? I can not seem to incorporate and into the statement

 

$query  = "SELECT * FROM Project_Registrations WHERE 
(First_Name = '$_POST[name_1]' AND Teacher = '$_POST[teacher]' AND School = '$_POST[school]') OR
(Second_Name = '$_POST[name_1]' AND Teacher = '$_POST[teacher]' AND School = '$_POST[school]') OR
(First_Name = '$_POST[name_2]' AND Teacher = '$_POST[teacher]' AND School = '$_POST[school]') OR
(Second_Name = '$_POST[name_2]' AND Teacher = '$_POST[teacher]' AND School = '$_POST[school]')
";

 

Thanks

Link to comment
Share on other sites

i dont think your building the string correctly, try this and let me know if it works....

 

$query  = 
"SELECT * FROM Project_Registrations WHERE 
(First_Name = '". $_POST[name_1] . "' AND Teacher = '". $_POST[teacher] ."' AND School = '". $_POST[school] ."') 
OR 
(Second_Name = '". $_POST[name_1] ."' AND Teacher = '". $_POST[teacher] ."' AND School = '". $_POST[school] ."') 
OR (First_Name = '". $_POST[name_2] ."' AND Teacher = '". $_POST[teacher] ."' AND School = '". $_POST[school] ."') 
OR (Second_Name = '". $_POST[name_2] ."' AND Teacher = '". $_POST[teacher] ."' AND School = '". $_POST[school] ."')

 

Try that and see what happens, if not echo out the result of yours and mine and see whats shown, you could then paste the displayed SQL statements directly into mysql_workbench or phpmyadmin (whichever you use) and that will show you where the problem is

 

BTW - This is my first time ever responding to a PHP post, so if im well off - be nice :)

Link to comment
Share on other sites

i dont think your building the string correctly, try this and let me know if it works....

 

$query  = 
"SELECT * FROM Project_Registrations WHERE 
(First_Name = '". $_POST[name_1] . "' AND Teacher = '". $_POST[teacher] ."' AND School = '". $_POST[school] ."') 
OR 
(Second_Name = '". $_POST[name_1] ."' AND Teacher = '". $_POST[teacher] ."' AND School = '". $_POST[school] ."') 
OR (First_Name = '". $_POST[name_2] ."' AND Teacher = '". $_POST[teacher] ."' AND School = '". $_POST[school] ."') 
OR (Second_Name = '". $_POST[name_2] ."' AND Teacher = '". $_POST[teacher] ."' AND School = '". $_POST[school] ."')

 

Try that and see what happens, if not echo out the result of yours and mine and see whats shown, you could then paste the displayed SQL statements directly into mysql_workbench or phpmyadmin (whichever you use) and that will show you where the problem is

 

BTW - This is my first time ever responding to a PHP post, so if im well off - be nice :)

 

That isn't any different from the code in the OP, functionally.

Link to comment
Share on other sites

Concatenation with that many variables also results in a huge number of typos and the resulting php syntax errors.

 

@pioneerx01 your query is LOGICALLY correct SQL, though it can be greatly simplified. If it's not working, then either your variables don't have the values you expect (as already suggested) or your query is producing an error of some kind (I'm not going to bother to list all of the possibilities because your error checking and error reporting logic in your code should be telling you if a query error occurred) or you aren't executing the query (we see that a lot) or you aren't fetching the results correctly from the query (we see that a lot too)...

Link to comment
Share on other sites

Speaking of greatly simplified sql, the following should (untested) work (assuming your table/columns names are correct, you are escaping data, your data has expected values...) -

 

$names = "'{$_POST['name_1']}','{$_POST['name_2']}'";
$query  = "SELECT * FROM Project_Registrations WHERE Teacher = '{$_POST['teacher']}' AND School = '{$_POST['school']}' AND
(First_Name IN($names) OR Second_Name IN($names))";

Link to comment
Share on other sites

Ah my bad, i just tested it and the Concatenation works either way i didnt think that " '$_POST[name]' " would work, there are always those small things that people miss or forget  :-[

 

So Thanks

 

Anyway, back to solving it,

 

firstly like other people have already stated, make sure that your getting the correct values, your running the query, and fetching results (make sure you have posted values), if its failing at any of those points just let us know where and what errors you get and im sure someone will be able to help.

 

If you do need to post back for assistance i would suggest that you echo your query and post all your code and any error messages your getting or explain your unexpected results

 

 

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.