Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Nov 2006
    Location
    Malvern, PA
    Posts
    33
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Passing a query variable from a URL

    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:

    PHP Code:
    $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:

    Code:
    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.

  • #2
    Senior Coder koyama's Avatar
    Join Date
    Dec 2006
    Location
    Copenhagen, Denmark
    Posts
    1,246
    Thanks
    1
    Thanked 5 Times in 5 Posts
    Get the $query printed:
    PHP Code:
    echo $query
    It will tell you what's wrong. Maybe $tabsel is missing.

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

  • #3
    New Coder
    Join Date
    Nov 2006
    Location
    Malvern, PA
    Posts
    33
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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:

    PHP Code:
    $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
    PHP Code:
    $tabsel $HTTP_POST_VARS['tabsel']; 
    to

    PHP Code:
    $tabsel $_GET['tabsel']; 
    and add
    Code:
    "&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?
    Last edited by dmilani; 01-03-2007 at 05:06 AM.

  • #4
    Senior Coder koyama's Avatar
    Join Date
    Dec 2006
    Location
    Copenhagen, Denmark
    Posts
    1,246
    Thanks
    1
    Thanked 5 Times in 5 Posts
    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 Code:
    <?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...

  • #5
    New Coder
    Join Date
    Nov 2006
    Location
    Malvern, PA
    Posts
    33
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Thumbs up Problem Solved

    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:

    PHP Code:

    $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;" 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •