【问题标题】:Insert a SET element into mysql db table with PHP使用 PHP 将 SET 元素插入 mysql db 表
【发布时间】:2014-06-29 15:37:45
【问题描述】:

假设我的 mysql 数据库中有一个名为 Film 的表。它有一个 film_id 是 id 列和 special_features 列是 SET 类型。以前从未使用过 SET 类型。 Special_features 列的类型为 set('Trailers', 'Commentaries', 'behind'),

我想在表格中输入这一行:

SET("trailers", "commentaires", "behind the scenes");

我的基本 sql 命令在 php 中应该是什么样子?我正在使用 MySQLi,我想要基本的 sql 字符串。

$sql = "INSERT INTO (special_features) VALUES (('trailers', 'commentaires', 'behind the scenes'))";

如果我只想插入预告片和评论,我的 sql 字符串会是什么样子?

这只是一个例子,不知道是否正确?

【问题讨论】:

  • 没人明白吗? special_features 是一列,它最多可以接受以下三个值的集合:“预告片”、“评论”、“幕后花絮”、
  • 如果你投反对票,应该有理由投反对票吗?

标签: php mysql sql mysqli


【解决方案1】:

我想应该是这样的:

$sql = "INSERT INTO set_field(special_features) VALUES('trailers,commentaires')";

这是另一个包含所有 Film 表列的示例:

INSERT INTO `film`(`title`, `description`, `release_year`, `language_id`, `original_language_id`, `rental_duration`, `rental_rate`, `length`, `replacement_cost`, `rating`, `special_features`) VALUES ('blade','cutting vampires','1998',1,1,0.4,4.99,120,60,0.8, ('Trailers,Commentaries,Deleted Scenes,Behind the Scenes'))

但是,您必须小心使用逗号分隔的值。之间不应有空格,并且所有值必须与创建表时完全匹配(所有字符大写和小写)。

【讨论】:

    【解决方案2】:

    使用这个将数据插入mysql $sql = "INSERT INTO special_features (row1,row2,row3) VALUES ('trailers', 'commentaires', 'behind the scene')";

    【讨论】:

    • 其实 SET 数据类型是一列,而不是三列。
    【解决方案3】:

    如果我没记错的话,您想向数据库中添加值,但是您需要插入相同数量的参数。 现在您的查询正在寻找 1 个参数以存储在 special_futures 列中,但您正在传递 3 个值。

    这样的东西会更合适。

    $sql = "INSERT INTO (trailers, commentaires, behindthescenes) VALUES ("$trailers",     "$commentaires", "$behind the scenes")";
    

    【讨论】:

      猜你喜欢
      • 2012-07-27
      • 1970-01-01
      • 2012-09-27
      • 2020-05-08
      • 1970-01-01
      • 2011-01-25
      • 1970-01-01
      • 2018-07-26
      • 1970-01-01
      相关资源
      最近更新 更多