473,545 Members | 2,705 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

40 New Member
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.p hp:

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_c ombined 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 2626
ronnil
134 Recognized Expert New Member
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 New Member
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 Recognized Expert New Member
lol... think it's getting late here... you got two files :P

well... congrats on solving the problem :)
Jun 4 '07 #4
jej1216
40 New Member
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_c ombined 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 New Member
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 New Member
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
12598
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
1567
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? Current code : try:
1
11796
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 multi values. CREATE OR REPLACE PROCEDURE eva_sp_wrk014_spec_test ( p_eva_product_header_ids IN VARCHAR2, cur_spec_cd IN...
58
10072
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 code... TCHAR myArray; DoStuff(myArray);
7
2836
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 parameter to the XSLT stylesheet XsltArgumentList xsltArgList = new XsltArgumentList(); xsltArgList.AddParam("pmID", "", pmID);...
11
4432
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 accomplish. // - - - - - - - - begin code - - - - - - - typedef int sm_t; typedef int bg_t; sm_t sm; bg_t bg;
9
1166
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); thr1.Start(); The process started contains a Very large function that is in a continuous loop (here is part of it just to demonstrate my problem):
3
9338
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 "converted it" to VB.Net. My receiving method looks like this: Public DoIt(ByRef Array( , ) as string) as boolean but what I am not clear on is what...
3
2600
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 wrapper function. I would prefer to be able to pass the member function as a void* to the static wrapper but I suspect this may not even be possible....
3
2119
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 the DLL to get some results back from it. My Structure In C# looks like this: public struct InputPurchaseOrder { public...
0
7432
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7689
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7943
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6022
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5076
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3490
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3470
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1919
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1044
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.