SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 45
  1. #1
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,388
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    $_GET not working

    On this page I have:

    PHP Code:
    <?php  echo "<p><a href='tracking-edit.php?cvid=$cvid'>Made a mistake above? Click to edit.</a></p>"?>
    On the referenced page I have:

    PHP Code:
    $cvid $_GET['cvid'];     
    $cvid mysql_real_escape_string($cvid);

    $cover_tiers = @mysql_query("SELECT mag_name, mag_month, mag_year 
    FROM cover_tiers 
    WHERE id='
    $cvid'
    "
    );
    if (!
    $cover_tiers) {
        exit(
    '<p>Error fetching details. ' mysql_error() . '</p>'); } 
    I keep getting the error:

    Notice: Undefined index: cvid in C:\wamp\www\MEDIA_TRACKING\tracking-edit-COVER_TIERS.php on line 87

    This is line 87: $cvid = $_GET['cvid'];

    Why is the error being thrown?

    Thanks,
    Steve

  2. #2
    SitePoint Guru
    Join Date
    Aug 2009
    Posts
    669
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is the value actually being set in the html link? - I know you probably think it is but it looks to me like it might not be being set for some reason so when the link is clicked php doesn't see it.

    Can you show us the html for the link?
    I'll do anything to avoid working on my own code

    Are you using: if (isset($_POST['submit'])) ?
    IE has a bug and does not always send the value.

  3. #3
    SitePoint Guru
    Join Date
    Aug 2009
    Posts
    669
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Also can you var_dump $_GET please?
    I'll do anything to avoid working on my own code

    Are you using: if (isset($_POST['submit'])) ?
    IE has a bug and does not always send the value.

  4. #4
    SitePoint Addict Kevinslair's Avatar
    Join Date
    Jul 2002
    Location
    Binghamton
    Posts
    246
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I used to get the same error. You need to do this:

    PHP Code:
    <?php
    if(isset($_GET['cvid'])) {
    $cvid $_GET['cvid'];
    }
    ?>
    This stopped the error.
    I PHP!

  5. #5
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,388
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by tangoforce View Post
    Is the value actually being set in the html link? - I know you probably think it is but it looks to me like it might not be being set for some reason so when the link is clicked php doesn't see it.

    Can you show us the html for the link?
    Earlier on the page I have:
    PHP Code:
    $cover_tiers mysql_query("INSERT INTO cover_tiers SET
    mag_name = '
    $mag_name',
    mag_month = '
    $mag_month',
    mag_year = '
    $mag_year',
    "
    );
     
     
    $cvid mysql_insert_id(); 
    That's where I get the $cvid from. The insert works fine.

  6. #6
    SitePoint Guru
    Join Date
    Aug 2009
    Posts
    669
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Kevinslair View Post
    I used to get the same error. You need to do this:

    PHP Code:
    <?php
    if(isset($_GET['cvid'])) {
    $cvid $_GET['cvid'];
    }
    ?>
    This stopped the error.


    But that only hides the error. It doesn't solve why its happening in the first place. The Op wants to know why its not set when its being output in the hyperlink. Thats the problem hiding the error doesn't help as the error as actually useful in telling us there is something wrong.
    I'll do anything to avoid working on my own code

    Are you using: if (isset($_POST['submit'])) ?
    IE has a bug and does not always send the value.

  7. #7
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,077
    Mentioned
    103 Post(s)
    Tagged
    0 Thread(s)
    It's because $_GET['cvid'] isn't set. The error can be elimianted by the use of isset()

    PHP Code:
    if (isset($_GET['cvid'])) {
    $cvid $_GET['cvid']
    } else {
    $cvid 0// Sets a default value

    You need to look at the code that generates the value of $cvid in this line

    PHP Code:
     <?php  echo "<p><a href='tracking-edit.php?cvid=$cvid'>Made a mistake above? Click to edit.</a></p>"?>
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  8. #8
    SitePoint Guru
    Join Date
    Aug 2009
    Posts
    669
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by StevenHu View Post
    Earlier on the page I have:
    PHP Code:
    $cover_tiers mysql_query("INSERT INTO cover_tiers SET
    mag_name = '
    $mag_name',
    mag_month = '
    $mag_month',
    mag_year = '
    $mag_year',
    "
    );
     
     
    $cvid mysql_insert_id(); 
    That's where I get the $cvid from. The insert works fine.
    So is the value definitely in the html for the hyperlink when you look at it in the browser? - Can you show us the html the browser is seeing?
    I'll do anything to avoid working on my own code

    Are you using: if (isset($_POST['submit'])) ?
    IE has a bug and does not always send the value.

  9. #9
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,388
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Kevinslair View Post
    I used to get the same error. You need to do this:

    PHP Code:
    <?php
    if(isset($_GET['cvid'])) {
    $cvid $_GET['cvid'];
    }
    ?>
    This stopped the error.
    It's not stopping the error this time, sorry!

    Thanks,
    Steve

  10. #10
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,388
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by tangoforce View Post
    So is the value definitely in the html for the hyperlink when you look at it in the browser? - Can you show us the html the browser is seeing?
    Yes, the browser shows the correct id:

    http://localhost/tracking-edit.php?cvid=23

  11. #11
    SitePoint Guru
    Join Date
    Aug 2009
    Posts
    669
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by StevenHu View Post
    Yes, the browser shows the correct id:

    http://localhost/tracking-edit.php?cvid=23
    Thats odd then because it looks like some sort of php problem..

    Can you post the source for the whole script? - Maybe there is something else at play here..

    Also can you var_dump $_REQUEST ?
    I'll do anything to avoid working on my own code

    Are you using: if (isset($_POST['submit'])) ?
    IE has a bug and does not always send the value.

  12. #12
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,388
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Here's the page:
    PHP Code:
    <?php
    error_reporting
    (E_ERROR E_WARNING E_PARSE E_NOTICE);
    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html> 
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <link rel="stylesheet" type="text/css" href="tracking.css" />

    <title>Ad and Editorial Tracking: COVER TIERS EDIT ENTRY</title>


    </head>

    <body>

    <?php
    require_once ('tracking-CONNECT.php'); 
    include (
    'tracking-retrieve-inc_RETURN.php');


    /* ////////////////////////////////////////////////////////
    Insert COVER_TIERS table editing
    //////////////////////////////////////////////////////// */

    /* Checking to see if form was filled out for editing the table. If so, update the table. If not, show the fields for editing. */
    if (isset($_POST['mag_issue']))
    {
    $cvid $_POST['cvid'];
    $mag_name $_POST['mag_name'];
    $mag_month $_POST['mag_month'];
    $mag_year $_POST['mag_year'];

    $sqla "UPDATE cover_tiers SET
    mag_name = '
    $mag_name',
    mag_month = '
    $mag_month',
    mag_year = '
    $mag_year'
    WHERE id='
    $cvid'";

    if (@
    mysql_query($sqla)) { $inserta 1;
         } else {
         exit(
    '<p>Updating cover tiers table failed. Database not updated.' mysql_error() . '</p>'); 
         }


    echo 
    "<table class='form-table'><tbody>    <tr>";
    echo 
    "<td class='row-header' colspan='3'>";
    echo 
    "<h3 style='text-align:center'>DATA ENTRY RESULTS</h3></td></tr>";
    echo 
    "<tr><td class='row1'><p>You inserted:</p>";
    echo 
    "<p>Magazine name: " $mag_name;
    echo 
    "<br />Magazine month: " $mag_month;
    echo 
    "<br />Magazine year: " $mag_year "</p><br />";
            
    echo 
    "<br /><br />Cover Tiers table ID: " $cvid"</p>";
    echo 
    "</td></tr><tr><td class='row2'>";
    echo 
    "<p><a href:'tracking-edit.php?cvid=$cvid'>Click to edit</a></p>";
    echo 
    "</td></tr></table><br />";
    echo 
    "<div class='go'><p><a href='tracking-entry.php'>Return to Tracking Entry</a></p></div>";
    exit();

    // if (isset) closing tag

    /* ////////////////////////////////////////////////////////
    Edit COVER_TIERS table
    how-to: BYODB book, p.113-114
    //////////////////////////////////////////////////////// */

    /* Populate the fields for editing. */
    else {


    $cvid $_GET['cvid']) {
    $cvid mysql_real_escape_string($cvid); }

    $cover_tiers = @mysql_query("SELECT mag_name, mag_month, mag_year 
    FROM cover_tiers 
    WHERE id='
    $cvid'
    "
    );
    if (!
    $cover_tiers) {
        exit(
    '<p>Error fetching cover tier details. ' mysql_error() . '</p>'); }
        
    $cover_tiers mysql_fetch_array($cover_tiers);
    $mag_name mysql_real_escape_string($cover_tiers['mag_name']);
    $mag_month mysql_real_escape_string($cover_tiers['mag_month']);
    $mag_year mysql_real_escape_string($cover_tiers['mag_year']);
     
    ?>
     
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <p><strong>Edit the entry:</strong><br />
    <label>Magazine name: <input type="text" name="Magazine name" value="<?php echo $mag_name?>" /></label><br />
    <label>Magazine month: <input type="text" name="Magazine month" value="<?php echo $mag_month?>" /></label><br />
    <label>Magazine year: <input type="text" name="Magazine year" value="<?php echo $mag_year?>" /></label><br />
    <input type="hidden" name="cvid" value="<?php echo $cvid?>" />
    <input type="submit" value="Submit Changes" /></p>
    </form>
    <div class='go'><p><a href='tracking-entry.php'>Return to Tracking Entry</a></p></div>

    <?php }  ?>

    </body></html>

  13. #13
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,388
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by tangoforce View Post
    Also can you var_dump $_GET please?
    string '26' (length=2)

  14. #14
    SitePoint Addict Kevinslair's Avatar
    Join Date
    Jul 2002
    Location
    Binghamton
    Posts
    246
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think right here:

    $cvid = $_GET['cvid'] {
    $cvid = mysql_real_escape_string($cvid); }

    //change to:

    if(isset($_GET['cvid'])) {
    $cvid = $_GET['cvid'];
    }else{
    $cvid = "default value";
    }

    $cvid = mysql_real_escape_string($cvid); }
    I PHP!

  15. #15
    SitePoint Addict Kevinslair's Avatar
    Join Date
    Jul 2002
    Location
    Binghamton
    Posts
    246
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Also, I don't think the curly bracket after:
    PHP Code:
    $cvid mysql_real_escape_string($cvid); } 

    is needed.
    I PHP!

  16. #16
    SitePoint Guru
    Join Date
    Aug 2009
    Posts
    669
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But that still doesn't explain why the value is not being passed. The dump clearly shows its not there.

    StevenHu if you have Teamviewer installed I can take a look with you thsi evening (UK time).
    I'll do anything to avoid working on my own code

    Are you using: if (isset($_POST['submit'])) ?
    IE has a bug and does not always send the value.

  17. #17
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,077
    Mentioned
    103 Post(s)
    Tagged
    0 Thread(s)
    Try changing the
    PHP Code:
    $cvid $_GET['cvid']) { 
    to
    PHP Code:
    $cvid $_POST['cvid']) { 
    as your using POST for the form method
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  18. #18
    SitePoint Guru
    Join Date
    Aug 2009
    Posts
    669
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SpacePhoenix View Post
    as your using POST for the form method
    Since when did a hyperlink become $_POST ? - look at the very top of the page he is using a hyperlink. Yes the source for the page does use the same value as a hidden element in a form but if you look at the script again you can see that is processed elsewhere using $_POST.

    The ops problem is that the hyperlink isn't passing the variable in $_GET.
    I'll do anything to avoid working on my own code

    Are you using: if (isset($_POST['submit'])) ?
    IE has a bug and does not always send the value.

  19. #19
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,077
    Mentioned
    103 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $cvid $_GET['cvid']) { 
    to

    PHP Code:
    $cvid $_GET['cvid'] { 
    You've got a stray ) and a stray { in there
    Last edited by SpacePhoenix; Feb 22, 2011 at 07:59.
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  20. #20
    SitePoint Enthusiast webdesignhouston's Avatar
    Join Date
    Dec 2010
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Seems like you have full error reporting enabled. The error is actually a notice which you can turn off by changing the "error_reporting" value inside php.ini or inside the code.

    Anyway, the problem is there and you should check for the presence of the key inside an array before attempting to access it. Lots of ways to do this. Here is the simplest one of them all:

    PHP Code:
    $cvid = @$_GET['cvid'];
            ^---- 
    error suppression operator 
    Alternate way is to use is_set():

    PHP Code:
    $cvid = isset($_GET['cvid']) ? $_GET['cvid'] : ''

    Yet another way is to use array_key_exists():

    PHP Code:
    $cvid array_key_exists('cvid'$_GET) ? $_GET['cvid'] : ''

  21. #21
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,388
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thanks for pointing out the stray characters. I saw them and removed them.

    When the page is first accessed, the $cvid is successfully carried over, for the number is echoed correctly next to the Submit button:

    PHP Code:
    <?php echo $cvid?>
    <input type="hidden" name="cvid" value="<?php echo $cvid?>" />
    <input type="submit" value="Submit Changes" /></p>
    </form>
    The fields are correctly filled in for editing. But making the changes to the fields returns NULL for $cvid when the Submit button is pressed and the page is refreshed, and I get the notice of the undefined var cvid. Currently, I have:

    PHP Code:
    if(isset($_GET['cvid'])) {
    $cvid $_GET['cvid'];
    $cvid mysql_real_escape_string($cvid); } 

    var_dump($cvid);
    $cover_tiers = @mysql_query("SELECT mag_name, mag_month, mag_year 
    FROM cover_tiers 
    WHERE id='
    $cvid'
    "
    ); 
    Also, I had overlooked an error at the top of the page:

    if (isset($_POST['mag_issue']))

    mag_issue should be named mag_name, since I had made a change to the table, and I forgot to update the page at that location. Even with this fix, the page is not updating the table.

    In summary:
    The cvid is correctly carried over the first time. When changes are made to the fields, that's when the cvid is undefined.

    Thanks,
    Steve

  22. #22
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,388
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Another thing: when the form fields are edited and the page is refreshed, the form is not filled in again (the top form).

  23. #23
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,077
    Mentioned
    103 Post(s)
    Tagged
    0 Thread(s)
    Top form? There is only one form in that script, have you tried yet changing the occurennce in the script of $_GET to $_POST ?
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  24. #24
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,388
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    By "top form" I really meant the table's vars at top. Those are not filled in.

    Changing the GET to POST has the effect of not showing the fields filled in when first accessed, whereas GET shows the fields filled in from the beginning. In both cases, I get the undefined var notice.

  25. #25
    SitePoint Member
    Join Date
    Feb 2011
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I had encountered the similar problem once and then i used $_REQUEST instead of $_GET and it really worked for me.


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
  •