PHP更新查询:“字段列表”中的未知列

时间:2012-01-14 10:19:20

标签: php mysql

需要帮助。

我很难用PHP更新数据库。它继续给我“'字段列表'中的未知列”错误。我通过PHPMyadmin运行相同的命令,它成功更新了数据。

以下是表格结构:

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;

如果我在列名中插入反向标记

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

我收到此错误

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

如果我插入没有后退

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

我收到此错误

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

如果我使用单一刻度

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

我收到此错误

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

2 个答案:

答案 0 :(得分:2)

高度值的示例可以是6'2"。当您将其插入查询字符串($sql)时,它最终会看起来像:

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

你知道这会弄乱你的弦吗?这搞砸了你的报价。

尝试使用PHP的mysql_real_escape_string()函数:

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

答案 1 :(得分:0)

使用mysql_real_escape_string

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