This question already has an answer here:
这个问题在这里已有答案:
- “Notice: Undefined variable”, “Notice: Undefined index”, and “Notice: Undefined offset” using PHP 28 answers
使用PHP 28答案“注意:未定义的变量”,“注意:未定义的索引”和“通知:未定义的偏移量”
<?php
$servername = "localhost";
$username = "root";
$password = "vish";
$database = "android";
$conn= new mysqli($servername,$username,$password,$database);
if(!$conn)
{
#ie("connection failed" ,mysql_connect_error());
echo "connection failed";
}
if(!isset($_POST['fname']))
{
$uname = $_POST['fname'];
}
if(!isset($_POST['lname']))
{
$lnam = $_POST['lname'];
}
$qur = "
INSERT INTO test(Username, l_name)
VALUES ('$uname','$lnam')
";
$fetch_qur = "SELECT * FROM test";
$result = mysqli_query($conn,$qur);
$result2 = mysqli_query($conn,$fetch_qur);
while($row=mysqli_fetch_assoc($result2))
{
echo $row['Username'];
echo $row['l_name'];
}
mysqli_close($conn);
?>
Its giving me error as Notice: Undefined index: fname in C:\wamp\www\test\database.php on line 17 as well as for lname.
它给出了我的错误,注意:未定义的索引:第17行的C:\ wamp \ www \ test \ database.php中的fname以及lname。
this code works fine if i use get method . can anyone explain why this is so thanks
如果我使用get方法,此代码工作正常。任何人都可以解释为什么这是如此感谢
1 个解决方案
#1
You are checking it wrong. If they are present then set the value. This should be -
你检查错了。如果它们存在则设置值。这应该是 -
if(isset($_POST['fname']))
{
$uname = $_POST['fname'];
}
if(isset($_POST['lname']))
{
$lnam = $_POST['lname'];
}
Update
if(isset($_POST['fname']) && isset($_POST['lname']))
{
$uname = $_POST['fname'];
$lnam = $_POST['lname'];
$qur = "
INSERT INTO test(Username, l_name)
VALUES ('$uname','$lnam')
";
}
#1
You are checking it wrong. If they are present then set the value. This should be -
你检查错了。如果它们存在则设置值。这应该是 -
if(isset($_POST['fname']))
{
$uname = $_POST['fname'];
}
if(isset($_POST['lname']))
{
$lnam = $_POST['lname'];
}
Update
if(isset($_POST['fname']) && isset($_POST['lname']))
{
$uname = $_POST['fname'];
$lnam = $_POST['lname'];
$qur = "
INSERT INTO test(Username, l_name)
VALUES ('$uname','$lnam')
";
}