动态更改mysql中的自动增量 [英] dynamically change the auto-increment in mysql

查看:152
本文介绍了动态更改mysql中的自动增量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个向表中添加一些数据的表格.我有一个php文件,将数据打印出来.在此页面的每一行中,都打印了一个链接,当您单击该链接时,您将进入一个页面.删除该单行. 该页面称为delete.php 但是有一个问题:当删除一行时,如果我添加另一行,则自动递增将打印错误! 例如,如果您删除第7行,并且打印了13行,那么当您添加另一行时,打印的自动增量列将如下所示: 1,2,...,12,14

I have a form that adds some data to a table.and i have a php file that prints out the data.in this page in each row a link is printed that when you click on it you will proceed to a page that deletes that single row. this page is called delete.php but there is a problem:when a single row is deleted , if i add another row , the auto-increment will be printed wrong! fow example if you delete row 7 and there are 13 rows printed,when you add another row the printed auto-increment column will be something like this: 1,2,...,12,14

这是delete.php的代码:

here's the code of delete.php:

$conn = new mysqli($servername, $username, $password, $dbname);  
    if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
        } 
 else {
    echo "" . mysqli_error($conn);
}

$id=$_GET['id'];


$sql = "DELETE FROM qolak WHERE id='$id'";

if (mysqli_query($conn, $sql)) {
    echo "";
    for($i=$id+1;$i<=1000;$i++){
    $j=$i-1;
    $sql = "UPDATE qolak SET id='$j' WHERE id='$i'";
   // 

    if (mysqli_query($conn, $sql)) {
    echo "";
} else {
    echo "" . mysqli_error($conn);
}
 }
    }
$rownum=$_GET['rownum'];
$rownum+=1;
$ssql = "ALTER TABLE qolak AUTO_INCREMENT ='$rownum'";


    if (mysqli_query($conn, $ssql)) {
    echo "";
} else {
    echo "" . mysqli_error($conn);

}

mysqli_close($conn);

这是我的问题:

$ssql = "ALTER TABLE qolak AUTO_INCREMENT ='$rownum'";

此查询不会动态更改自动增量 请帮忙!!!

this query wont dynamically change the auto-increment please help!!!

推荐答案

所选方法不符合基本问题:

The chosen approach doesn't meet the basic problem:

您必须区分作为ID的自动增量和作为人类可读的编号的输出.将其分开,因为自动增量将永远不会连续,这是设计使然!

You must distinguish between your auto-increment as an ID and your Output as a kind of human readable numbering. Separate this, as auto-increment will never be continuous and that's by design!

绝对不要尝试手动更改自动增量值.期间.

如果您希望具有一些连续的行编号,请在html上添加一个额外的列(由于数据库中没有行号",因此不在数据库中!)

If you want to have some continuous row numbering, add an extra column on your html (NOT in the database as there are no "row numbers" in there!)

使用ID = 13删除第7行将导致第8行成为新的7,依此类推.

Deleting the row #7 with ID = 13 will then cause row #8 to become the new 7 and so on.

这篇关于动态更改mysql中的自动增量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