Mohammad Abdullah Mohammad Abdullah - 3 months ago 6
MySQL Question

Error: Unknown column '' in 'field list' MySQL error

I am unable to insert data. The error is:


Error: Unknown column '' in 'field list'


It is simpy an HTML form page and data is taken by the user to insert in a database dB.
Help is truly required. I am unable to get the error things. I am a newbie.

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

Answer

You are using backticks[used for columns] for variables. Make use of single quotes. Try the below code

$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')";
Comments