473,396 Members | 1,966 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Problem passing values from a multi-field search page to the MySQL statement

40
I am trying to put together a PHP search page in which the user can select none, one, two, or three fields to search, and then the results php will build the SQL with dynamic where caluses to reflect the fields chosen.

The first page, where they select the search fields and submit:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $db = mysql_connect("localhost", "root", "yeahright");
  3. if (!$db)
  4.    {
  5.    die('Could not connect:'.mysql_error);
  6.    }
  7. mysql_select_db("mytest",$db);
  8. $sql = "SELECT distinct fac_id FROM incidents ORDER BY fac_id";
  9. $result = mysql_query($sql);
  10. if (!$result)
  11.     {
  12.     die('Error: '.mysql_error());
  13.     }
  14. $sql2 = "SELECT distinct person_type FROM incidents ORDER BY person_type";
  15. $result2 = mysql_query($sql2);
  16. if (!$result2)
  17.     {
  18.     die('Error: '.mysql_error());
  19.     }
  20. $sql3 = "SELECT distinct severity FROM incidents ORDER BY severity";
  21. $result3 = mysql_query($sql3);
  22. if (!$result3)
  23.     {
  24.     die('Error: '.mysql_error());
  25.     }
  26. mysql_close($db);
  27. ?>
  28.  
and

Expand|Select|Wrap|Line Numbers
  1. <FORM METHOD="POST" ACTION="ir_multiquery.php">
  2. <p>Search existing Incident Reports by selecting one of the search fields below:</p>
  3. <select name="fac_id" size="1">
  4. <option selected value="">Facility</option>
  5.  
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. while ($myrow = mysql_fetch_array($result))
  3.    {
  4.    echo "<option value='fac_id'>".$myrow["fac_id"]."</option>\n";
  5.    }
  6. ?>
  7.  
Expand|Select|Wrap|Line Numbers
  1. </select>
  2. <select name="person_type" size="1">
  3. <option selected value="">Person Type</option>
  4.  
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. while ($myrow2 = mysql_fetch_array($result2))
  3.    {
  4.    echo "<option value='person_type'>".$myrow2["person_type"]."</option>\n";
  5.    }
  6. ?>
  7.  
Expand|Select|Wrap|Line Numbers
  1. </select>
  2. <select name="severity" size="1">
  3. <option selected value="">Severity Level</option>
  4.  
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. while ($myrow3 = mysql_fetch_array($result3))
  3.    {
  4.    echo "<option value='severity'>".$myrow3["severity"]."</option>\n";
  5.    }
  6. ?>
  7.  
Expand|Select|Wrap|Line Numbers
  1. </select>
  2. <p>
  3. <input type="submit" value="Search" />
  4. </p>
  5. </FORM>
  6.  
ir_multiquery.php:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $db = mysql_connect("localhost", "root", "yeahright");
  3. if (!$db)
  4.    {
  5.    die('Could not connect:'.mysql_error);
  6.    }
  7. mysql_query("mytest", $db);
  8. // The basic SELECT statement
  9. $select = 'SELECT fac_id';
  10. $from = ' FROM incidents';
  11. $where = ' WHERE 1=1';
  12. // If facility was selected
  13. $facility = $_REQUEST["fac_id"];
  14. if ($facility != '') {
  15. echo "REQUEST Facility: ";
  16. echo $_REQUEST["fac_id"];
  17. echo " ";
  18. $where .= ' AND fac_id='.$_REQUEST["fac_id"].'';
  19. //$where .= " AND fac_id='000955'";
  20. }
  21. // If person type was selected
  22. $persontype = $_REQUEST["person_type"];
  23. if ($persontype != '') {
  24. echo "Persontype: ";
  25. echo $_REQUEST["person_type"];
  26. $where .= ' AND person_type='.$_REQUEST["person_type"].'';
  27. }
  28. // If severity was selected
  29. $severity = $_REQUEST["severity"];
  30. if ($severity != '') {
  31. echo "Severity: ";
  32. $_REQUEST["severity"];
  33. $where .= ' AND severity='.$_REQUEST["severity"].'';
  34. }
  35. echo " Final Select: ";
  36. echo $select;
  37. echo $from;
  38. echo $where;
  39. ?>
  40.  
and

Expand|Select|Wrap|Line Numbers
  1. <table>
  2. <tr><th>Query</th><th>Results</th></tr>
  3.  
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $query  = sprintf($select . $from . $where);
  3. echo 'query: ';
  4. echo $query;
  5. if (!$query)
  6.    {
  7.    die('Could not run query:'.mysql_error);
  8.    }
  9. $result = mysql_query($query);
  10. echo '<p>result: ';
  11. echo $result;
  12. echo '</p>';
  13. if (!$result) {
  14. echo '</table>';
  15. exit('<p>Error retrieving data from db - this is from the $result var<br />'.
  16. 'Error: ' . mysql_error() . '</p>');
  17. }
  18. else
  19. {
  20. echo '<p>Eureka!</p>';
  21. }
  22. while ($myrow = mysql_fetch_array($result))
  23. {
  24. echo "<tr valign='top'>\n";
  25. $facility = $myrow['fac_id'];
  26. echo "<td>$facility</td>\n";
  27. echo "</tr>\n";
  28. }
  29. mysql_close($db);
  30. ?>
  31.  
