dts777777 2015-03-16 20:57
浏览 63

PHP在重复密钥更新时将数据插入mysql - 添加后缀

want insert a duplicate entry into mysql with a suffix. here is an example:

+-------+--------------+-------+
| ID    | Title        | url   |
+-------+--------------+-------+

+-------+--------------+-------+
| 1     | test         | test1 |
+-------+--------------+-------+
| 2     | test         | test2 |
+-------+--------------+-------+
| 3     | test         | test3 |
+-------+--------------+-------+

means, i want to add auto number as suffix to end of each url that have duplicate title. titles are duplicated but url are kinda unique.

Here is query:

 $i = 0;
    $i++;

    $sql = "INSERT INTO `content` (title, text, cat, url, tag)
    VALUES ('".$_POST["title"]."', '".$_POST["text"]."', '".$_POST["category"]."', '".$_POST["url"]."', '".$_POST["tag"]."') ON DUPLICATE KEY UPDATE url=".$_POST["url"]."+". $i ."";

note: url made dynamically based on title entry. this query not working, what is wrong with my code?

UPDATE: here is error after run query:

error: INSERT INTO content (title, text, cat, url, tag) VALUES ('test', '', '16', 'test', '') ON DUPLICATE KEY UPDATE url=test+1 Unknown column 'test' in 'field list'

  • 写回答

1条回答 默认 最新

  • dsiy62758 2015-03-16 21:14
    关注

    Your code has some strange things. $i = 0; followed by $i++; is just equivalent than doing $i = 1; But then again, what's the purpose on setting a variable that you only use once?

    What you really need to do is:

    1. INSERT query forgetting about the "url" field and removing the ON DUPLICATE...
    2. Get the ID of the last insert. Have a look at the following link to see how to get that value: http://www.w3schools.com/php/php_mysql_insert_lastid.asp

    3. UPDATE query to set the value of "url"

      $_POST["url"].$id_of_last_query
      
    评论

报告相同问题?

悬赏问题

  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法