SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 48
  1. #1
    SitePoint Guru hisham777's Avatar
    Join Date
    Dec 2005
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question GET value and execute it as query

    my opolagies if this is considered as a dublicated thread.

    Q is this code is to get the category id from a table and display it
    as a link.
    PHP Code:
    while ($row mysql_fetch_assoc($result)) {
    echo 
    '<a href="Displaycategory.php?category_id='
    '">'
    $row['category_id']  
    '</a>' '<br />'
    this is the code to GET the product id an fatch the rows from a table
    where the product id = Whats in GET
    PHP Code:
    $result mysql_query("select * from products where category_id = '"$_GET['category_id'] ."'");

    if (!
    $result) {
    die(
    'Invalid query: ' mysql_error());
    }

    if(
    mysql_num_rows($result) != 0)
    {
    while(
    $rows mysql_fetch_assoc($result)) {
    echo 
    $rows['description'] .'<br />';
    }
    }
    else
    {
    echo 
    "no results";

    after testing the code the 1st one working fin it get the category id
    from the table and display it as a link.
    but when its clicked it execute the secound script but it does nothing
    -there are few records in the tables.

    is this the write method to use it as a query.?
    help please.

  2. #2
    SitePoint Wizard holmescreek's Avatar
    Join Date
    Mar 2001
    Location
    Northwest Florida
    Posts
    1,707
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    while ($row = mysql_fetch_assoc($result)) { 
     $category_id = ['category_id'];
      $title = ['category_title'];
      $output .= "<a href=\"Displaycategory.php?category_id=$category_id\">$title" ;
    }
    
     echo $output;
    intragenesis, llc professional web & graphic design

  3. #3
    SitePoint Wizard silver trophy Jelena's Avatar
    Join Date
    Feb 2005
    Location
    Universum, 3rd Corner
    Posts
    3,000
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Have you tried to echo $_GET["category_id"]?
    Try also to dump the results of the query..
    -- Jelena --

  4. #4
    SitePoint Wizard holmescreek's Avatar
    Join Date
    Mar 2001
    Location
    Northwest Florida
    Posts
    1,707
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In some cases, php might be generating an error from a problem in the second script, displaying the error messages in white text on a white background. Try running clicking on the link and press CTL+A, select all on the page and see if their are any error messages. This white on white effect doesn't occur on my local apache server, but, does occur on my hosted server.
    intragenesis, llc professional web & graphic design

  5. #5
    SitePoint Guru hisham777's Avatar
    Join Date
    Dec 2005
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i get this error
    Parse error: parse error, unexpected '[' in

    am trying.
    after modifing the code all i get is the error above.

  6. #6
    SitePoint Guru hisham777's Avatar
    Join Date
    Dec 2005
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sorry the error is on this line
    PHP Code:
    while ($row mysql_fetch_assoc($result)) { 
     --
    error--------------$category_id = ['category_id'];
      
    $title = ['category_title'];
      
    $output .= "<a href=\"Displaycategory.php?category_id=$category_id\">$title;


  7. #7
    SitePoint Wizard holmescreek's Avatar
    Join Date
    Mar 2001
    Location
    Northwest Florida
    Posts
    1,707
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Red face

    Quote Originally Posted by hisham777
    sorry the error is on this line
    PHP Code:
    while ($row mysql_fetch_assoc($result)) { 
      
    $category_id $row['category_id'];
      
    $title $row['category_title'];
      
    $output .= "<a href=\"Displaycategory.php?category_id=$category_id\">$title";

    Sorry, if your using my code above, I corrected it...I didn't prefix ['category_id'] which sould have been $row['category_id'] etc. Typing too quick for my own good.


    More than likely, if your getting a parse error your probably leaving out ), }, ; somewhere.
    intragenesis, llc professional web & graphic design

  8. #8
    SitePoint Guru hisham777's Avatar
    Join Date
    Dec 2005
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i get this massge
    Resource id #2

  9. #9
    SitePoint Wizard holmescreek's Avatar
    Join Date
    Mar 2001
    Location
    Northwest Florida
    Posts
    1,707
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How about pm me your code.
    intragenesis, llc professional web & graphic design

  10. #10
    SitePoint Wizard holmescreek's Avatar
    Join Date
    Mar 2001
    Location
    Northwest Florida
    Posts
    1,707
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why not just use mysql_fetch_array()?

    Field names returned mysql_fetch_assoc() are case-sensitive.
    intragenesis, llc professional web & graphic design

  11. #11
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    157 Post(s)
    Tagged
    3 Thread(s)
    Run this simplified script...
    PHP Code:
    $category_id $_GET['category_id'];
    echo 
    $category_id;
    $sql "select * from products where category_id = "$category_id
    echo 
    $sql;
    $query mysql_query($sql) or die(mysql_error());

    while(
    $rows mysql_fetch_assoc($query)) { 
    echo 
    $rows['description'] .'<br />'

    See what that gives you.

    Spike

  12. #12
    SitePoint Guru hisham777's Avatar
    Join Date
    Dec 2005
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    spikeZ after using the code i get this massge

    select * from products where category_id = 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 '' at line 1

  13. #13
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    157 Post(s)
    Tagged
    3 Thread(s)
    Do you not get an echo from the $category_id?

  14. #14
    SitePoint Guru hisham777's Avatar
    Join Date
    Dec 2005
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    no nothing the html source
    </head>

    <body>
    select * from products where category_id = 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 '' at line 1

  15. #15
    SitePoint Guru hisham777's Avatar
    Join Date
    Dec 2005
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    is it something have to do with global variable off or on .?

    am using
    php 5.0
    mySQL 5.0
    on IIS

  16. #16
    SitePoint Wizard holmescreek's Avatar
    Join Date
    Mar 2001
    Location
    Northwest Florida
    Posts
    1,707
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Stick in an :

    exit;

    right after echo $sql; in spikeZ code.

    If $category_id is null your going to have an incomplete $sql statement which is going to generate the MySQL error before the value of your $sql is displayed on the screen.
    intragenesis, llc professional web & graphic design

  17. #17
    SitePoint Guru hisham777's Avatar
    Join Date
    Dec 2005
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    bit confused holmescreek
    please exicuse my hembile knolewage of php & mysql
    what do you mean

  18. #18
    SitePoint Wizard holmescreek's Avatar
    Join Date
    Mar 2001
    Location
    Northwest Florida
    Posts
    1,707
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    $category_id = $_GET['category_id']; 
    
    $sql = "select * from products where category_id = ". $category_id;
    
    // --- debug
    
    echo "category_id    : $category_id" . "<br>"; 
    echo "sql statement :  $sql"; 
    exit;
    
    // --- end debug
    
    $query = mysql_query($sql) or die(mysql_error()); 
    
    while($rows = mysql_fetch_assoc($query)) { 
    echo $rows['description'] .'<br />'; 
    }
    See where I put the exit to terminate the program? The above will display the value of $category_id and your $sql statement then terminate the program before the sql query is executed.
    intragenesis, llc professional web & graphic design

  19. #19
    SitePoint Guru hisham777's Avatar
    Join Date
    Dec 2005
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i get this
    category_id :
    sql statement : select * from products where category_id =


    i think am to tired i been a wake 26 hours straight.
    i need some rest
    ill go to sleep.
    thanks Spikez and holmescreek

  20. #20
    SitePoint Wizard holmescreek's Avatar
    Join Date
    Mar 2001
    Location
    Northwest Florida
    Posts
    1,707
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Lightbulb

    Your on the right track! Notice your output above? category_id has no value. The value category_id is not being passed to your script correctly.

    Try taking this code and save it as test.php and load it in your browser. The first time you load test.php, it will tell you that $category_id is not set, then provides you a link that sets category_id to a random number.

    If this code fails to work then it is probably a setting in your php.ini file :

    Code:
    <?php
    
      $_GET['category_id'];
      
      if($category_id){
        echo "category_id : $category_id <br><br>";
      } else {
        echo "category_id is not set...<br><br>";
      }
      
      $some_value = rand(1,999);
      
      echo "<a href=\"test.php?category_id=$some_value\">click here to set category_id to $some_value</a>";
      
    
    ?>
    intragenesis, llc professional web & graphic design

  21. #21
    SitePoint Guru hisham777's Avatar
    Join Date
    Dec 2005
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    holmescreek i tryed the code
    it gives me this out put

    category_id is not set...

    click here to set category_id to 848

    html source
    <body>
    category_id is not set...<br><br><a href="test.php?category_id=639">click here to set category_id to 639</a>
    </body>

    it means that there is no variable past to GET

    my php.ini
    is standard exept for the extention file i have changed it to point
    to a folder were all my DLL files there
    i installed the manual install
    !!! ?????

  22. #22
    SitePoint Wizard holmescreek's Avatar
    Join Date
    Mar 2001
    Location
    Northwest Florida
    Posts
    1,707
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, run the code I gave you category_id shouldn't be set the first time, but if you click on the link each time, it should set category_id to some random value.

    After clicking the link, if category_id is still not set let me know. $_POST is a php superglobal.

    You stated your using PHP 5.0, I noted a bug in the change log where $_POST was not working in specific versions of PHP 5 : http://bugs.php.net/bug.php?id=32109

    --- so try upgrading to the newest version of PHP 5.x
    intragenesis, llc professional web & graphic design

  23. #23
    SitePoint Guru hisham777's Avatar
    Join Date
    Dec 2005
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi holmescreek
    i tryed your code
    i did click on the link but the variable is not set!!
    am using php 5.1.1
    and
    mysql-essential-5.0.18

  24. #24
    SitePoint Wizard holmescreek's Avatar
    Join Date
    Mar 2001
    Location
    Northwest Florida
    Posts
    1,707
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by holmescreek
    Your on the right track! Notice your output above? category_id has no value. The value category_id is not being passed to your script correctly.

    Try taking this code and save it as test.php and load it in your browser. The first time you load test.php, it will tell you that $category_id is not set, then provides you a link that sets category_id to a random number.

    If this code fails to work then it is probably a setting in your php.ini file :

    Code:
    <?php
    
      $category_id = $_GET['category_id']; // added this
      
      if($category_id){
        echo "category_id : $category_id <br><br>";
      } else {
        echo "category_id is not set...<br><br>";
      }
      
      $some_value = rand(1,999);
      
      echo "<a href=\"test.php?category_id=$some_value\">click here to set category_id to $some_value</a>";
      
    
    ?>
    Ok, I changed a line. Try it again, if it doesn't work, you have a problem with PHP. I can't find anything on why the $_POST could be empty. Even if register_globals is on or off it shouldn't affect the superglobal $_POST

    You might have to re-install php 5.1.1 is the only thing I can think of if this code doesn't work...if your running IIS, then it could be an issue with it, dunno, I run Apache.

    Maybe try posting my code in a new thread and asking someone with your same php version, server etc, to test it. If it runs ok, then you know its got to be your install.
    intragenesis, llc professional web & graphic design

  25. #25
    SitePoint Wizard holmescreek's Avatar
    Join Date
    Mar 2001
    Location
    Northwest Florida
    Posts
    1,707
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Another shot, check the post_max_size directive (even though its typicall used to restrict the size uploded files) in your php.ini. If this is like 0, then $_POST will return empty.
    intragenesis, llc professional web & graphic design


Bookmarks

Posting Permissions

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