andrew_ww Posted January 3, 2007 Share Posted January 3, 2007 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 DBasemysql_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> </td> </tr> <tr> <td><input type="radio" name="RadioGroup1[]" value="name" />Name</td> <td> </td> </tr> </table> <p> </p></form><br /><hr /><? if ($trimmed == ""){echo "<p>Please enter a search...</p>";exit;}// check for a search parameterif (!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 0if (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 forecho "<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><?phpmysql_free_result($result);?> [/code] Quote Link to comment https://forums.phpfreaks.com/topic/32726-problem-with-php-search-script/ Share on other sites More sharing options...
Jessica Posted January 3, 2007 Share Posted January 3, 2007 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'){} Quote Link to comment https://forums.phpfreaks.com/topic/32726-problem-with-php-search-script/#findComment-152332 Share on other sites More sharing options...
ober Posted January 3, 2007 Share Posted January 3, 2007 I'm not sure about Perl, but the @ signs in PHP are error suppression. Quote Link to comment https://forums.phpfreaks.com/topic/32726-problem-with-php-search-script/#findComment-152336 Share on other sites More sharing options...
Jessica Posted January 3, 2007 Share Posted January 3, 2007 Ah, in Perl I think @ means an array, like how $ means a variable in PHP. Quote Link to comment https://forums.phpfreaks.com/topic/32726-problem-with-php-search-script/#findComment-152338 Share on other sites More sharing options...
andrew_ww Posted January 3, 2007 Author Share Posted January 3, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/32726-problem-with-php-search-script/#findComment-152361 Share on other sites More sharing options...
Jessica Posted January 3, 2007 Share Posted January 3, 2007 I think you need to use == not ===. I think that is why it is always doing the second one.Can you echo the value of radio right before the loop and make sure it's set correctly? Quote Link to comment https://forums.phpfreaks.com/topic/32726-problem-with-php-search-script/#findComment-152362 Share on other sites More sharing options...
andrew_ww Posted January 3, 2007 Author Share Posted January 3, 2007 I'm sorry, I have changed that (cut and pasted the wrong stuff)Whereabout would the echo code go ? Quote Link to comment https://forums.phpfreaks.com/topic/32726-problem-with-php-search-script/#findComment-152365 Share on other sites More sharing options...
Jessica Posted January 3, 2007 Share Posted January 3, 2007 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'";} Quote Link to comment https://forums.phpfreaks.com/topic/32726-problem-with-php-search-script/#findComment-152368 Share on other sites More sharing options...
andrew_ww Posted January 3, 2007 Author Share Posted January 3, 2007 I added the print $radio however it behaved exactly the same ??The results page did not include any new output. Quote Link to comment https://forums.phpfreaks.com/topic/32726-problem-with-php-search-script/#findComment-152378 Share on other sites More sharing options...
andrew_ww Posted January 3, 2007 Author Share Posted January 3, 2007 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=numberand 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=nameHopefully this might highlight where its going wrong ? Quote Link to comment https://forums.phpfreaks.com/topic/32726-problem-with-php-search-script/#findComment-152384 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.