Dude just turn my Insert code into Delete code and it'll work for you. Here is 2 ways that I use my insert code
The code I'm about to give you should not be used if your joining 2 tables using primary keys. You could modify it to be able to use if two tables are joined but this code is for 1 table. There are two ways to do it as far as I know.
Way 1: It finds the missing number from 1,3,4,... and turns the 3 into a 2, then the 4 into the 3. This uses a lot of database resources if the database table holds a lot of data.
Way 2: It inserts the data into the last field of the table. In this case it's 5 because there are only 4 things in the table. It finds the missing number from 1,3,4,... and turns the Primary Key field that was inserted into the table into a 2.
In both of these ways, it resets the primary key autoincrementor.
<?php
$user = "EnterUserNameHere";
$password = "EnterPasswordHere";
$database = "Enter DatabaseNameHere";
$host = "EnterHostNameHere";
$table = "EnterTableNameHere";
$primaryKeyID = "EnterPrimaryKeyIDFieldNameHere";
$name=$POST['txtContactName'];
$email=$POST['txtContactEmail'];
$question=$_POST['txtContactQuestion'];
$insert = "INSERT INTO $table VALUES('NULL','$name','$email','$question')";
mysql_connect($host,$user, $password);
@mysql_select_db($database) or die( "Unable to select database");
///////////////////////////////////////////////
//////////DO NOT EDIT BELOW THIS LINE//////////
///////////////////////////////////////////////
////////WAY 1////////
////////////////////////
/////Primary Key Gap Filler Code/////
$countFunction = "SELECT COUNT('$primaryKeyID') AS num FROM $table";
$data = mysql_query($countFunction) or die(mysql_error());
$row = mysql_fetch_assoc($data);
$numRows = $row['num'];
if ( $numRows > 1 )
{
$currentRow = 1;
$PKIDvalue = $currentRow;
while ($currentRow <= $numRows)
{
$rowIDquery = mysql_query("SELECT FROM $table WHERE $primaryKeyID = $PKIDvalue");
$rowID = mysql_num_rows($rowIDquery);
while ($rowID < 1)
{
$PKIDvalue = $PKIDvalue + 1;
$rowIDquery = mysql_query("SELECT FROM $table WHERE $primaryKeyID = $PKIDvalue");
$rowID = mysql_num_rows($rowIDquery);
if ($rowID != 0)
{
mysql_query("UPDATE $table SET $primaryKeyID=$currentRow WHERE $primaryKeyID = $PKIDvalue");
}
}
$currentRow = $currentRow + 1;
$PKIDvalue = $currentRow;
}
}
$reset = "ALTER TABLE $table AUTO_INCREMENT = 1";
mysql_query($reset);
/////Insert Input into Database/////
mysql_query($insert) or die ("error updating database");
/////Redirect to View Page/////
echo "<meta http-equiv=\"refresh\" content=\"0;URL=../View/View.php\">";
mysql_close();
?>
///////////////////////////////////////////////
//////////DO NOT EDIT BELOW THIS LINE//////////
///////////////////////////////////////////////
////////WAY 2////////
///////////////////////
/////Primary Key Gap Filler Code/////
$countFunction = "SELECT COUNT('$primaryKeyID') AS num FROM $table";
$data = mysql_query($countFunction) or die(mysql_error());
$row = mysql_fetch_assoc($data);
$numRows = $row['num'];
if ( $numRows > 1 )
{
$currentRow = 1;
while ($currentRow <= $numRows)
{
$rowIDquery = mysql_query("SELECT * FROM $table WHERE $primaryKeyID = $currentRow");
$rowID = mysql_num_rows($rowIDquery);
if ($rowID < 1)
{echo $rowID; echo "<br/>";
$query = "SELECT $primaryKeyID FROM $table ORDER BY $primaryKeyID DESC LIMIT 0,1";
$result = mysql_query($query) or die(mysql_error());
$grab = mysql_fetch_assoc($result);
$max = $grab[$primaryKeyID];
echo $max;
mysql_query("UPDATE $table SET $primaryKeyID=$currentRow WHERE $primaryKeyID = $max");
}
$currentRow = $currentRow + 1;
}
}
$reset = "ALTER TABLE $table AUTO_INCREMENT = 1";
mysql_query($reset);
/////Insert Input into Database/////
mysql_query($insert) or die ("error updating database");
/////Redirect to View Page/////
echo "<meta http-equiv=\"refresh\" content=\"0;URL=../View/View.php\">";
mysql_close();
?>