-1
<?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.

this code works fine if i use get method . can anyone explain why this is so thanks

Vish
  • 1
  • 1

1 Answers1

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')
        ";
}
Sougata Bose
  • 31,517
  • 8
  • 49
  • 87