在mysql数据库中插入未定义的索引

时间:2018-08-12 14:56:41

标签: php html mysql forms

我的代码中的未定义索引,我真的不知道这里的问题在哪里... 这是我的HTML代码

    <?php

$host = "localhost";
$user = "root";
$password = "";
$database = "db_ass";

$con = mysqli_connect($host,$user,$password);

if(!$con)
{
    echo 'Not Connected to Server';
}

if(!mysqli_select_db($con,'db_ass'))
{
    echo 'Database Not Selected';
}

$firstname= $_POST['fname'];
$lastname= $_POST['lname'];
$course= $_POST['course'];
$yr= $_POST['year'];

$sql = "INSERT INTO students (firstname,lastname,course,year) VALUES ('$firstname', '$lastname', '$course','$yr')";

if(!mysqli_query($con,$sql))
{
    echo "Not inserted";
}
else
    {
    echo "Inserted";
}

?>

它总是在第20-23行中说未定义的索引

  $firstname= $_POST['fname'];
    $lastname= $_POST['lname'];
    $course= $_POST['course'];
    $yr= $_POST['year'];

    $sql = "INSERT INTO students (firstname,lastname,course,year) VALUES ('$firstname', '$lastname', '$course','$yr')";

请帮助我T-T

1 个答案:

答案 0 :(得分:-2)

我认为您应该从值规范中的变量中删除逗号。