2017-07-01 14 views
1

データを保存するときに値idを保存しようとしています。私は自動増分オプションでそれを行うことができますが、私はそれをデータベースに使用したくありません。mysqlで増分値を保存できません

でも、最初のレコードを保存することはできますが、それ以降は前のIDを繰り返します。この問題を解決するのを手伝ってください。多くのおかげでPHPで

コントローラ

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']); 
} 

ビュー

function save_vid() 
{ 
    var id = $('#VillageId_save').val(); 
    $.ajax({ 
      type:'POST', 
      dataType:'json', 
      url:'Village/get_lastvillageid', 
      data:{'id':id}, 
      success:function(response){  
       console.log("Data is : "+response); 
      console.log("Data is : "+ JSON.stringify(response)); 
       data = JSON.parse(response); 
       $("#VillageId_save").val(data); 
      } 
    }); 
} 

<input type="hidden" value="<?php 
    foreach ($getLastInserted as $row) {    
     echo $row['VillageId']+1; 
    }?>" name="VillageId_save" id ="VillageId_save"/> 

答えて

0

は、

は、最後のそれを挿入し、 "1" によるMySQLと増分を使用してIDを挿入しなさい。レコードセットがデフォルトで「1」に設定されていない場合。

/* Set DB Connection. */ 
$select_last_id = "select max(id) as auto_inc from table name"; 
$res = mysqli_query($mysqli,$select_last_id); 
if(mysqli_num_rows($res)>0){ 
    $row = mysqli_fetch_array($res); 
    $new_increment_id = intval($row['auto_inc'])+1; 
} 
else{ 
    $new_increment_id = 1; 
} 
+0

私はこれをajaxでやりたかったのです。それ以外の場合は、重複IDが作成されます。 –

+0

したがって、このコードをajaxファイルにも入れます。 –

関連する問題