dtukyb8095 2014-02-01 23:50
浏览 150

从插入的数据中获取id

My data is being inserted via ajax and then in the php function I want as soon as data has been inserted the user can click on a preview button, and will be redirected to a dynamic page where data will be pulled from the db based on the id. At the moment the id in the query string is getting nothing.

I have:

   <?php

       class Campaign {

     public $int_campaign_id;


    public function Insert($title,$desc,$cat,$type){

            $_title = $this->db->mysqli->real_escape_string($title);
            $_desc = $this->db->mysqli->real_escape_string($desc);
            $_cat = $this->db->mysqli->real_escape_string($cat);
            $_type = $this->db->mysqli->real_escape_string($type);
            $sql = "INSERT INTO mytable VALUES(NULL,
                     '{$_title}','{$_desc}','{$_cat}','{$_type}', NOW())";

                $result = $this->db->mysqli->query($sql);

                if(!$result)
                {
                        throw new Exception("Query failed!" . $sql . " " . $this->db->mysqli->error);

                }else{
                    $this->int_campaign_id = $this->db->mysqli->insert_id;

            }  

          }
            .....

the button:

     <?php $camp = new Campaign();
                         $campaign_id2 = $camp->int_campaign_id;

                        ?>
                        <a href="<?php echo site_url('business_campaigns/player_preview');?>?id=<?php echo $campaign_id2;?>" name=""  id=""  class="previewBtn2"> 
           </a>

Any help will be greatly appreciated, Mike

  • 写回答

2条回答 默认 最新

  • drd43058 2014-02-02 00:01
    关注

    I don't know what variable name you use for your MySQL connection. But $|connection variable|->insert_id should get the id you just inserted.

    Have you read the PHP documentation?

    I also recommend using prepared statements to prevent SQL injections.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据