-1

我无法插入数据。错误是:

错误:“字段列表”中的未知列“”

它是一个简单的 HTML 表单页面,用户将数据插入数据库 dB。确实需要帮助。我无法得到错误的东西。我是新手。

<?php
$con=mysqli_connect("localhost", "root", "root","dB");
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$enrollno=$_POST['enrollno'];
$fname=$_POST['fname'];
$lname=$_POST['lname'];
$fathername=$_POST['fathername'];
$coursename=$_POST['coursename'];
$yearsem=$_POST['yearsem'];
$facultyno=$_POST['facultyno'];
$hostel=$_POST['hostel'];
$roomno=$_POST['roomno'];
$email=$_POST['email'];
$mobileno=$_POST['mobileno'];
$peradd=$_POST['peradd'];
$district=$_POST['district'];
$state=$_POST['state'];
$amount=$_POST['amount'];
$pwd=$_POST['pwd'];
$tnc=$_POST['tnc'];

$sql="INSERT INTO registration (enrollno, fname, lname, fathername, coursename, yearsem, facultyno, hostel, roomno, email, mobileno, peradd, district, state, amount, pwd, tnc) VALUES ('$enrollno',`$fname`,`$lname`,`$fathername`,`$coursename`,`$yearsem`,`$facultyno`,`$hostel`,`$roomno`,`$email`,`$mobileno`,`$peradd`,`$district`,`$state`,`$amount`,`$pwd`,'$tnc')";

if (!mysqli_query($con,$sql))
  {
  die('Error: ' . mysqli_error($con));
  }
echo "1 record added";

mysqli_close($con);
?>
4

2 回答 2

1

查询中的值INSERT INTO必须用单引号括起来,'而不是反引号,并且不能同时使用两者。反引号只能用于字段和表名。

$sql="INSERT INTO registration (enrollno, fname, lname, fathername, coursename, yearsem, facultyno, hostel, roomno, email, mobileno, peradd, district, state, amount, pwd, tnc) VALUES ('$enrollno','$fname','$lname','$fathername','$coursename','$yearsem','$facultyno','$hostel','$roomno','$email','$mobileno','$peradd','$district','$state','$amount','$pwd','$tnc')";

旁注:您的代码容易受到SQL Injection的攻击。考虑使用带有绑定参数的Prepared Statement,而不是将用户输入连接到查询中。

于 2013-11-03T17:51:12.857 回答
1

您正在对变量使用反引号 [用于列]。使用单引号。试试下面的代码

$sql="INSERT INTO registration (enrollno, fname, lname, fathername, coursename, yearsem, facultyno, hostel, roomno, email, mobileno, peradd, district, state, amount, pwd, tnc) VALUES ('$enrollno','$fname','$lname','$fathername','$coursename','$yearsem','$facultyno','$hostel','$roomno','$email','$mobileno','$peradd','$district','$state','$amount','$pwd','$tnc')";
于 2013-11-03T17:50:27.773 回答