主键增加不是有序的

时间:2013-12-29 13:20:18

标签: php mysql

我有一个卡名列表和一个插入卡的php文件。当我插入一张卡并将其删除在数据库中,然后再次插入。卡删除后主键增加。例如:

Card number 1
Card number 2
Card number 3 --> if i delete this value and inserte again the primary key is 4 not 3 how to fix that problem ?

这是我的代码

    <?php
// this file show card name and picture
    include("connect.inc");
    $connect=mysqli_connect($host,$username,$password,$dbname) or die ("can't connect to server");
    $query="SELECT * FROM dragon ";
    $result=mysqli_query($connect,$query) or die("can't execute query");
    echo $_SESSION['count'];
    echo "<hr/>";

    while($row=mysqli_fetch_assoc($result))
    {
        extract($row);
        echo $type."<br/>";
        echo $CardName."/";
        echo $Description;
        echo "<br/>";
        echo "<a href='../dragon/{$picture}' border='0'> <img src='../dragon/{$picture}' border='0' width='300' height='300'/></a>";

        echo "<hr/>";

    }


    ?>

此文件显示插入表单

<?php
$labels=array("type"=>"type",
                "CardName"=>"Card Name",
                "Description"=>"Description",
                "atk"=>"Attack",
                "def"=>"Defend",
                "picture"=>"picture");

    echo "<form action='InsertCard.php' method='POST'>";
    echo "<h2>Insert new card </h2>";
    foreach($labels as $keys =>$values)
    {
        echo "$values <input type='text' name='$keys'/><br/>";
    }
    echo "<input type='submit'  value='insert new cards'/>";
    echo "<input type='submit' name='return' value='return'/>";
    echo "</form>";

?>

此文件处理插入的文件

<?php
$labels=array("type"=>"type",
                "CardName"=>"Card Name",
                "Description"=>"Description",
                "atk"=>"Attack",
                "def"=>"Defend",
                "picture"=>"picture");

if(@isset($_POST['return']))
{
    header("Location:ShowCatalog.php");
}
include("connect.inc");
$connect=mysqli_connect($host,$username,$password,$dbname) or die("can't connect to server");

foreach($_POST as $keys =>$values)
{
    if(empty($values))
    {
        if($keys=='type' or $keys=='CardName' or $keys=='Description' or $keys=='picture')
        {
            $empty_values[]=$keys;
        }
    }
    else
    {
        if($keys=='type')
        {
            if(!preg_match("/^[A-Za-z -]{4,15}$/",$values))
            {
                $invalid_data[]=$keys;
            }
        }
        elseif($keys=='CardName')
        {
            if(!preg_match("/^[A-Za-z -]{4,30}$/",$values))
            {
                $invalid_data[]=$keys;
            }
        }
        elseif($keys=='Description')
        {
            if(!preg_match("/^[A-Za-z., -]{4,255}$/",$values))
            {
                $invalid_data[]=$keys;
            }
        }
        elseif($keys=="atk" or $keys=="def")
        {
            if(!preg_match("/^[0-9]{3,5}$/",$values))
            {
                $invalid_data[]=$keys;
            }
        }
        elseif($keys=='picture')
        {
            if(!preg_match("/^[A-Za-z -]{4,30}(.jpg)$/",$values))
            {
                $invalid_data[]=$keys;
            }
        }
        /*else
        {
            $clean_data[$keys]=trim(strip_tags($values));
        }*/
    }
}

if(@sizeof($empty_values)>0 or @sizeof($invalid_data)>0)
{
    if(@sizeof($empty_values)>0)
    {
    $join=join(", ",$empty_values);
    $msg="You forgot to input: $join<br/>";
    echo $msg;
    }
    if(@sizeof($invalid_data)>0)
    {
    $join=join(", ",$invalid_data);
    $msg="Invalid data: $join";
    echo $msg;
    }
    echo "<form action='$_SERVER[PHP_SELF]' method='POST'>";
    echo "<h2>Insert new card </h2>";
    foreach($labels as $keys =>$values)
    {
        echo "$values <input type='text' name='$keys'/><br/>";
    }
    echo "<input type='submit'  value='insert new cards'/>";
    echo "<input type='submit' name='return' value='return'/>";
    echo "</form>";
    exit();
}

foreach($_POST as $keys =>$values)
{
    $queried_data[$keys]=mysqli_real_escape_string($connect,trim(strip_tags($values)));
}
$check_existence="SELECT CardName FROM dragon WHERE CardName=";
foreach($queried_data as $keys =>$values)
{
    if($keys=="CardName")
    {
        $check_existence.="'".$values."'";
    }
}
$checking_result=mysqli_query($connect,$check_existence)or die("can't execute query ".mysqli_error($connect));
    if(mysqli_affected_rows($connect)>0)
    {
        echo "card is already existed !";
        include("ShowForm.php");
        exit();
    }
    else
    {
        $query="INSERT INTO dragon(";
        foreach($queried_data as $keys =>$values)
        {
            $query.=$keys.",";
        }
        $query.=")";
        $query=preg_replace("/\,\)/",")",$query);
        $query.="VALUES(";
        foreach($queried_data as $keys =>$values)
        {
            if($keys=="type" or $keys=="CardName" or $keys=="Description")
            {
                $values=ucfirst($values);
            }
            if($keys=="atk" or $keys=="def")
            {
                if(empty($values))
                {
                    $values='n/a';
                }
            }
            $query.="'".$values."',";   
        }
        $query.=")";
        $query=preg_replace("/\,\)/",")",$query);
        $result=mysqli_query($connect,$query);
        echo "card is inserted !";
    }
?>

3 个答案:

答案 0 :(得分:2)

这是预期的行为,换句话说,现在是人工智能的。不要指望ID是连续的,你应该自己跟踪它,以备不时之需。使用mysqli完成插入后,如果需要稍后参考,可以在查询完成后获取“插入ID”。

有关获取插入ID的更多信息,请参阅: http://www.php.net/manual/en/mysqli.insert-id.php

答案 1 :(得分:2)

这不是问题。使用自动增量主键时,分配给输入的键是尚未分配的最低正值。这在很多方面都很有用。例如,您有另一个带有外键的表。如果您的设计不好,您可以从此表中删除一行并保留一个孤立的外键。这是一个容易检测到的错误,但如果插入另一行时,它会获取已删除行的键,那么外键将指向其他数据。这个错误很难检测到。因此,问题不在于如何分配密钥,而在于您的期望。您可以考虑询问您想要达到的目标,以便为您的工作找到有用的答案。

答案 2 :(得分:1)

我发现你没有发布任何与删除查询相关的代码,所以如果你手动使用sql中的删除,请使用以下代码。

ALTER TABLE dragon AUTO_INCREMENT = 1

这将重置自动递增,并且sql将插入下一个最高整数。

请参阅https://stackoverflow.com/a/8923132/2401804