簡體   English   中英

記錄未按升序插入

[英]Records not getting inserted in ascending order

我有一個奇怪的問題。 我有一個包含PHP代碼的HTML頁面,該頁面可將數據插入MySQL數據庫。 數據被保存到數據庫,沒有任何錯誤,但是順序不正確。

這是屏幕截圖。 右側的表顯示現有記錄。 前2條記錄正確顯示。 在此處輸入圖片說明 但是,當我保存更多記錄時,它顯示如下。 在此處輸入圖片說明在此處輸入圖片說明

即使在MySQL表中,記錄也會以這種方式插入。

在此處輸入圖片說明

我不確定問題出在哪里,所以在下面的頁面中顯示了整個代碼。 我已經評論了每個代碼塊的功能。 如果您需要我進行說明,請發表評論。

位置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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM