Reputation: 35
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
Upvotes: 0
Views: 415
Reputation: 10479
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.
Upvotes: 1
Reputation: 35
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
Upvotes: 0
Reputation: 8369
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);
}?>
Upvotes: 0
Reputation: 27614
Update this insert query,
$sql="insert into form(name,website,gender,comment) values('". $name ."','". $website ."','". $gender ."','". $comment ."')";
Hope this help you!
Upvotes: 1