...

View Full Version : Passing a query variable from a URL



dmilani
01-03-2007, 04:51 AM
Hi Folks,

I am trying to pass a MySQL query variable from a URL. It is only working sometimes and I can't figure out why. Here is the pertinent PHP:



$sortvar = $_GET['sortvar'];

if (empty($sortvar)) {
$sortvar= 'tname' ;
}


$query = "SELECT tname, contact, email, reqdiv, day, eve, cell, misc FROM $tabsel WHERE sess_two= 'yes' ORDER BY '$sortvar' ASC";


I displaying the data in a table and what to sort by clicking on the headers of the table. The relevant HTML here is:



echo "<tr>" .
"<th><a href=\"RegDisplay1.php?sortvar=tname\">Team Name</a></th>" .
"<th><a href=\"RegDisplay1.php?sortvar=reqdiv\">Req Division</a></th>" .
"<th><a href=\"RegDisplay1.php?sortvar=contact\">Contact</a></th>" .
"<th><a href=\"RegDisplay1.php?sortvar=email\">Email</a> &nbsp;" ;


The table displays fine when viewed directly but when clicking the link to sort I get this error:


Query failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE sess_two= 'yes' ORDER BY 'email' ASC' at line 1

Any insight would be greatly appreciated.
Dan

P.S. Is there an official term to describe the passing of variables from a URL to PHP? I have tried to search for this and don't think I have been using the proper terms.

koyama
01-03-2007, 05:04 AM
Get the $query printed:


echo $query;

It will tell you what's wrong. Maybe $tabsel is missing.

Don't know about your P.S. question...

dmilani
01-03-2007, 06:00 AM
Thanks for the debug tip.

The issue is that $tabsel is not being set by the sorting link. The initial UI to the data sets $tabsel from a dropdown menu in a form. This is passed to the PHP successfully, but the variable is not retained with the sorting. Here is the PHP with the $tabsel segment:



$tabsel = $HTTP_POST_VARS['tabsel'];
$sortvar = $_GET['sortvar'];

if (empty($sortvar)) {
$sortvar= 'tname' ;
}


$query = "SELECT tname, contact, email, reqdiv, day, eve, cell, misc FROM $tabsel WHERE sess_one= 'yes' ORDER BY '$sortvar' ASC";


If I change

$tabsel = $HTTP_POST_VARS['tabsel'];

to


$tabsel = $_GET['tabsel'];

and add
"&tabsel=$tabsel" to the hyperlinks the sorting works, but then my initial table selection form doesn't work anymore. Is there a way to have my cake and eat it too?

koyama
01-03-2007, 06:19 AM
You will have to send the $tabsel together with the sorting thing also in subsequent requests---unless you store it on the server-side in a session variable or something like that.

How about adding a hidden input field sending $tabsel together with the sorting thing?



<?php
echo <<<CODE
<form>
<input type="hidden" name="tabsel" value="$tabsel" />
/* the rest of the form */
</form>
CODE;
?>

In this way it is sent again. Hard to give you more details without seeing the form...

dmilani
01-03-2007, 11:58 PM
Thanks for your help koyama,

The solution I came up with was to rename the variable in the URL and then check if the tabsel variable was set. Like so:




$tabsel = $HTTP_POST_VARS['tabsel'];
$sorttab = $_GET['sorttab'];
$sortvar = $_GET['sortvar'];

if (empty($sortvar)) {
$sortvar= 'tname' ;
}

if (empty($tabsel)) {
$tabsel= $sorttab ;
}

$query = "SELECT tname, contact, email, reqdiv, day, eve, cell, misc FROM $tabsel WHERE sess_one= 'yes' ORDER BY '$sortvar' ASC";

$result = mysql_query($query) or die('Query failed: ' . mysql_error());



echo "<table>";

echo "<tr>" .
"<th><a href=\"RegDisplay1.php?sortvar=tname&sorttab=$tabsel\">Team Name</a></th>" .
"<th><a href=\"RegDisplay1.php?sortvar=reqdiv&sorttab=$tabsel\">Req Division</a></th>" .
"<th><a href=\"RegDisplay1.php?sortvar=contact&sorttab=$tabsel\">Contact</a></th>" .
"<th><a href=\"RegDisplay1.php?sortvar=email&sorttab=$tabsel\">Email</a> &nbsp;" .



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum