Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 未按升序插入记录_Php_Mysql - Fatal编程技术网

Php 未按升序插入记录

Php 未按升序插入记录,php,mysql,Php,Mysql,我有一个奇怪的问题。我有一个带有PHP代码的HTML页面,可以将数据插入MySQL数据库。数据保存到数据库时没有任何错误,但顺序不正确 这是一个截图。右侧的表格显示现有记录。前2条记录显示正确。 但当我保存更多记录时,它会显示如下。 即使在MySQL表中,记录也是这样插入的 我不确定问题到底出在哪里,所以我在下面的页面中展示了整个代码。我已经注释了每个代码块的作用。如果你需要我澄清一些事情,请发表评论 位置ID是自动生成的代码 <html> <head> <sc

我有一个奇怪的问题。我有一个带有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>

函数SelectAll(源)
{//“全选”复选框的代码
复选框=document.getElementsByTagName(“输入”);
用于(复选框中的var i)
{
如果(复选框[i]。类型=='checkbox')
{
复选框[i]。选中=源。选中;
}
}
}
位置


数据库中的记录没有特定的顺序存储在数据库中(当然,它有一定的顺序,但由引擎决定)。如果希望按特定顺序获得结果,则需要在查询数据时显式指定它。在您的情况下,进行以下更改:

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

数据库中的记录没有特定的顺序存储在数据库中(当然,它有一定的顺序,但由引擎决定)。如果希望按特定顺序获得结果,则需要在查询数据时显式指定它。在您的情况下,进行以下更改:

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

在SELECT语句中添加ORDERBY子句?记录不会按任何顺序插入-它们只是存在。顺序应用于查询中。另外,生成位置ID的方式是错误的。您应该使用自动递增列,而不是查询最后一条记录。您不能保证记录插入的顺序。表中数据的顺序并不重要!如果您想在输出中排序,只需在相应的SELECT语句中插入ORDERBY子句即可。我对此表示怀疑,因为这是在我实现自动生成ID时才开始出现的。在此之前,即使在数据库表中也可以。现在我明白了。谢谢大家。在SELECT语句中添加ORDER BY子句?记录不会按任何顺序插入-它们只是存在。顺序应用于查询中。另外,生成位置ID的方式是错误的。您应该使用自动递增列,而不是查询最后一条记录。您不能保证记录插入的顺序。表中数据的顺序并不重要!如果您想在输出中排序,只需在相应的SELECT语句中插入ORDERBY子句即可。我对此表示怀疑,因为这是在我实现自动生成ID时才开始出现的。在此之前,即使在数据库表中也可以。现在我明白了。谢谢大家,我明白了。我对此表示怀疑,因为这是在我实现自动生成ID时才开始出现的。在此之前,即使在数据库表中也可以。现在我明白了。添加
orderby
子句解决了显示的问题。谢谢,我明白了。我对此表示怀疑,因为这是在我实现自动生成ID时才开始出现的。在此之前,即使在数据库表中也可以。现在我明白了。添加
orderby
子句解决了显示的问题。非常感谢。