dongyu7074 2018-01-23 08:46
浏览 5
已采纳

使用插入mysql表的最后一个数据生成下一个数字

My database has a table named tbltest . in this there is column id that is set to auto increment and another column is named as app_id .

What i want here is i want to generate app_id when someone visit My Form page Called apply.php and submit application form there. is it possible to get the last inserted id in table and get the corresponding app_id generated for that id and generate new app_id on submit the form page.

enter image description here

Now As there is last inserted id is 1 and app_id is IVISA250531 , So, When We Submit the form, it Should generate Next app_id that Should be,

IVISA250532

  • 写回答

2条回答 默认 最新

  • dongyu6276 2018-01-23 09:51
    关注

    you can do:

    $sql = "SELECT * FROM tbltest ORDER BY id DESC LIMIT 1;";
    $result = mysqli_query($conn, $sql);
    if (mysqli_num_rows($result) > 0) {
        $row = mysqli_fetch_assoc($result) ;
    
        $str = $row["app_id"];//"IVISA250532";
        $int = preg_replace('/[^0-9]/', '',$str);//to get the Int from string '250532'
        $pure_str = str_replace($int, "", $str);// tog get only the string word 'IVISA'
    
        $Next_app_id = $pure_str.($int+1); // << that is what you need "IVISA250533"
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效