0

i read all answer about undefined index error but not help full for me because i'm already using isset function to check plz how to slove this problem..

<?php
$con=mysqli_connect("localhost","root","","contact");
if (mysqli_connect_errno())
{
    echo "failed".mysqli_connect_error();
    }

checking for submited data

if(isset($_POST['submit']))
        {
        $name=$_POST['name']; 
        $website=$_POST['website']; 
        $gender=$_POST['gender']; 
        $comment=$_POST['comment']; 
        }
        $sql="insert into form(name,website,gender,comment) Values('$_POST[name]','$_POST[website]','$_POST[gender]','$_POST[comment]')";

        if(!mysqli_query($con,$sql))
            {
                die('error:'.mysqli_error($con));
            }
        else "added";

        mysqli_close($con);
        ?>
    <html>
        <body>
            <form method=post action="<?php echo $_SERVER['PHP_SELF']; ?>">
                Name: <input type="text" name="name"><br>
                E-mail: <input type="text" name="email"><br>
                Website: <input type="text" name="website"><br>
                <input type="radio" name="gender" value="female">Female
                <input type="radio" name="gender" value="male">Male<br>
                Comment: <textarea name="comment" rows="5" cols="40"></textarea>
                 <input type=submit name="submit"><br>
            </form>
        </body>

these errors comes

Notice: Undefined index: name in H:\Wamp\Xamp\htdocs\form.php on line 15

Notice: Undefined index: website in H:\Wamp\Xamp\htdocs\form.php on line 15

Notice: Undefined index: gender in H:\Wamp\Xamp\htdocs\form.php on line 15

Notice: Undefined index: comment in H:\Wamp\Xamp\htdocs\form.php on line 15
Zu007
  • 35
  • 6
  • 1
    move ur query code inside if(isset($_POST['submit'])) – Abhik Chakraborty Apr 08 '14 at 04:24
  • 1
    possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – Rikesh Apr 08 '14 at 04:25
  • Take the closing bracket of isset($_POST['submit']) and place it in between `mysqli_close($con);` and `?>` – Vagabond Apr 08 '14 at 04:29

4 Answers4

1

Update this insert query,

$sql="insert into form(name,website,gender,comment) values('". $name ."','". $website ."','". $gender ."','". $comment ."')";

Hope this help you!

Jaykumar Patel
  • 26,836
  • 12
  • 74
  • 76
1

Please try the following corrected code :

if(isset($_POST['submit']))
        {
        $name=isset($_POST['name']) ? $_POST['name'] : ''; 
        $website=isset($_POST['website']) ? $_POST['website'] : ''; 
        $gender=isset($_POST['gender']) ? $_POST['gender'] : ''; 
        $comment=isset($_POST['comment']) ? $_POST['comment'] : ''; 
        $sql="insert into form(name,website,gender,comment) Values('$name','$website','$gender','$comment')";

        // Open the database connection here
        // aka, mysqli_connect()


        if(!mysqli_query($con,$sql))
            {
                die('error:'.mysqli_error($con));
            }
        else "added";

        mysqli_close($con);
        }
        ?>
    <html>
        <body>
            <form method=post action="<?php echo $_SERVER['PHP_SELF']; ?>">
                Name: <input type="text" name="name"><br>
                E-mail: <input type="text" name="email"><br>
                Website: <input type="text" name="website"><br>
                <input type="radio" name="gender" value="female">Female
                <input type="radio" name="gender" value="male">Male<br>
                Comment: <textarea name="comment" rows="5" cols="40"></textarea>
                 <input type=submit name="submit"><br>
            </form>
        </body>

What I did is added validation to check if those fields are set, and if so, then set the value, if not, then set the variable (aka $name) to ''. You should probably add some further validation in the event of required fields being = '' (equal to blank).

I also adjusted your query to not use the $_POST vars, instead it uses the variables that you are assigning the $_POST values to, so you know they exist for sure.

And lastly, I moved the mysql connection code and query itself into the if(isset(submit)) statement so it does not try to process those on regular page load where the form has not been submitted yet.

Kraang Prime
  • 9,981
  • 10
  • 58
  • 124
0

Change

if(isset($_POST['submit']))
        {
        $name=$_POST['name']; 
        $website=$_POST['website']; 
        $gender=$_POST['gender']; 
        $comment=$_POST['comment']; 
        }
        $sql="insert into form(name,website,gender,comment) Values('$_POST[name]','$_POST[website]','$_POST[gender]','$_POST[comment]')";
        if(!mysqli_query($con,$sql))
        {
            die('error:'.mysqli_error($con));
        }
       else "added";

       mysqli_close($con);
       ?>

to

if(isset($_POST['submit']))
{
        $name=$_POST['name']; 
        $website=$_POST['website']; 
        $gender=$_POST['gender']; 
        $comment=$_POST['comment']; 
        $sql="insert into form(name,website,gender,comment) values ('$name','$website','$gender','$comment')";
        if(!mysqli_query($con,$sql))
        {
            die('error:'.mysqli_error($con));
        }
        else "added";
        mysqli_close($con);

  }?>
Jenz
  • 8,280
  • 7
  • 44
  • 77
0

than friends problem is slove What wrong?

if(isset($_POST['submit']))
        {
        $name=$_POST['name']; 
        $website=$_POST['website']; 
        $gender=$_POST['gender']; 
        $comment=$_POST['comment']; 


        $sql="insert into form(name,website,gender,comment) Values('". $name . "','" . $website . "','" . $gender . "','" . $comment . "')";

//these also in if(isset()) Block

if(!mysqli_query($con,$sql))
            {
                die('error:'.mysqli_error($con));
            }
        else "added";
        }

thanx to all

Zu007
  • 35
  • 6