0

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
3
  • Have you tried putting backticks around the tablename, user? It's not a reserved keyword in MySQL I think, but it is in some other SQL based systems, can't hurt to try anyway :) Commented Jan 14, 2012 at 10:27
  • 1
    Can you check that show create table user; is like your create table statement. Commented Jan 14, 2012 at 10:28
  • Im not creating table on runtime. I just showed that it is my table structure. Commented Jan 14, 2012 at 11:26

2 Answers 2

2

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'";
Sign up to request clarification or add additional context in comments.

5 Comments

addslashes() should never be used for this. Instead, mysql_real_escape_string() is the better solution (php.net/manual/en/function.mysql-real-escape-string.php). If magic quotes are on, stripslashes() should be called first before doing the mysql escape.
value for $height is already coming through mysql_real_escape_string()
echo $sql; What do you get?
I think where the problem is. $height value is an array and implode function is used. $height = mysql_real_escape_string(implode("/|!-!|/", $height)); If I echo this out i get "6\' 2"/|!-!|/1.88 m" without implode its output is "Array ( [imperial] => 6' 2" [metric] => 1.88 m )".
If I hard code the value of height I'm still getting the same error.
0

use mysql_real_escape_string

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

1 Comment

Sorry guys this was a mistake from myside. I never looked at what DB was used. Somebody has selected different DB and I though that I was working on actual DB. It has almost same structure except few fields are missing. I'm really very embarrassed after finding that. I think I need a break. Time to go for a beer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.