dtgv52982 2012-06-28 14:05
浏览 5
已采纳

记录未按升序插入

I'm having a strange problem. I have a HTML page with PHP code which inserts data to a MySQL database. The data gets saved to the DB without any errors but in an incorrect order.

Here's a screenshot. The table on the right side displays the existing records. The first 2 records are shown correctly. enter image description here But when I save more records, it displays like this. enter image description hereenter image description here

Even in the MySQL table, the records are inserted that way.

enter image description here

I'm not sure where exactly the problem is so I've shown the whole code for the page below. I've commented what each code block does. Please comment if you need me to clarify something.

The Location ID is an auto-generated code.

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

Can anyone please tell me what is causing this and how to rectify it.

Thank you.

  • 写回答

1条回答 默认 最新

  • dongya6395 2012-06-28 14:09
    关注

    The records in the database are stored in the database in no particular order (well, there's some order to it, but it's up to the engine to determine it). If you want to get the results in a particular order, then you need to explicitly specify it when querying the data. In your case, make this change:

    /* Displaying the exsisting locations */        
    $query = "SELECT * FROM locations ORDER BY lid";
    $result = mysql_query($query, $conn);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度