Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何发现删除后的最后一个id并设置新的自动增量mysql数据行_Php_Mysql_Codeigniter - Fatal编程技术网

Php 如何发现删除后的最后一个id并设置新的自动增量mysql数据行

Php 如何发现删除后的最后一个id并设置新的自动增量mysql数据行,php,mysql,codeigniter,Php,Mysql,Codeigniter,我有一个问题,我在两个表中按id从mysql中删除数据,但在sales表中,我希望在删除后保持相同的id。当我添加新销售时,保留上次删除的id 例如,我在删除时有销售id nr 100,在创建新销售时,id将为101。 如果我删除,我想保留100 这是我基于codeigniter的模型: public function getAllInvoiceItems($sale_id) { $q = $this->db->get_where('sale_items',

我有一个问题,我在两个表中按id从mysql中删除数据,但在sales表中,我希望在删除后保持相同的id。当我添加新销售时,保留上次删除的id

例如,我在删除时有销售id nr 100,在创建新销售时,id将为101。 如果我删除,我想保留100

这是我基于codeigniter的模型:

public function getAllInvoiceItems($sale_id) 
    {
        $q = $this->db->get_where('sale_items', array('sale_id' => $sale_id));
        if($q->num_rows() > 0) {
            foreach (($q->result()) as $row) {
                $data[] = $row;
            }

            return $data;
        }
    }

    public function getInvoiceByID($id)
        {

            $q = $this->db->get_where('sales', array('id' => $id), 1); 
              if( $q->num_rows() > 0 )
              {
                return $q->row();
              } 

              return FALSE;

        }

        public function deleteInvoice($id) 
        {
            $inv = $this->getInvoiceByID($id);
            $warehouse_id = $inv->warehouse_id;
            $items = $this->getAllInvoiceItems($id);

            foreach($items as $item) {
                $product_id = $item->product_id;
                $item_details = $this->getProductQuantity($product_id, $warehouse_id);
                $pr_quantity = $item_details['quantity'];
                $inv_quantity = $item->quantity;
                $new_quantity = $pr_quantity + $inv_quantity;

                $this->updateQuantity($product_id, $warehouse_id, $new_quantity);

            }

            if($this->db->delete('sale_items', array('sale_id' => $id)) && $this->db->delete('sales', array('id' => $id))) {
                return true;
            }
        return FALSE;
        }

首先,只要在insert语句中声明该列,就可以始终使用AI覆盖具有自己(唯一)值的列。第二,如果AI为200,而你删除了100,你会怎么做?仍然希望下一个销售项目为100?首先获取最后一个插入id,然后通过传递“id”字段将其递增1,并通过查询插入新id。您是正确的,但是,即使我删除100,最后一个是200,脚本也需要从最后一个id自动递增:我的意思是,如果我删除100,将有99-101,然后200>201。谢谢你的目标是一个没有差距的主键?主键ID中存在间隙是正常的。