Bhaskar Wankhede
Bhaskar Wankhede

Reputation: 69

PHP Update query : Unknown column in 'field list'

Need help.

I'm having hard time trying to update the database with PHP. It keeps on giving me "Unknown column in 'field list'" error. I have run the same command through PHPMyadmin and it is successfully updating the data.

Below is the table structure:

CREATE TABLE `user` (
  `uid`              int AUTO_INCREMENT NOT NULL,
  `name`            text,
  `photo_localurl`  text,
  `birthday`        text,
  `nickname`        text,
  `height`          text,
  `lastupdate`      timestamp,
  PRIMARY KEY (`uid`)
) ENGINE = InnoDB;

if I insert with back-tick in column name

$sql = "UPDATE user SET `height` = '$height' WHERE uid = '$uid'";

i get this error

UPDATE user SET `height` = '6\' 2"/|!-!|/1.88 m' WHERE uid = '51' Unknown column 'height' in 'field list'

if i insert without back-tick

$sql = "UPDATE user SET height = '$height' WHERE uid = '$uid'";

I get this error

UPDATE user SET height = '6\' 2"/|!-!|/1.88 m' WHERE uid = '51' Unknown column 'height' in 'field list'

if I use single tick

$sql = "UPDATE user SET 'height' = $height WHERE uid = $uid";

I get this error

UPDATE user SET 'height' = 6\' 2"/|!-!|/1.88 m WHERE uid = 51 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''height' = 6\' 2"/|!-!|/1.88 m WHERE uid = 51' at line 1

Upvotes: 0

Views: 2948

Answers (2)

Pradeep Singh
Pradeep Singh

Reputation: 3634

use mysql_real_escape_string

  $sql = "UPDATE `user`.`userit` SET `userit`.`height` = '" . mysql_real_escape_string($height) . "' WHERE uid = '$uid'";

Upvotes: 0

Ayman Safadi
Ayman Safadi

Reputation: 11552

An example of a height value could be 6'2". When you insert it into the query string ($sql), it ends up looking something like:

$sql = "UPDATE user SET height = '6'2"' WHERE uid = '8'";

You see how this messes up your string right? It's screwing up your quotes.

Try using PHP's mysql_real_escape_string() function:

$sql = "UPDATE user SET height = '" . mysql_real_escape_string($height) . "' WHERE uid = '$uid'";

Upvotes: 2

Related Questions