tags:

views:

54

answers:

5

Im trying use the following insert:

mysql_query ("INSERT INTO users (company_name, fname, lname, salt, email, date_added, password) 
                VALUES ('$CompanyName', '$fname', '$lname', '$salt', '$email', '$mysqldate', '$encrypted')")
  or die(mysql_error());

But I get an error:

Unknown column 'company_name' in 'field list'

If I echo out the query, paste it as an SQL statement and run it, it does the insert. All the fields exist, and, as I say, if I echo out the result it works fine.

A: 

Why are your variables enclosed in single quotes? PHP will read those as string. Enclose them in double quotes. But I don't see why you get that error. How about using table_name.column_name instead of just column_name?

mives
It will not. It's enclosed in double quotes, the single quote is just for MySQL. In other word, the single quotes are enclosed in double quotes.
martin.malek
A: 

I don't see any problem with your query, if it matches the schema, it should work. Anyway, try escaping the table and field names, below is the modified query:

msql_query ("INSERT INTO `users` (`company_name`, `fname`, `lname`, `salt`, `email`, `date_added`, `password`)
               VALUES ('$CompanyName', '$fname', '$lname', '$salt', '$email', '$mysqldate', '$encrypted')")
  or die(mysql_error());
jerjer
A: 

Try

mysql_query ("INSERT INTO users (fname, lname, salt, email, date_added, password)
                VALUES ('$fname', '$lname', '$salt', '$email', '$mysqldate', '$encrypted')")
  or die(mysql_error())

If the problem still persists, there is a problem with your connection or your users table.

halocursed
+1  A: 

I see you have mixed case in the $CompanyName variable. Is it possible your MySQL column is also titled in mixed case? Maybe "Company_name" or "Company_Name"?

The Dude
Good point, but if the query cut and pasted into an SQL client works there should not be a case sensitiveness problem...
Manrico Corazzi
A: 

Just a blind guess... are you sure that the mysql_query statement replaces the placeholders with the actual variable values as "echo" does? Try:

mysql_query ("INSERT INTO users (company_name, fname, lname, salt, email, date_added, password) 
            VALUES ('" . $CompanyName . "', '" . $fname . "', '" . $lname . "', '" . $salt. "', '" . $email . "', '" . $mysqldate . "', '" . $encrypted . "')")
or die(mysql_error());
Manrico Corazzi