undefined variables!!!

Discussion in 'PHP' started by ninio, Feb 15, 2010.

  1. #1
    Hello everyone,
    On the code below I've got three undefined variables (name, comments and ID). How can I defined these three variables to be active on the code.

    Thanks>>>
    <?php
    error_reporting(E_ALL);
    ?>
    <body bgcolor="#CCCCFF"><?php
     
     
     
      mysql_connect("localhost", "root", "") or die('Error' . mysql_error());
      mysql_select_db("test1");
    
    
    // update data in mysql database
    $sql="UPDATE testtable SET name='$name', comments='$comments' WHERE ID='$id'";
    $result=mysql_query($sql);
    
    // if successfully updated.
    if($result){
    echo "Successful";
    echo "<BR>";
    echo "<a href='update3.php'>View result</a>";
    }
    
    else {
    echo "ERROR";
    }
    
    ?>
    
     
    PHP:
     
    ninio, Feb 15, 2010 IP
  2. bitist

    bitist Peon

    Messages:
    28
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    
    <?php
    $name='name_you_update';
    $comments='comment_you_update';
    $id='id_to_update';
    ?>
    
    PHP:
    Add this code above this line:
    
    $sql="UPDATE testtable SET name='$name', comments='$comments' WHERE ID='$id'";
    
    PHP:
     
    bitist, Feb 15, 2010 IP
  3. ninio

    ninio Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi Bitist..

    Thanks a lot I've all three variables defined now...

    but in another code I've this code withe undefined error.. How can I adjust it?

    // get value of id that sent from address bar
    $id=$_GET['ID'];
    PHP:
     
    ninio, Feb 15, 2010 IP
  4. bitist

    bitist Peon

    Messages:
    28
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    You have to have a get variable in the URL with name ID eg.
    http://yoururl.com/index.php?ID=12
     
    bitist, Feb 15, 2010 IP
  5. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #5
    what is the error your getting? i think its only a notice your having
     
    bartolay13, Feb 15, 2010 IP
  6. ninio

    ninio Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    yes, bartoly13
    it's just notice. and it shown that

    Notice: Undefined index: ID

    any idea how to define it?
     
    ninio, Feb 16, 2010 IP
  7. dannywwww

    dannywwww Well-Known Member

    Messages:
    804
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    110
    #7
    You could do something like this..

    $id = abs((int) $_GET['ID']) ? abs((int) $_GET['ID']) : '';
     
    dannywwww, Feb 16, 2010 IP
  8. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #8
    its only a notice, its not an error...
    you can either define it as "" or null
    or
    you can just put @ to disable the notice

    ie.
    $variable = @$_GET['variable'];
     
    bartolay13, Feb 16, 2010 IP
  9. northernweb

    northernweb Peon

    Messages:
    319
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    if it works but spills up errors, just disable PHP errors on your server. technically variables need to be defined at execution but sometimes they dont get defined until later, this causes error. so you can disable these errors and it will still work.
     
    northernweb, Feb 16, 2010 IP