douchuo0730 2011-05-12 13:31
浏览 13
已采纳

如果名称存在,则保存为名称(1)名称(2),依此类推

There's a mysql database that stores ids and names, when users are creating names with a form they can create existent names since unique ids are the ids such as:

d49f2c32f9107c87c7bb7b44f1c8f297 name
2fa9c810fbe082900975f827e8ed9408 name

what i want to do is saving the second "name" -> "name(1)" when inserting into database.

So far what I've got is this as the idea

lets say the name entered is 'name'

$input = 'name';

select the name we want to check from mysql database

mysql_query(SELECT * FROM  `table` WHERE  `name` =  '$input');

if the result exists, then insert as $input.'(1)'

my question is what if name exists, and name(1) also exists, then how can i add the name(2) there...

  • 写回答

5条回答 默认 最新

  • duanjuhuo8772 2011-05-12 13:36
    关注
    $i = 1;
    $sourceName = $name;
    while( sql "SELECT COUNT(*) FROM table WHERE name = '$name'" ) {
    
      $name = $sourceName.' ('.$i.')';
      $i++;
    }
    

    At this point you have the final $name (with $i counting the iteration)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 怎样才能让鼠标沿着线条的中心线轨迹移动
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?