Expand|Select|Wrap|Line Numbers
  1. </table>
  2.  
When I run this, I get the following:

The echo of $query shows that the three values I am trying to pass do not get passed - for example, if I select Facility it shows
SELECT fac_id FROM jos_incidents_combined WHERE 1=1 AND fac_id=fac_id

This of course causes the $result to be blank.

I then get the error:
Error retrieving data from db - this is from the $result var
Error: No database selected

I have checked and rechecked my code for the db connection, and it is functioning. Also, when I paste the $query in phpmyadmin without the AND part, it runs successfully.

Is the problem just the fact that I'm not passing the values or does the 'No database selected' indicate a second problem as well?

What changes do I need to do to my code to get the variables passed?

Thanks - I apologize for the large content - but I have re-read some of the PHP materials and tried a few changes but nothing seems to work.

TIA,

jej1216
Jun 4 '07 #1
6 2611
ronnil
134 Expert 100+
sounds like you are missing a mysql_select_db('db_name'); statement, but you're not. :)

You say that if you select facility it returns 0 rows? is that with other options than the hardcoded one? (with value=""), you shouldn't be getting any result if it's that one. (since fac_id in the query will then be "")

Why do you use 1=1 in your SQL statement?
Jun 4 '07 #2
jej1216
40
sounds like you are missing a mysql_select_db('db_name'); statement
Well, I feel like an idiot. That's exactly what was missing.

Thanks,

jej1216
Jun 4 '07 #3
ronnil
134 Expert 100+
lol... think it's getting late here... you got two files :P

well... congrats on solving the problem :)
Jun 4 '07 #4
jej1216
40
It definitely was late. The missing a mysql_select_db('db_name') got rid of the error about the database, but it's still not passing the variables from the first php page to the second.

I get this as the resulting SQL statement:
"query: SELECT fac_id, room_descr, person_type, severity FROM jos_incidents_combined WHERE 1=1 AND fac_id=fac_id"

It's probably a quote thing, but why isn't the variable from the first php page not getting passed?

Also, I use WHERE 1=1 so the other AND statements can be in any combination. Apparently, WHERE 1=1 is a way to run the query whether the user picks none, one, two, or three search variables.
Jun 5 '07 #5
jej1216
40
I've almost got it -- I changed to the following code in the 1st php:
Expand|Select|Wrap|Line Numbers
  1. while ($myrow = mysql_fetch_array($result))
  2.    {
  3.    echo "<option value='$myrow[fac_id]'>".$myrow[fac_id]."</option>\n";
  4.    }
  5. ?>
  6.  
For fac_id, which is numeric, this works - the variable is passed and the query results are correct.

Now I have a new error. When the variable to be passed is character, I need to know how to change the following code to enclose it in quotes for the select statement:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. while ($myrow2 = mysql_fetch_array($result2))
  3.    {
  4.    echo "<option value='$myrow2[person_type]'>".$myrow2[person_type]."</option>\n";
  5.    }
  6. ?>
  7.  
This code returns this SQL:
SELECT fac_id, room_descr, person_type, severity FROM incidents WHERE 1=1 AND person_type =Outpatient
and this message:
"Error: Unknown column 'Outpatient' in 'where clause'"

How do I get the php code to enclose the value "Outpatient?"

thanks,

jej1216
Jun 5 '07 #6
jej1216
40
Since this is a new problem, I have started a new thread for how to add single quotes to a passed value.

Thanks,

jej1216
Jun 5 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: harry | last post by:
I have 2 multi-dim arrays double subTotals = null; String rowTitles = null; I want to pass them to a function that initialises & populates them like so - loadData( rowTitles, subTotals);
2
by: John Leslie | last post by:
I am porting a script from Korn Shell to python and want to pass named parameters like -JOB 123456 -DIR mydir I can get it to work passing --JOB and --DIR but not -JOB and -DIR Any ideas? ...
1
by: Berend | last post by:
I am trying to pass multi values into a where clause with an in clause in a store procedure to use in a Crystal report. This can change depending on the user. Maybe there is another way to pass...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
7
by: Harolds | last post by:
The code below worked in VS 2003 & dotnet framework 1.1 but now in VS 2005 the pmID is evaluated to "" instead of what the value is set to: .... xmlItems.Document = pmXML // Add the pmID...
11
by: truckaxle | last post by:
I am trying to pass a slice from a larger 2-dimensional array to a function that will work on a smaller region of the array space. The code below is a distillation of what I am trying to...
9
by: trint | last post by:
Ok, I have a thread that I start when user clicks to start: ThreadStart myThreadDelegate = new ThreadStart ThreadFunction1.getOneAtATime); Thread thr1 = new Thread(myThreadDelegate);...
3
by: SQLScott | last post by:
I have looked all over and I cannot find an example or information on passing a multi-dimensional array. Well, that is not true. I found a close example in C++ but it didn't work when I...
3
by: dice | last post by:
Hi, In order to use an external api call that requires a function pointer I am currently creating static wrappers to call my objects functions. I want to re-jig this so I only need 1 static...
3
by: ishwarbg | last post by:
Hi Everyone, I have a .Net Application, through which I am invoking a function from a legacy DLL developed in C++. My structure in C# contains some data of type double which I need to pass to to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.