Forum Moderators: coopster

Message Too Old, No Replies

Problem with Query

i get no results and no error message

         

Malle

6:54 am on Nov 6, 2004 (gmt 0)

10+ Year Member



Hi all,
i am trying to retrieve data from a mysql table.
Since i only want to have certain columns displayed and not all i am trying to make a dropdown together with the search.

The problem is that the search form immediatly displays resetted and there is no output even if i search for a correct data.

I have tried for two days now and can't get it to work. Maybe someone has an idea. Here is the code:

<?PHP 

INCLUDE 'db.php';

IF(!empty($_POST['select'])) {

$sql = mysql_query("SELECT first_name, last_name, ".$_POST['extrafield']." FROM users
WHERE ".$_POST['search']." LIKE '%".$_POST['extrafield']."%'");

WHILE ($row = mysql_fetch_array($sql)) {?>

<HTML>
<HEAD></HEAD>
<BODY>

<DIV ALIGN=center>
<TABLE BORDER="1" CELLSPACING="0" CELLPADDING="1">
<TR>
<TD ALIGN=left BGCOLOR=#1A5D15><?php ECHO $row["first_name"];?></TD>
<TD ALIGN=left BGCOLOR=#1A5D15><?php ECHO $row["last_name"];?></TD>
<TD ALIGN=left BGCOLOR=#1A5D15><?php ECHO $row["extrafield"];?></TD>
</TR>
</TABLE>
</DIV>

<? mysql_close($connection);
}

}
ELSE {

?>

<FORM ACTION="testsearch.php" METHOD="post">
<DIV ALIGN=center>
<TABLE BORDER="1" CELLSPACING="0" CELLPADDING="1">
<TR><TD>Choose Field: <SELECT NAME="extrafield">
<OPTION VALUE="id">ID</option>
<OPTION VALUE="email_address">E-Mail</option>
<OPTION VALUE="username">User</option>
<OPTION VALUE="password">PW</option>
</SELECT>
Search For: <INPUT TYPE="text" NAME="search"></TD></TR>
<TR><TD><INPUT TYPE="submit" NAME="Select"></TD></TR>
</TABLE>
</DIV>
</FORM>

<? }?>

</BODY>
</HTML>


Any help is welcome
Malle

[edited by: jatar_k at 7:40 am (utc) on Nov. 6, 2004]
[edit reason] no personal urls thanks [/edit]

dreamcatcher

7:55 am on Nov 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Malle,

Try echoing your query to see exactly whats it is saying:

$sql = mysql_query("SELECT first_name, last_name, ".$_POST['extrafield']." FROM users WHERE ".$_POST['search']." LIKE '%".$_POST['extrafield']."%'");

echo $sql;
exit;

Should this:

WHERE ".$_POST['search']." LIKE '%".$_POST['extrafield']."%'");

actually be this:

WHERE ".$_POST['extrafield']." LIKE '%".$_POST['search']."%'");

dc :)

Malle

8:41 am on Nov 6, 2004 (gmt 0)

10+ Year Member



Hi Dreamcatcher,
yes you were right, i switched extrafield and search hehe.

Then i tried following and didnt get a result back (also no error message)

<?PHP 

INCLUDE 'db.php';

$sql = mysql_query("SELECT first_name, last_name, ".$_POST['extrafield']." FROM users
WHERE ".$_POST['extrafield']." LIKE '%".$_POST['search']."%'");

echo $sql;
exit;

?>

Somehow it seems as if the IF condition never takes place. But i just don't know why.
I would write more to the problem if i knew what it was about. Till then i have to rely on you people out there to put me in the right direction :)

Thx for trying to help me
Malle

tomda

9:08 am on Nov 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Firstly, change
<INPUT TYPE="submit" NAME="Select">
to
<INPUT TYPE="submit" NAME="select">

I am sure it will help.
------------------
If it doesn't resolve the problem, than you can use a hidden value in your form :
<INPUT name='step' type='hidden' value='1'>

Just retrieve the data using PHP_POST and change your if statement to :
if ($step=="1") {blah blah}

Malle

9:43 am on Nov 6, 2004 (gmt 0)

10+ Year Member



Thx tomda!

Firstly, change
<INPUT TYPE="submit" NAME="Select">
to
<INPUT TYPE="submit" NAME="select">

I am sure it will help.

Nope didn't help. Maybe i have to put in value as well? will try later....


If it doesn't resolve the problem, than you can use a hidden value in your form :
<INPUT name='step' type='hidden' value='1'>

Just retrieve the data using PHP_POST and change your if statement to :
if ($step=="1") {blah blah}

This did the job. Thanks very much, you are the best :)
Even tho i still don't know why it didn't work the other way round...but i couldn't care less atm ;)

You saved my day
Malle

dreamcatcher

10:34 am on Nov 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



LOL! Glad you got it sorted. :)