无法在mysql中保存增量值

时间:2017-07-01 11:00:30

标签: php mysql json ajax codeigniter

我在保存数据时也尝试保存值package main import ( "encoding/json" "fmt" "log" "reflect" ) const s = ` { "localhost": { "tag": "dev_latest", "vhost": "localhost.com" }, "development": { "tag": "dev_latest", "vhost": "dev.com" } } ` func flatten(m map[string]interface{}) map[string]string { mm := make(map[string]string) for k, v := range m { switch reflect.TypeOf(v).Kind() { case reflect.Map: mv := flatten(v.(map[string]interface{})) for kk, vv := range mv { mm[k+"-"+kk] = vv } case reflect.Array, reflect.Slice: for kk, vv := range m { if reflect.TypeOf(vv).Kind() == reflect.Map { mv := flatten(vv.(map[string]interface{})) for kkk, vvv := range mv { mm[k+"-"+kkk] = vvv } } else { mm[k+"-"+kk] = fmt.Sprint(vv) } } default: mm[k] = fmt.Sprint(v) } } return mm } func main() { var m map[string]interface{} err := json.Unmarshal([]byte(s), &m) if err != nil { log.Fatal(err) } b, _ := json.MarshalIndent(flatten(m), "", " ") println(string(b)) } 。我可以使用自动增量选项,但有些原因我不想将其用于数据库。

甚至,我能够保存第一条记录,但之后它会重复以前的ID。请帮我解决这个问题。非常感谢

控制器

id

查看

public function ajax_add()
{
    $this->_validate();
    $data = array(
        'VillageName'=> $this->input->post('VillageName'),
        'VillageCode'=> $this->input->post('VillageCode'),                              
        'VillageId_save'=> $this->input->post('VillageId_save'),                                
        'VillageType'=> $this->input->post('VillageType'),
        'BlockId'=> $this->input->post('BlockId'),
        'CreatedBy' => $this->input->post('CreatedBy'),
    );
    $insert = $this->village->save($data);
    echo json_encode(array("status" => TRUE));
}

public function get_lastvillageid()
{
    $data = $this->village->getLastInserted();
     echo json_encode($data[0]['VillageId']);
}

1 个答案:

答案 0 :(得分:0)

使用PHP,

  

使用mysql获取最后一个插入的id并按“1”递增然后插入它。如果没有记录集默认为“1”。

例如

public double getBatteryCapacity() {
        Object mPowerProfile = null;
        try {
            mPowerProfile = Class.forName("com.android.internal.os.PowerProfile")
                    .getConstructor(Context.class)
                    .newInstance(getContext());
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
// get access to method named "getAveragePower()" in the class "PowerProfile"
            Method getAveragePower = Class.forName("com.android.internal.os.PowerProfile").getMethod("getAveragePower", String.class);
            //Get total battery capacity in mAh.
            double batteryCapacity = (Double) getAveragePower.invoke(mPowerProfile, "battery.capacity");
            return batteryCapacity;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0.0;
    }