実際には、idカラムの 'ギャップ'を埋めるとデータベースやシステム全体の参照整合性が損なわれる可能性があるため、実際には何をしているのか非常に注意しなければなりません。テーブルは他のテーブルによって参照されます。
そうでない場合は、簡単な方法は、例えば、最初にMySQLを使用して、既存の行で、これらの「空」のIDを埋めることですテーブルは昇順でidカラムにソートされていることを確認した後、次のようなもので、phpmyadminのを使用して:
SET @count = 0;
UPDATE the_table SET id = @count:= @count + 1;
#Then after this you do your insert operations.
上記の順番にすべての既存のIDを更新します、そしてあなたが挿入する項目は次のようになりますテーブルの総数行に等しいidにauto_incrementedするだけです。
しかし、あなたはそのまま、既に空でないIDを残しておきたい場合は、PHPでこのような何かを行うことができます:自動インクリメント列のギャップを埋める
/* Assume that you want to insert into a table called the_table with columns id, col1, col2, col3 a new row with values for the three cols
$value1, $value2, $value3 respectively, using an existing 'gap' in the id numbering: */
/* Get an array of all present ids: */
$arr = [];
$q1 = mysqli_query($con,"SELECT id from the_table");
while(list($id) = mysqli_fetch_array($q1)){
$arr[] = $id;
}
/* Get the currently largest id in the table as $largest_id */
$q2 = mysqli_query($con,"SELECT MAX(id) from the_table");
list($largest_id) = mysqli_fetch_array($q2);
/* Loop through all integers up to $largest_id + 1 */
/* And do the insert operation just one time, once you find a number not in $arr */
/* Use the $not_yet_inserted variable to break out of the loop */
$not_yet_inserted = true;
for($j = 1; $j <= $largest_id + 1; $j += 1){
if(!in_array($j,$arr) && $not_yet_inserted){
mysqli_query($con,"INSERT INTO the_table (id, col1, col2,col3) values ('$j','$value1','$value3,'$value3'')");
$not_yet_inserted = false;
}
}
これはひどい考えです。代わりに、**あなたの要件が何であるかを説明し、専門家によって作成された自動インクリメントIDでそれを満たす方法についてのヘルプを求める新しい質問**をお願いします。 「あなた自身」を作成することは、初心者ではありません。 – alexis