www.webdeveloper.com
Results 1 to 6 of 6

Thread: Variable $q=$_GET["q"]

  1. #1
    Join Date
    Aug 2004
    Location
    Florianópolis
    Posts
    267

    Variable $q=$_GET["q"]

    Hi,

    The variable $q=$_GET["q"]; is passing correctly the value "q" to the $query and echoing the . $row['YEAR'] .

    HOWEVER, it is not working with the while statement IN THE SAME CODE.

    PHP Code:
    while ($rows $result2->fetch_array())

    {
    $row0 [] = $rows['YEAR'];            
    $row1 [] = $rows['MONTH'];        
    $row2 [] = $rows['SALES'];

    IF I try to type directly the value after the $_GET it does work ( $q=2013; for example )

    Can anyone gime a help to make the while statement work with $q=$_GET["q"];

    Bellow is the complete code.

    Appreciate any help

    PHP Code:
    <?php
    $q
    =$_GET["q"];

    $mysqli = new mysqli('localhost','user','pswd','database');


    /* check connection */
    if (mysqli_connect_errno()) {
        
    printf("Connect failed: %s\n"mysqli_connect_error());
        exit();    
    }

    $query "SELECT * FROM table_test WHERE `YEAR`='".$q."'";

    $result $mysqli->query($query);

    if (
    $row $result->fetch_array())

    {
      echo 
    "<td>" $row['YEAR'] . "</td>";

      }    

    $result2 $mysqli->query($query);

    while (
    $rows $result2->fetch_array())

    {
    $row0 [] = $rows['YEAR'];            
    $row1 [] = $rows['MONTH'];        
    $row2 [] = $rows['SALES'];
    }    
        


        
    /* free result set */
        
    $result->close();


    $mysqli->close();

    ?>
    Sandro27

  2. #2
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,374
    You have already run a query once and then you repeat the same query, I don't see the logic of your script.

    You need to check to see if any rows were returned with the proper command, then you use the while loop to iterate the results

    PHP Code:
    $query "SELECT * FROM table_test WHERE `YEAR`='".$q."'";

    $result $mysqli->query($query);

    if ( 
    $result->num_rows )
    {
        while (
    $rows $result->fetch_array() ){
            echo 
    $rows['YEAR'] . "/" $rows['MONTH'] . " = " $rows['SALES'] . "<br />";

    for example...

    You also need to sanitize your input in to the database query otherwise you run the risk of someone trying to execute sql injection codes to break the script and database.
    Last edited by \\.\; 08-01-2013 at 04:07 AM.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  3. #3
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    817
    Are all your column names really all Uppercase?

  4. #4
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,374
    Quote Originally Posted by ginerjm View Post
    Are all your column names really all Uppercase?
    It makes no difference if the users naming convention is in upper case or lower case or a mixed case.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  5. #5
    Join Date
    Aug 2004
    Location
    Florianópolis
    Posts
    267
    Hi

    Thank you for your reply !

    I get the rows returned correctly if I type the value instead of getting it with the GET command , for example: $q=$_GET["q"]; instead I do $q=2013;

    I reason I am using the query twice is because I have another page that has AJAX which sends the the value from a selector box.

    see bellow.

    Code:
    <script>
    function showyear(str)
    {
    if (str=="")
      {
      document.getElementById("txtHint").innerHTML="";
      return;
      } 
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
        document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
        }
      }
    xmlhttp.open("GET","db_connect.php?q="+str,true);
    xmlhttp.send();
    }
    </script>
    </head>
    <body>
    <div class="gridContainer clearfix">
      <div id="Header">
        <h1>Header</h1>
    <form>
    <select name="years" onChange="showyear(this.value)">
    <option value="">Select a year:</option>
    <option value="2012">2012</option>
    <option value="2013">2013</option>
    </select>
    </form>
    <div id="txtHint"><b>year will be listed here.</b></div>
    So the problem is that I am not getting the code to work with $q=$_GET["q"]; on the while statement.
    Sandro27

  6. #6
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,374
    The point I am making is that you already have the query results, you only need reset the row marker to the beginning of the results set and then process the results again.

    You should also sanitize the input from the request as well as escape the string ready for use in the query string.

    So what exactly is happening when your form submits its data, is it doing anything?
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles