自动生成序列号 [英] Auto generate serial numbers

查看:203
本文介绍了自动生成序列号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面编写了计数代码,目的是使它为我的数据生成自动序列号,以代替一旦删除该行即会产生的MySQL序列号.但是,当我运行它时,没有观察到对MySQL表的任何输入.后来我将代码更改为Dreamweaver插入记录,并且在那里我发现SN(序列号)字段不必为NULL.

I wrote the count code below with the aim of having it produce auto-serial numbers for my data in place of the MySQL serial number which goes once the row is deleted. But when I run it, I observed no entries where made to the MySQL table. I later changed the codes to Dreamweaver Insert Record and there I observed that the SN (Serial Number) fields needed not to be NULL.

通过要求我在发布前在SN字段中输入值,这意味着以下行代码:"$ query = ......"和"$ sn = ......"将不会生成预期的值

By asking me to enter value to the SN field before posting it means this line code: "$query=......" and "$sn=......" will not generate the values expected of it.

因此,原则上,我需要有关如何从1到将要创建的尽可能多的数据生成自动序列号的帮助.

So in principle I need help on how to generate an auto serial number from 1 to as many datas as will be created.

<?php
if(isset($_POST['submit']))
{
    $query  = mysql_query("SELECT COUNT(*) as counter FROM tbl_donors");
    $sn     = mysql_num_rows($query) + 1;
    $donorname      = $_POST['donorname'];
    $designation    = $_POST['designation'];
    $address        = $_POST['address'];
    $city       = $_POST['city'];
    $state      = $_POST['state'];
    $country        = $_POST['country'];
    $phone      = $_POST['phone'];
    $emailaddr      = $_POST['emailaddr'];
    $user_name      = $_POST['user_name'];

    mysql_select_db ($database_xxxxxxxxxx,$xxxxxxxxxx);
   $query = ("INSERT INTO tbl_donors (donor_id, sn, donorname, designation, address , city, state, country , phone, emailaddr, user_name) VALUES ('', '$sn', '$donorname' , '$designation', '$address', '$city' , '$state', '$country', '$phone, '$emailaddr', '$user_name')");
    $results = mysql_query($query,$xxxxxxxxxx) or die
    ("Could not execute query : $query." . mysql_error());
    mysql_close();
}  
?>

推荐答案

您需要配置MySQL表,以使sn列启用AUTO_INCREMENT.考虑来自 AUTO_INCREMENT上的MySQL 5.0参考的该示例:

You need to configure your MySQL table so that the sn column has AUTO_INCREMENT enabled. Consider this example from the MySQL 5.0 Reference on AUTO_INCREMENT:

CREATE TABLE animals (
 id MEDIUMINT NOT NULL AUTO_INCREMENT,
 name CHAR(30) NOT NULL,
 PRIMARY KEY (id)
) ENGINE=MyISAM;

INSERT INTO animals (name) VALUES
 ('dog'),('cat'),('penguin'),
 ('lax'),('whale'),('ostrich');

SELECT * FROM animals;

返回:

+----+---------+
| id | name    |
+----+---------+
|  1 | dog     |
|  2 | cat     |
|  3 | penguin |
|  4 | lax     |
|  5 | whale   |
|  6 | ostrich |
+----+---------+

在此示例中,没有任何内容作为id传递,但结果是自动生成的.

In this example, nothing was passed as id, but the result was automatically generated.

您应该为此表中的sn列定义一些链接.

You should define something link this for the sn column in your table.

假设您绝对必须感觉自己有一排额外的,偏移的,递增的列,一种选择是使其出现 ,以便在查询表时使用.在上面的示例中,假设我们要增加一列sn,并且idsn之间的差是7.我们可以执行以下操作:

Assuming you absolutely must feel like you have an additional, offset, incrementing column, one option is to make it appear so when you query the table. In the example above, let's say we want an additional column, sn, and that the difference between id and sn is 7. We could do the following:

SELECT *, (`id` + 7) as `sn` FROM animals;

返回:

+----+---------+----+
| id | name    | sn |
+----+---------+----+
|  1 | dog     |  8 |
|  2 | cat     |  9 |
|  3 | penguin | 10 |
|  4 | lax     | 11 |
|  5 | whale   | 12 |
|  6 | ostrich | 13 |
+----+---------+----+

请注意,尽管您可以在JOIN和其他语句中使用sn,但是您将无法在其他表中将其用作外键.

Note that although you can use sn in JOINs and other statements, you won't be able to use it as a foreign key in other tables.

这篇关于自动生成序列号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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