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:
[code=php]
<?php
$db = mysql_connect(" localhost", "root", "yeahright" );
if (!$db)
{
die('Could not connect:'.mysql _error);
}
mysql_select_db ("mytest",$d b);
$sql = "SELECT distinct fac_id FROM incidents ORDER BY fac_id";
$result = mysql_query($sq l);
if (!$result)
{
die('Error: '.mysql_error() );
}
$sql2 = "SELECT distinct person_type FROM incidents ORDER BY person_type";
$result2 = mysql_query($sq l2);
if (!$result2)
{
die('Error: '.mysql_error() );
}
$sql3 = "SELECT distinct severity FROM incidents ORDER BY severity";
$result3 = mysql_query($sq l3);
if (!$result3)
{
die('Error: '.mysql_error() );
}
mysql_close($db );
?>
[/code]
and
[code=html]
<FORM METHOD="POST" ACTION="ir_mult iquery.php">
<p>Search existing Incident Reports by selecting one of the search fields below:</p>
<select name="fac_id" size="1">
<option selected value="">Facili ty</option>
[/code]
[code=php]
<?php
while ($myrow = mysql_fetch_arr ay($result))
{
echo "<option value='fac_id'> ".$myrow["fac_id"]."</option>\n";
}
?>
[/code]
[code=html]
</select>
<select name="person_ty pe" size="1">
<option selected value="">Person Type</option>
[/code]
[code=php]
<?php
while ($myrow2 = mysql_fetch_arr ay($result2))
{
echo "<option value='person_t ype'>".$myrow2["person_typ e"]."</option>\n";
}
?>
[/code]
[code=html]
</select>
<select name="severity" size="1">
<option selected value="">Severi ty Level</option>
[/code]
[code=php]<?php
while ($myrow3 = mysql_fetch_arr ay($result3))
{
echo "<option value='severity '>".$myrow3["severity"]."</option>\n";
}
?>
[/code]
[code=html]
</select>
<p>
<input type="submit" value="Search" />
</p>
</FORM>
[/code]
ir_multiquery.p hp:
[code=php]
<?php
$db = mysql_connect(" localhost", "root", "yeahright" );
if (!$db)
{
die('Could not connect:'.mysql _error);
}
mysql_query("my test", $db);
// The basic SELECT statement
$select = 'SELECT fac_id';
$from = ' FROM incidents';
$where = ' WHERE 1=1';
// If facility was selected
$facility = $_REQUEST["fac_id"];
if ($facility != '') {
echo "REQUEST Facility: ";
echo $_REQUEST["fac_id"];
echo " ";
$where .= ' AND fac_id='.$_REQU EST["fac_id"].'';
//$where .= " AND fac_id='000955' ";
}
// If person type was selected
$persontype = $_REQUEST["person_typ e"];
if ($persontype != '') {
echo "Persontype : ";
echo $_REQUEST["person_typ e"];
$where .= ' AND person_type='.$ _REQUEST["person_typ e"].'';
}
// If severity was selected
$severity = $_REQUEST["severity"];
if ($severity != '') {
echo "Severity: ";
$_REQUEST["severity"];
$where .= ' AND severity='.$_RE QUEST["severity"].'';
}
echo " Final Select: ";
echo $select;
echo $from;
echo $where;
?>
[/code]
and
[code=html]
<table>
<tr><th>Query </th><th>Results</th></tr>
[/code]
[code=php]
<?php
$query = sprintf($select . $from . $where);
echo 'query: ';
echo $query;
if (!$query)
{
die('Could not run query:'.mysql_e rror);
}
$result = mysql_query($qu ery);
echo '<p>result: ';
echo $result;
echo '</p>';
if (!$result) {
echo '</table>';
exit('<p>Error retrieving data from db - this is from the $result var<br />'.
'Error: ' . mysql_error() . '</p>');
}
else
{
echo '<p>Eureka!</p>';
}
while ($myrow = mysql_fetch_arr ay($result))
{
echo "<tr valign='top'>\n ";
$facility = $myrow['fac_id'];
echo "<td>$facil ity</td>\n";
echo "</tr>\n";
}
mysql_close($db );
?>
[/code]
[code=html]
</table>
[/code]
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
The first page, where they select the search fields and submit:
[code=php]
<?php
$db = mysql_connect(" localhost", "root", "yeahright" );
if (!$db)
{
die('Could not connect:'.mysql _error);
}
mysql_select_db ("mytest",$d b);
$sql = "SELECT distinct fac_id FROM incidents ORDER BY fac_id";
$result = mysql_query($sq l);
if (!$result)
{
die('Error: '.mysql_error() );
}
$sql2 = "SELECT distinct person_type FROM incidents ORDER BY person_type";
$result2 = mysql_query($sq l2);
if (!$result2)
{
die('Error: '.mysql_error() );
}
$sql3 = "SELECT distinct severity FROM incidents ORDER BY severity";
$result3 = mysql_query($sq l3);
if (!$result3)
{
die('Error: '.mysql_error() );
}
mysql_close($db );
?>
[/code]
and
[code=html]
<FORM METHOD="POST" ACTION="ir_mult iquery.php">
<p>Search existing Incident Reports by selecting one of the search fields below:</p>
<select name="fac_id" size="1">
<option selected value="">Facili ty</option>
[/code]
[code=php]
<?php
while ($myrow = mysql_fetch_arr ay($result))
{
echo "<option value='fac_id'> ".$myrow["fac_id"]."</option>\n";
}
?>
[/code]
[code=html]
</select>
<select name="person_ty pe" size="1">
<option selected value="">Person Type</option>
[/code]
[code=php]
<?php
while ($myrow2 = mysql_fetch_arr ay($result2))
{
echo "<option value='person_t ype'>".$myrow2["person_typ e"]."</option>\n";
}
?>
[/code]
[code=html]
</select>
<select name="severity" size="1">
<option selected value="">Severi ty Level</option>
[/code]
[code=php]<?php
while ($myrow3 = mysql_fetch_arr ay($result3))
{
echo "<option value='severity '>".$myrow3["severity"]."</option>\n";
}
?>
[/code]
[code=html]
</select>
<p>
<input type="submit" value="Search" />
</p>
</FORM>
[/code]
ir_multiquery.p hp:
[code=php]
<?php
$db = mysql_connect(" localhost", "root", "yeahright" );
if (!$db)
{
die('Could not connect:'.mysql _error);
}
mysql_query("my test", $db);
// The basic SELECT statement
$select = 'SELECT fac_id';
$from = ' FROM incidents';
$where = ' WHERE 1=1';
// If facility was selected
$facility = $_REQUEST["fac_id"];
if ($facility != '') {
echo "REQUEST Facility: ";
echo $_REQUEST["fac_id"];
echo " ";
$where .= ' AND fac_id='.$_REQU EST["fac_id"].'';
//$where .= " AND fac_id='000955' ";
}
// If person type was selected
$persontype = $_REQUEST["person_typ e"];
if ($persontype != '') {
echo "Persontype : ";
echo $_REQUEST["person_typ e"];
$where .= ' AND person_type='.$ _REQUEST["person_typ e"].'';
}
// If severity was selected
$severity = $_REQUEST["severity"];
if ($severity != '') {
echo "Severity: ";
$_REQUEST["severity"];
$where .= ' AND severity='.$_RE QUEST["severity"].'';
}
echo " Final Select: ";
echo $select;
echo $from;
echo $where;
?>
[/code]
and
[code=html]
<table>
<tr><th>Query </th><th>Results</th></tr>
[/code]
[code=php]
<?php
$query = sprintf($select . $from . $where);
echo 'query: ';
echo $query;
if (!$query)
{
die('Could not run query:'.mysql_e rror);
}
$result = mysql_query($qu ery);
echo '<p>result: ';
echo $result;
echo '</p>';
if (!$result) {
echo '</table>';
exit('<p>Error retrieving data from db - this is from the $result var<br />'.
'Error: ' . mysql_error() . '</p>');
}
else
{
echo '<p>Eureka!</p>';
}
while ($myrow = mysql_fetch_arr ay($result))
{
echo "<tr valign='top'>\n ";
$facility = $myrow['fac_id'];
echo "<td>$facil ity</td>\n";
echo "</tr>\n";
}
mysql_close($db );
?>
[/code]
[code=html]
</table>
[/code]
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
Comment