php post方法未定义数据库中的索引错误

时间:2015-05-07 10:37:59

标签: php

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

&GT;

它给出了我的错误通知:未定义的索引:第17行的C:\ wamp \ www \ test \ database.php中的fname 以及lname。

如果我使用get方法,这段代码工作正常。 任何人都可以解释为什么会这样 谢谢

1 个答案:

答案 0 :(得分:1)

你检查错了。如果它们存在则设置值。这应该是 -

if(isset($_POST['fname']))
{
    $uname = $_POST['fname'];
}
if(isset($_POST['lname']))
{
    $lnam = $_POST['lname'];
}

<强>更新

if(isset($_POST['fname']) && isset($_POST['lname']))
{
    $uname = $_POST['fname'];
    $lnam = $_POST['lname'];
     $qur = "
        INSERT INTO test(Username, l_name)
        VALUES ('$uname','$lnam')
        ";
}