Jump to content

Problem with PHP Search script.


andrew_ww

Recommended Posts

I had a script that worked fine. I then attempted to change it to allow the user to select a radio button which would determine which column in the database would be searched. I think I'm going the right way in solving this however not matter which radio button I select it always searches the same recordset.

Does anything look out of place with my code?


[code]<?php


<?
$var = @$_GET['q'] ; // get the query for the search engine (if applicable)
$trimmed = trim($var); //trim whitespace from the stored variable


// Connection to DBase
mysql_connect($host,$user,$password);
@mysql_select_db($dbase) or die("Unable to select database");




$table = mysql_real_escape_string($table);
$field_to_search = "site";
$field_to_search1 = "work";
$field_to_search = mysql_real_escape_string($field_to_search);
$field_to_search1 = mysql_real_escape_string($field_to_search1);

$trimmed = mysql_real_escape_string($trimmed);
$radio = (!empty($_POST['RadioGroup1'][0])) ? $_POST['RadioGroup1'][0] : 'number';
if (trim($radio) === 'number') {
  $query = "SELECT * FROM $table WHERE $field_to_search LIKE '$trimmed'";
} else {
  $query = "SELECT * FROM $table WHERE $field_to_search1 LIKE '$trimmed'";
}


$result = mysql_query($query)or die(mysql_error());
$count = mysql_num_rows($result);
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title><?php echo $row_user['user_name']; ?>'s personal page</title>
<link rel="stylesheet" href="../css/basic.css" type="text/css" media="screen" charset="utf-8">
</head>
<body>
<div id="wrapper">


<form name="search" method="GET" action="<?=$PHP_SELF?>">
  <p>Search the database for:</p>
  <table width="240" border="0" cellpadding="0" cellspacing="0">
    <tr>
      <td width="121"><label>
        <input type="text" name="q" />
        </label></td>
    <td width="119">        <div align="center">
      <input type="submit" name="search" value="Search" />     
    </div></td>
    </tr>
    <tr>
      <td><input name="RadioGroup1[]" type="radio" value="number" checked="checked" selected/>
Number</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td><input type="radio" name="RadioGroup1[]" value="name" />
Name</td>
      <td>&nbsp;</td>
    </tr>
  </table>
  <p>&nbsp;  </p>
</form>
<br />
<hr />


<?
if ($trimmed == "")
{
echo "<p>Please enter a search...</p>";
exit;
}

// check for a search parameter
if (!isset($var))
{
echo "<p>We dont seem to have a search parameter!</p>";
exit;
}

$numresults=mysql_query($query);
$numrows=mysql_num_rows($numresults);

if ($numrows == 0)
{
echo "<h4>Results</h4>";
echo "<p>Sorry, your search: \"" . $trimmed . "\" returned zero results</p>";
}

// next determine if s has been passed to script, if not use 0
if (empty($s))
{
$s=0;
}

// get results
$result = mysql_query($query) or die("Couldn't execute query");

if($numrows > 1){ $return = "results";}
else{ $return = "result"; }

// display what the person searched for
echo "<p>Your search for \"" . $var . "\" returned $numrows $return.</p> <hr />";

// begin to show results set
$count = 1 + $s ;

while ($r= mysql_fetch_array($result))
{
//$id = $r["id"];
$site = $r["site"];
$name = $r["work"];
$region = $r["region"];

$count++ ;
?>

<strong>
Site Number:</strong> <? echo $site ?> and the site name is <? echo ucwords(strtolower($name)) ?> and the region is <? echo $region ?>
<input type="submit" name="Submit" value="Admin" />
<br />
Result Number: <? echo $count ?>
<br /><br />
<br />
<hr />
<? } ?>

</div>
</body>
</html>

<?php
mysql_free_result($result);
?> [/code]
Link to comment
Share on other sites

if (trim($radio) == 'number') {

Two == not three ===

What's with the @ signs? I thought that was only in Perl. PHP does this too?

Also, this is superfluous
$field_to_search = "site";
$field_to_search1 = "work";
$field_to_search = mysql_real_escape_string($field_to_search);
$field_to_search1 = mysql_real_escape_string($field_to_search1);

You defined them, you don't need to escape them.

I don't know why it doesn't work, I hate ternary operators. But you should just be able to do
$radio = mysql_real_escape_string($_POST['RadioGroup1']);
if($radio == 'number'){
}
Link to comment
Share on other sites


I've tried what you suggested, partial success in that in now that it searches the text record and not the numerical one.

Does it matter where this code blocks site, i.e. should it be after the form ?

[code]$radio = mysql_real_escape_string($_POST['RadioGroup1']);
if (trim($radio) === 'number') {
  $query = "SELECT * FROM $table WHERE $field_to_search LIKE '$trimmed'";
} else {
  $query = "SELECT * FROM $table WHERE $field_to_search1 LIKE '$trimmed'";
}[/code]

So no matter what radio button I select it's using the $field_to_search1 variable as the criteria.
Link to comment
Share on other sites

right before the loop...
Bah, I guess that's not technically a "loop". Before the ifelse.

$radio = mysql_real_escape_string($_POST['RadioGroup1']);
print $radio;
if (trim($radio) === 'number') {
  $query = "SELECT * FROM $table WHERE $field_to_search LIKE '$trimmed'";
} else {
  $query = "SELECT * FROM $table WHERE $field_to_search1 LIKE '$trimmed'";
}
Link to comment
Share on other sites


Here the two URL strings that are displayed.

If 'number' radio button selected:

http://localhost/01/user01/new%20search.php?q=testvalue+&RadioGroup1%5B%5D=number

and this is what I get when the second radio button is selected:

http://localhost/01/user01/new%20search.php?q=testvalue&search=Search&RadioGroup1%5B%5D=name

Hopefully this might highlight where its going wrong ?
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.