我阅读了关于未定义索引错误的所有答案,但对我来说没有帮助,因为我已经使用isset函数来检查plz如何解决这个问题..
<?php
$con=mysqli_connect("localhost","root","","contact");
if (mysqli_connect_errno())
{
echo "failed".mysqli_connect_error();
}
检查提交的数据
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>
这些错误来自
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
答案 0 :(得分:1)
更新此插入查询
$sql="insert into form(name,website,gender,comment) values('". $name ."','". $website ."','". $gender ."','". $comment ."')";
希望这对你有帮助!
答案 1 :(得分:1)
请尝试以下更正的代码:
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>
我所做的是添加验证来检查这些字段是否已设置,如果是,则设置值,如果没有,则将变量(又名$ name)设置为''。如果必填字段=''(等于空白),您应该添加一些进一步的验证。
我还调整了你的查询以不使用$ _POST变量,而是使用你分配$ _POST值的变量,所以你知道它们确实存在。
最后,我将mysql连接代码和查询本身移动到if(isset(submit))语句中,因此它不会尝试处理那些尚未提交表单的常规页面加载。
答案 2 :(得分:0)
更改
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);
?>
到
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);
}?>
答案 3 :(得分:0)
比朋友问题更难解决 怎么了?
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(isset())Block
中if(!mysqli_query($con,$sql))
{
die('error:'.mysqli_error($con));
}
else "added";
}
thanx to all