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
3
  • 1
    move ur query code inside if(isset($_POST['submit'])) Commented Apr 8, 2014 at 4:24
  • 1
    possible duplicate of PHP: "Notice: Undefined variable" and "Notice: Undefined index" Commented Apr 8, 2014 at 4:25
  • Take the closing bracket of isset($_POST['submit']) and place it in between mysqli_close($con); and ?> Commented Apr 8, 2014 at 4:29

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

1

Update this insert query,

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

Hope this help you!

Comments

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);

  }?>

2 Comments

Try @user1153551 answer
@Zu007 - take your complete query code inside if condition not just a sql statement.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.