php - 记录未按升序插入

标签 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>

谁能告诉我这是什么原因造成的以及如何纠正它。

谢谢。

最佳答案

数据库中的记录存储在数据库中没有特定的顺序(好吧,它有一些顺序,但由引擎决定)。如果你想得到特定顺序的结果,那么你需要在查询数据时显式指定它。在您的情况下,进行以下更改:

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

关于php - 记录未按升序插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11246424/

相关文章:

php - SQL 语句在 PHP 中不起作用,但在 MySQL 中起作用

php - SELECT count(column) 比 SELECT * 慢

mysql - 错误 SQL 更改表 #1064

mysql - 表 'XXX.dbo.aspnetusers'不存在,迁移VS-Mysql

php - 从另一个数组的键和值创建数组

MySQL - SELECT ...(子查询)AS 字段名

php - - 突然出现而不是子弹

php - Sonar 分析不起作用

php - 如何使用 foreach 进行连续的 mysql 插入?

PHP 猜字游戏(突出显示正确和错误位置的字母 - 就像策划者一样)