...

View Full Version : searching help



bfsog
06-19-2005, 05:30 PM
Im trying to search two columns in a table. They are called author and code_name

I use an if statement to see what the user wants to search for, but the variables always get set to the second if

So when I try and search by author I get my error message "Please do not use weird characters"

Heres the code I am using


if (isset($_POST['user'])) {
$searchby = $_POST['user'];
$setting = "user";
}
if(isset($_POST['codename'])) {
$searchby = $_POST['codename'];
$setting = "codename";
}
// Check for illegal characters
if(!eregi("[a-zA-Z_0-9]",$searchby)){
echo "Please do not use weird characters.";
exit;
}

include ("db.php");
if ($setting == "user") {
$query = "SELECT * FROM code WHERE author LIKE '%$searchby%' ORDER BY code_id DESC";
} else if ($setting == "codename") {
$query = "SELECT * FROM code WHERE code_name LIKE '%$searchby%' ORDER BY code_id DESC";
}
$result = mysql_query($query);
$numrows = mysql_num_rows($result);


Any ideas?

SeeIT Solutions
06-19-2005, 05:38 PM
It is doing that because when you submit the form it sets both $_POST['user'] and $_POST['codename']. You could use this to get around that


if (isset($_POST['user']) && $_POST['user'] != "") {
$searchby = $_POST['user'];
$setting = "user";
} elseif (isset($_POST['codename']) && $_POST['codename'] != "") {
$searchby = $_POST['codename'];
$setting = "codename";
} else {
//redirect back to search page because nothing is set
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum