记录未按升序插入

时间:2012-06-28 14:05:27

标签: php mysql

我遇到了一个奇怪的问题。我有一个带有PHP代码的HTML页面,它将数据插入到MySQL数据库中。数据保存到数据库中没有任何错误,但顺序不正确。

这是截图。右侧的表显示现有记录。前2个记录显示正确。 enter image description here 但是当我保存更多记录时,它会显示如下。 enter image description here enter image description here

即使在MySQL表中,也会以这种方式插入记录。

enter image description here

我不确定问题究竟在哪里,所以我已经显示了下面页面的整个代码。我评论了每个代码块的作用。如果您需要我澄清一些事情,请发表评论。

位置ID 是自动生成的代码。

<html>
<head>
<script language="javascript">
function SelectAll(source)
{   //The code for the 'Select All' checkbox
    checkboxes = document.getElementsByTagName("input");
    for(var i in checkboxes)
    {
        if(checkboxes[i].type == 'checkbox')
        {
            checkboxes[i].checked = source.checked;
        }
    }
}
</script>
</head>
<body>

<?php
//Database connection initialization
require_once("db_handler.php");

$conn = iniCon();
$db = selectDB($conn);

/* Generating the new Location ID */
$query = "SELECT LID FROM locations ORDER BY LID DESC LIMIT 1";
$result = mysql_query($query, $conn);
$row = mysql_fetch_array($result);
$last_id = $row['LID'];

$id_letter = substr($last_id, 0, 1);
$id_num = substr($last_id, 1) + 1;
$id_num = str_pad($id_num, 3, "0", STR_PAD_LEFT);
//$id_num = sprintf("%03d", $id_num);
$new_id = $id_letter . $id_num;

/* Displaying the exsisting locations */        
$query = "SELECT * FROM locations";
$result = mysql_query($query, $conn);

$count = mysql_num_rows($result);

?>

<! The table which displays the existing records >
<div id="display">
<b>Locations</b><br/><br/>
<form name="displayLocs" action="<?php echo $PHP_SELF; ?>" method="post" >
<table border="1">
    <tr>
        <th>Location ID</th>
        <th>Code</th>
        <th>Location</th>
        <th><i>Delete</i></th>
    </tr>
    <?php
    while($row = mysql_fetch_array($result))
    { 
    ?>
    <tr>
        <td align="center"><? echo $row["LID"]; ?></td>
        <td align="center"><? echo $row["Code"]; ?></td>
        <td><? echo $row["Location"]; ?></td>
        <td align="center"><input type="checkbox" name="checkbox[]" value="<? echo $row["LID"]; ?>" /></td>
    </tr>
    <?php
    }
    ?>
</table>

<br/>
    <div id="buttons2">
          <input type="checkbox" onclick="SelectAll(this)" />Select All <input type="reset" value="Clear" /> <input type="submit" value="Delete" name="deletebtn" />
    </div>
</form>
</div>

<! New record saving area >
<b id="loc_caption_1">Enter a new location</b>
<div id="loca">
    <form name="locForm" action="<?php echo $PHP_SELF; ?>" method="post" >
        <table width="300" border="0">
          <tr>
            <td>Location ID</td>
            <td><input type="text" name="lid" readonly="readonly" value="<?php echo $new_id; ?>" style="text-align:right" /></td>
          </tr>
          <tr>
            <td>Code</td>
            <td><input type="text" name="code" style="text-align:right" /></td>
          </tr>
          <tr>
            <td>Location</td>
            <td><input type="text" name="loc" style="text-align:right" /></td>
          </tr>
        </table>
</div>
<br/>
<div id="buttons">
    <input type="reset" value="Clear" /> <input type="submit" value="Save" name="savebtn" />
</div>
    </form>

<?php
//Saving record
if(isset($_POST["savebtn"]))
{
    $id = $_POST["lid"];
    $code = $_POST["code"];
    $location = $_POST["loc"];

    $query = "INSERT INTO locations(LID, Code, Location) VALUES('$id', '$code', '$location')";
    $result = mysql_query($query, $conn);

    if (!$result)
    {
        die("Error " . mysql_error());
    }
    else
    {
        echo "<br/><br/>";
        echo "<strong>1 record added successfully!</strong>";
        echo "<meta http-equiv=\"refresh\" content=\"3;URL=locations.php\">";
    }

    mysql_close($conn);
}

//Deleting selected records
if(isset($_POST["deletebtn"]))
{
    for($i = 0; $i < $count; $i++)
    {
        $del_id = $_POST["checkbox"][$i];
        $query = "DELETE FROM locations WHERE LID = '$del_id' ";
        $result = mysql_query($query, $conn);
    }

    if (!$result)
    {
        die("Error " . mysql_error());
    }
    else
    {
        echo "<meta http-equiv=\"refresh\" content=\"0;URL=locations.php\">";
    }

    mysql_close($conn);
}

?>

</body>
</html>

任何人都可以告诉我是什么导致这种情况以及如何纠正它。

谢谢。

1 个答案:

答案 0 :(得分:6)

数据库中的记录没有特定的顺序存储在数据库中(好吧,它有一些顺序,但由引擎决定它)。如果要以特定顺序获取结果,则需要在查询数据时明确指定结果。在您的情况下,进行此更改:

/* Displaying the exsisting locations */        
$query = "SELECT * FROM locations ORDER BY lid";
$result = mysql_query($query, $conn);