SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Member
    Join Date
    Jan 2014
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Query string error: Undefined index: id

    Hello experts,
    I am using a query string code. I want to "get" variable of one page to another page.

    Query1.php:
    PHP Code:
    <?php
    $konek 
    mysql_connect("localhost","root","") or die("Cannot connect to server");
    mysql_select_db("test",$konek) or die("Cannot connect to the database");
    $query mysql_query("select * from persons where id='1'");
    $row mysql_fetch_array($query);
    $q$row['details'];
    ?>

    <a href='query_1.php?id=" . $q . "'>aa</a>
    query_1.php

    PHP Code:
    <?php
    //$ab=1;

    //if(isset($_GET['id'])) 
    $ab$_GET['id'];
    echo 
    $ab;
    ?>


    The above code does not run and give error:

    Notice: Undefined index: id

    I am not geting what i am missing.
    So, can some help me out wid my mistake....

  2. #2
    SitePoint Addict
    Join Date
    Aug 2006
    Location
    Nantwich, Cheshire
    Posts
    282
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    What's the value of the 'id' variable in the URL if you 'view source' on the query1.php page, before you click the link to query_1.php?
    http://www.firenza.net - my homage to a car from the 1970s

  3. #3
    SitePoint Enthusiast
    Join Date
    Sep 2013
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Also you should not be using the mysql_* functions as they are deprecated.

    More info http://www.php.net/manual/en/intro.mysql.php

  4. #4
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    16,494
    Mentioned
    164 Post(s)
    Tagged
    1 Thread(s)
    In your example, it looks like you are trying to use the $q variable without "going back into" PHP.

  5. #5
    SitePoint Member
    Join Date
    Jan 2014
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well..., the code works perfectly fine in Query1.php page. But if i would nt have pasted my link
    i.e echo "<a href='edit_last_follow.php?id=" . $a . "'>aa</a>";

    inspite only :

    echo "<a href='edit_last_follow.php?id=" . $a . "'></a>";

    so how will i get the $a on my next page which is query_1.php page:
    Code:
    <?php  
    
    if(isset($_GET['id']))   
    $ab= $_GET['id'];  
    echo $ab;  
    ?>
    I want to echo $a of Query1.php page on query_1.php page.
    I tried with the code above. I am stuck what i am missing.

  6. #6
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    16,494
    Mentioned
    164 Post(s)
    Tagged
    1 Thread(s)
    PHP will only process PHP inside of <?php ...... ?> (technically not entirely correct, but close enough for this discussion)

    So once you have ?> the rest is interpreted as HTML untl it next sees <?php

    i.e. you need to enlose $q inside of <?php ..... ?>

    Or

    If your latter examples are closer to what you're doing, you need to be careful using quotes so the output isn't broken, eg. if the variale is 14
    HTML Code:
    <a href='edit_last_follow.php?id=14'>aa</a>
    <a href='edit_last_follow.php?id=14'></a>
    then as long as edit_last_follow.php is the file with the echo $_get you should see it OK

  7. #7
    SitePoint Member
    Join Date
    Jan 2014
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Mittineague View Post
    PHP will only process PHP inside of <?php ...... ?> (technically not entirely correct, but close enough for this discussion)

    So once you have ?> the rest is interpreted as HTML untl it next sees <?php

    i.e. you need to enlose $q inside of <?php ..... ?>

    Or

    If your latter examples are closer to what you're doing, you need to be careful using quotes so the output isn't broken, eg. if the variale is 14
    HTML Code:
    <a href='edit_last_follow.php?id=14'>aa</a>
    <a href='edit_last_follow.php?id=14'></a>
    then as long as edit_last_follow.php is the file with the echo $_get you should see it OK
    I have enclosed it in my Query1.php page:

    Code:
    <?php  
    $konek = mysql_connect("localhost","root","") or die("Cannot connect to server");  
    mysql_select_db("test",$konek) or die("Cannot connect to the database");  
    $query = mysql_query("select * from persons where id='1'");  
    $row = mysql_fetch_array($query);  
    $q= $row['details'];  
    
    echo "<a href='query_1.php?id=" . $q . "'>aa</a>"; 
    
    ?>
    But i dont want to give any link just get the variable $q in next page query_1.php
    Last edited by jackgoddy123; Jan 23, 2014 at 02:06. Reason: change

  8. #8
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    16,494
    Mentioned
    164 Post(s)
    Tagged
    1 Thread(s)
    If you try
    PHP Code:
    <?php  
    $konek 
    mysql_connect("localhost","root","") or die("Cannot connect to server");  
    mysql_select_db("test",$konek) or die("Cannot connect to the database");  
    $query mysql_query("select * from persons where id='1'");  
    $row mysql_fetch_array($query);  
    $q$row['details'];  
    var_dump($row);
    echo 
    "<br />";
    var_dump($q);
    //echo "<a href='query_1.php?id=" . $q . "'>aa</a>"; 
    ?>
    what do you see?

  9. #9
    SitePoint Member
    Join Date
    Jan 2014
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I get all my row value as an output.
    See, what i want is get th variable value on next page

  10. #10
    SitePoint Addict
    Join Date
    Aug 2006
    Location
    Nantwich, Cheshire
    Posts
    282
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    So you don't want a link that you have to press, you just want to go directly to the query_1.php page without clicking anything?

    If so, you could use:

    Code:
    header("Location: query_1.php?id=" . $q );
    instead of echoing the link, I think. There's probably a better way. When you displayed the link, did $q have the correct value in it? Remember we don't know your database layout so can't say whether $q= $row['details'] is going to need escaping for example.
    http://www.firenza.net - my homage to a car from the 1970s


Tags for this Thread

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
  •