Forum Moderators: coopster

Message Too Old, No Replies

forms & dynamic search.php

         

brodie_r

11:18 am on Feb 19, 2006 (gmt 0)

10+ Year Member



I want to be able to have one search.php and use it for searching different rows.

Here is part of my menu page, which includes a bunch of different text boxes. So i have Search by Name, Job ID, Machine, etc.

Here is a sample of my menu.php code (where the forms are located):


<form name="form1" method="post" action="search.php?mode=job_id">
<input name="searchstring" type="text" size="15" maxlength="15">
<input type="submit" name="submit" value="Search">
</form>

Then on my search page i am trying to make it so that it can search different columns. So i have tried to create a mode, so then on my menu page can just have mode=job_id, or mode=name, and it will search: WHERE $mode (the one chosen).

Here is the code i am currently trying to use on my search.php page:

require_once ('mysql_connect.php');
$searchstring = $_POST['searchstring'];
$mode2 = $_GET['mode']
$result = mysql_query("SELECT * FROM jobs WHERE $mode2 LIKE '%$searchstring%'",$dbc);
$num=mysql_numrows($result);
if ($num > 0) {
}

Can anyone see where i am going wrong, or have any suggestions or further questions.

jatar_k

4:29 pm on Feb 19, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I always use a couple steps for my queries

$result = mysql_query("SELECT * FROM jobs WHERE $mode2 LIKE '%$searchstring%'",$dbc);

I would write this like so

$query = "SELECT * FROM jobs WHERE " . $mode2 . " LIKE '%" . $searchstring . "%'";
echo '<p>select query: ',$query;
$result = mysql_query($query) or die ('<p>query died: ' . mysql_error());

I always concatenate my queries together instead of throwing all the variables into double quoted strings.

if I have trouble with a query I can then add an echo line so I can look it over to see if anything is missing. I can then comment that out later.

I use an 'or die' on my mysql_query call so I can see if there are any errors returning from mysql. This is also removed once the script is properly debugged.

you also don't need to pass the connection var to mysql_query unless there is more than one connection open.