Jump to content

Research Problem


brnevs
 Share

Recommended Posts

Hi! I have a problem and need some help....I'm triyng to search by database trough a form that I defined:

<form name="Pesquisa" action="produtos.php" method="post"><input type="radio" name="pes" value="mar"/> Marca<input type="radio" name="pes" value="mod"/> Modelo<input type="text" name="pesq" /><input type="submit" value="pesquisar" /></form>

and I'm using this php code:

<? if(isset($_GET["sort"]) && !empty($_GET["sort"]))    {        $sort = $_GET["sort"];		$tab = db_query("SELECT * FROM produtos WHERE cat='" . mysql_real_escape_string($sort) . "'");    }elseif (isset($_POST["pesq"]) && !empty($_POST["pes"]))	{	$cam = $_POST["pes"];	$pes = $_POST["pesq"];	$tab = db_query("SELECT * FROM produtos WHERE " . mysql_real_escape_string($cam) . "='%". mysql_real_escape_string($pes) ."%'");	}		for ($a=0; $a<=$li; $a++) {$res = mysql_fetch_assoc($tab);echo '<tr><td width="25%"><h3>'.$res['cat'].'</td>';echo '<td width="25%"><h3>'.$res['mar'].'</td>';echo '<td width="30%"><h3>'.$res['mod'].'</td>';echo '<td width="20%"><h3>'.$res['pvp'].'</td></tr>';}?>

the problem is that I can't search for parts of a string in my table fields. this was my last try.... without the wildcards % it works fine but it only gives me a product on my table if the user inputs the exact same product name or model, for ex:product brand: cooler masterinput:cooler masterretrieves product lines... ok!product brand: cooler masterinput:coolerdoesn't retrieve product lines... not ok!Thank's for the help in advance!

Link to comment
Share on other sites

What structure is the data Table? Is it designated for a full text search?
cod varchar(50) primary_keycat varchar(30)mar varchar(20) *mod varchar(50) *desc varchar(300)pvp int(11)I didn't have the full text search activated before but I activated it on the fields markd with *, still it didn't made any diference. Edited by brnevs
Link to comment
Share on other sites

Just out of curiosity, print out this query after you build it:"SELECT * FROM produtos WHERE " . mysql_real_escape_string($cam) . "='%". mysql_real_escape_string($pes) ."%'"
Tried and it gives nothing, I've change the query to this"SELECT * FROM produtos WHERE " . mysql_real_escape_string($cam) . "LIKE '%". mysql_real_escape_string($pes) ."%'"and still nothing, but I'm starting to think that it happens because of the validation section where I define which query to use.... or the way i'm printing it....I am using xampp and in phpmyadmin when I run this query it works just fine... and in my page all the other searches are shown fine in my table but this ones...I've even changed the way it gets the value of the button selected by a validation and continues the same:
<?phpsession_start();require 'inserir.php';$ped = db_query("SELECT * FROM produtos");$error='';if(isset($_GET["sort"]) && !empty($_GET["sort"]))    {        $sort = $_GET["sort"];		$ped = db_query("SELECT * FROM produtos WHERE cat='" . mysql_real_escape_string($sort) . "'");		}if (isset($_POST["pesq"]) && !empty($_POST["sel"]))	{	$cam = $_POST["sel"];	$pes = $_POST["pesq"];	if ($cam = 'mar'){	$ped = db_query("SELECT * FROM produtos WHERE 'mar' LIKE '%" . mysql_real_escape_string($pes) . "%'");		}	elseif ($cam = 'mod'){	$ped = db_query("SELECT * FROM produtos WHERE 'mod' LIKE '%" . mysql_real_escape_string($pes) . "%'");		}	}?>

Ok!!! now the search works.... it's so stupid $ped = db_query("SELECT * FROM produtos WHERE 'mod' LIKE '%" . mysql_real_escape_string($pes) . "%'"); - wrong!!!$ped = db_query("SELECT * FROM produtos WHERE mod LIKE '%" . mysql_real_escape_string($pes) . "%'"); - right!!!!!but still no number search, and no model search...

Edited by brnevs
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...