Unix Technical Forum

INSERT INTO using PHP

This is a discussion on INSERT INTO using PHP within the MySQL forums, part of the Database Server Software category; --> I have been working on PHP script, and was working through some problems over at the PHP group. However, ...


Go Back   Unix Technical Forum > Database Server Software > MySQL

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 09:31 AM
Jerim79
 
Posts: n/a
Default INSERT INTO using PHP

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.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 09:31 AM
Peter H. Coffin
 
Posts: n/a
Default 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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 09:31 AM
strawberry
 
Posts: n/a
Default Re: INSERT INTO using PHP


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.


Try this:

$query="INSERT INTO `review_registration` VALUES('$FName', '$LName',
'$Company', '$Title', '$Address', '$Apt', '$City', '$State', '$Zip',
'$Phone', '$Fax',
'$Email', '$Var1l', '$Var2', '$Var3', '$Var4', '$Var5');";

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-28-2008, 09:31 AM
Geoff Muldoon
 
Posts: n/a
Default Re: INSERT INTO using PHP

mylek@hotmail.com says...
> 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 PHP Parser will not attempt to parse strings encapsulated in single
quotes.

Use double-quotes instead, or escape+concatenate the variables.

$query="INSERT INTO review_registration() VALUES($FName, $LName,
$Company, $Title, $Address, $Apt, $City, $State, $Zip, $Phone, $Fax,
$Email, $Var1l, $Var2, $Var3, $Var4, $Var5)";

or

$query='INSERT INTO review_registration() VALUES('.$FName.', '.$LName.',
'.$Company.', '.$Title.', '.$Address.', '.$Apt.', '.$City.', '.$State.',
'.$Zip.', '.$Phone.', '.$Fax.', '.$Email.', '.$Var1l.', '.$Var2.', '.
$Var3.', '.$Var4.', '.$Var5.')';

Geoff M

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-28-2008, 09:31 AM
Pedro Graca
 
Posts: n/a
Default Re: INSERT INTO using PHP

Jerim79 wrote:
> $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 error is a PHP error.
Expand your error message:

<?php
// ...
$res = mysql_query($query);
if (!$res) {
# die('Query failed: ' . mysql_error()); // simple version
die('Query failed: ' . mysql_error() .
' The SQL statement was: ' . $query); // expanded version
}


--
I (almost) never check the dodgeit address.
If you *really* need to mail me, use the address in the Reply-To
header with a message in *plain* *text* *without* *attachments*.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 02-28-2008, 09:32 AM
Jerim79
 
Posts: n/a
Default Re: INSERT INTO using PHP


Pedro Graca wrote:
> Jerim79 wrote:
> > $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 error is a PHP error.
> Expand your error message:
>
> <?php
> // ...
> $res = mysql_query($query);
> if (!$res) {
> # die('Query failed: ' . mysql_error()); // simple version
> die('Query failed: ' . mysql_error() .
> ' The SQL statement was: ' . $query); // expanded version
> }
>
>
> --
> I (almost) never check the dodgeit address.
> If you *really* need to mail me, use the address in the Reply-To
> header with a message in *plain* *text* *without* *attachments*.


It is working now. The double quotes was the fix. Thanks for all the
help. (I still have the parse the phone numbers to get them to enter
right.)

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 05:28 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com