'Undefined index' notice - what does this mean?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • iera
    New Member
    • Jul 2007
    • 1

    'Undefined index' notice - what does this mean?

    i've got this message

    Notice: Undefined index: name in c:\program files\easyphp1-8\www\forum\kem askini.php on line 2

    i've used this coding..

    [code=php]<?php
    $name = $_POST['name'];
    $date = $_POST['date'];
    $time = $_POST['time'];
    $email = $_POST['email'];
    $comment = $_POST['comment'];

    $conn = mysql_connect(" localhost","roo t","");
    if (!$conn)
    {
    die('Could not connect: ' . mysql_error());
    }

    $db=mysql_selec t_db("forum", $conn);

    $query = "UPDATE ruang_forum
    SET date='$date', time='$time', email='$email', comment='$comme nt'
    WHERE name='$name'";

    if($query)
    {
    echo '';
    }
    else
    echo 'Error....';

    $result = mysql_query($qu ery);
    mysql_close($co nn);
    ?>
    <html><head><ti tle>forum</title><body>

    <p><a href="http://127.0.0.1/forum/main2.html">Hom e</a></p> <br>
    </body></head></html>[/code]

    [Please use CODE tags when posting source code. Thanks! --pbmods]

    can anybody help me to solve this problem?? tq :)
  • ronnil
    Recognized Expert New Member
    • Jun 2007
    • 134

    #2
    you get the error because the $_POST variable has not been set. In other words, the form has not yet been submitted.

    What you need to do is a statement that checks if the variable has been set. isset() is probably the best way to do this.

    if(isset($_POST['name']))
    {
    //your form has been submitted and you can go on executing you query message.
    }

    it is probably a good idea to do this with all your variables, assuring that the user has submitted anything.

    If you just want to remove the error, you can put an @ before $_POST['name']

    the @ silences any errors made by $_POST

    Comment

    • pbmods
      Recognized Expert Expert
      • Apr 2007
      • 5821

      #3
      Changed thread title to better describe the problem (did you know that threads whose titles contain only one word. such as, 'error' actually get FEWER responses?).

      Comment

      • kovik
        Recognized Expert Top Contributor
        • Jun 2007
        • 1044

        #4
        Originally posted by ronnil
        If you just want to remove the error, you can put an @ before $_POST['name']

        the @ silences any errors made by $_POST
        As a side-note, the second method is not recommended, but it is completely possible. Suppressing error messages is only useful before you know how to use try...catch statements on functions that deal with file handling or database connections. Also, the only other acceptable time to use it is if the error message would display private information that could compromise security.

        Comment

        • Motoma
          Recognized Expert Specialist
          • Jan 2007
          • 3236

          #5
          As others have said, your form did not post a 'name' variable. Remember, array indexes are case sensitive, so $_POST['name'] and $POST['Name'] are different fields.

          Comment

          Working...