从插入的数据中获取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

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.

You are not calling $camp->insert(...args...) after making an object of Campaign, in your button code. It should be

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