在php中错误INSERT INTO表

时间:2015-01-12 12:43:48

标签: php mysqli

请帮助我,我是php新手。 我试图将数据插入数据库。接受值我用html表格。 提前谢谢..

这是html代码

 <html>
<body>
    <form method = "post" action = "db_using_form.php">
First Name: <input type = "text" name = "fname" id = "fname"><br><br>
Last Name: <input type = "text" name = "lname" id = "lname"><br><br>
<input type = "submit" value = "Add to myDB">

    </form>
</body>
</html>

这里是php代码     

$servername = "localhost"; 
$username = "root";
$password = "asdf";
$dbname = "myDB";

$conn = new mysqli($servername,$username,$password,$dbname);

if($conn->connect_error)
{
    die("Connection Failed: " . $conn->connect_error);
}

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

$sql = "INSERT INTO MyGuest(fname,lname) VALUES('$fname','$lname')";

if($conn->query($sql) === TRUE)
{
    echo "New record inserted.";
}

else
{
    echo "Error " . $sql . "<br>" . $conn->error;
}
?>

错误如下

Error INSERT INTO MyGuest(fname,lname) VALUES('sachin','shelar')
Unknown column 'fname' in 'field list'

1 个答案:

答案 0 :(得分:1)

错误是表示表中没有名为fname的列。只需创建一个名为fname的列,然后重试。