我的数据正在保存在数据库中,但显示以下通知:
注意:未定义的索引:fname在D:\ xampp \ htdocs \ savedata \ saa.php中
20行
注意:未定义索引:lname在D:\ xampp \ htdocs \ savedata \ saa.php中
20行
添加了1条记录
这是验证代码:
<?php
$firstname=$lastname="";
$firstnameErr=$lastnameErr="";
if ($_SERVER['REQUEST_METHOD']== "POST") {
$valid = true; //Your indicator for your condition, actually it depends on what you need. I am just used to this method.
if(empty($_POST["fname"]))
{
$firstnameErr="*Name is Required";
$valid=false;
}
else
{
$firstname=test_input($POST["fname"]);
}
if(empty($_POST["lname"]))
{
$lastnameErr="*Name is Required";
$valid=false;
}
else
{
$$lastname=test_input($POST["lname"]);
}
//if valid then redirect
if($valid){
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=saa.php">';
exit;
// header('Location: datasubmitted.php');
// exit();
}
}
// Sanitize data
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
html表单代码
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
Firstname: <input type="text" name="fname" /><?php echo $firstnameErr;?><br />
Lastname: <input type="text" name="lname" /><?php echo $lastnameErr?>
<input type="submit" name="submit" value="Submit"/>
</form>
在数据库中插入数据的代码
<?php
$con = mysql_connect("localhost","root","geetha@99");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("test", $con);
$sql="INSERT INTO nametable (firstname,lastname)
VALUES
('$_POST[fname]','$_POST[lname]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
mysql_close($con)
?>
最佳答案
它是一个通知;不是错误
要解决此问题,您必须检查是否设置了$_POST['fname']
和$_POST['lname']
:
if(!isset($_POST["fname"] && empty($_POST["fname"])) {
...
}
if(!isset($_POST["lname"] && empty($_POST["lname"])) {
...
}