Re: INSERT INTO using PHP On 8 Nov 2006 14:53:22 -0800, Jerim79 wrote:
> I have been working on PHP script, and was working through some
> problems over at the PHP group. However, I believe my problem now boils
> down to a MySQL problem. The script takes PHP variables and reads them
> into a MySQL database. Here is tthe relevant part of the script:
>
> $query='INSERT INTO review_registration() VALUES($FName, $LName,
> $Company, $Title, $Address, $Apt, $City, $State, $Zip, $Phone, $Fax,
> $Email, $Var1l, $Var2, $Var3, $Var4, $Var5)'
>
> The error message is:
>
> Query failed: Unknown column '$FName' in 'field list'
>
> The table exists, and the varibles are correct. If I put quotation
> marks around each variable inside VALUES, it will actually write the
> variable name into the database. $FName for instance, instead of the
> data that $FName references. However, this demonstrates that the
> database connection is being made, the table is being found, and data
> can be written to it. I am running MySQL 4.0.1.
>
> The thing that sticks out at me is that $FName is not a column name,
> but I don't know why the system thinks that it is. I tried defining the
> columns in review_registration() but I got the same error message. Any
> help will be greatly appreciated.
There's several issues here. You've put the parens for your list of
column names, but not supplied any. Either list the columns or drop the
parens between the table name and the VALUES keyword, and then supply
values for all columns. (MySQL running in non-strict mode will allow you
to supply fewer column names, but will stick default values in the
unlisted columns, even if you didn't tell it to use a default. This may
not be what you want.)
Mor importantly, there's a PHP issue: Your quotes are too strong. Use
ones that allow variable interpretation. You'll also need to quote the
variable values, either internally (deprecated, IMHO), or inside your
query assignment literal text. DB2 and perl will let you get away
without the value quoting, but PHP and MySQL won't.
--
Science is like sex:
sometimes something useful comes out, but that's not why we're doing it.
-- Richard Feynman |