如何使用MySQL数据库通过PHP制作预订ID?

如何使用MySQL数据库通过PHP制作预订ID?,php,mysql,Php,Mysql,我有这样的案例: JS100829 0001 JS100829 0002 JS=code 10=year 08=month 29=date 0001=the sequence of today first entry 0002=the sequence of today second entry $sequence = mysql_fetch_array(mysql_query("select LatestID from seqtable where Date = CURDATE()"));

我有这样的案例:

JS100829 0001
JS100829 0002

JS=code
10=year
08=month
29=date
0001=the sequence of today first entry
0002=the sequence of today second entry
$sequence = mysql_fetch_array(mysql_query("select LatestID from seqtable where Date = CURDATE()"));
$sequence = $sequence + 1;

$code = "JS";
$mycode = $code . date("ymd") . str_pad($sequence, 4, '0', STR_PAD_LEFT);

mysql_query("insert into seqtable set LatestID = $sequence, Date = CURDATE ON DUPLICATE KEY UPDATE LatestID = $sequence");
    JS=code
    10=year
    08=month
    29=date
    0001=the sequence of today first entry
    0002=the sequence of today second entry
    */
    $js="JS";
    $date=date('ymd');
    for($i=1;$i<=10;$i++)
    {
      $entry=str_pad($i,4,0,STR_PAD_LEFT);
      echo $js.$date.$entry."<br>";
    }
//output
/*
JS1008290001
JS1008290002
JS1008290003
JS1008290004
JS1008290005
JS1008290006
JS1008290007
JS1008290008
JS1008290009
JS1008290010
*/
我需要生成这个代码。有人能帮我吗?
    JS=code
    10=year
    08=month
    29=date
    0001=the sequence of today first entry
    0002=the sequence of today second entry
    */
    $js="JS";
    $date=date('ymd');
    for($i=1;$i<=10;$i++)
    {
      $entry=str_pad($i,4,0,STR_PAD_LEFT);
      echo $js.$date.$entry."<br>";
    }
//output
/*
JS1008290001
JS1008290002
JS1008290003
JS1008290004
JS1008290005
JS1008290006
JS1008290007
JS1008290008
JS1008290009
JS1008290010
*/
谢谢。

由于不知道“序列计数器”的维护位置,这是我的最佳选择(故意冗长):

    JS=code
    10=year
    08=month
    29=date
    0001=the sequence of today first entry
    0002=the sequence of today second entry
    */
    $js="JS";
    $date=date('ymd');
    for($i=1;$i<=10;$i++)
    {
      $entry=str_pad($i,4,0,STR_PAD_LEFT);
      echo $js.$date.$entry."<br>";
    }
//output
/*
JS1008290001
JS1008290002
JS1008290003
JS1008290004
JS1008290005
JS1008290006
JS1008290007
JS1008290008
JS1008290009
JS1008290010
*/

在数据库或配置文件中保留
$sequence
的值。每次生成ID时,保持递增。包含列的数据库表:

  • 日期:日期(也是主键)
  • 最新版本:int
然后,像这样做:

JS100829 0001
JS100829 0002

JS=code
10=year
08=month
29=date
0001=the sequence of today first entry
0002=the sequence of today second entry
$sequence = mysql_fetch_array(mysql_query("select LatestID from seqtable where Date = CURDATE()"));
$sequence = $sequence + 1;

$code = "JS";
$mycode = $code . date("ymd") . str_pad($sequence, 4, '0', STR_PAD_LEFT);

mysql_query("insert into seqtable set LatestID = $sequence, Date = CURDATE ON DUPLICATE KEY UPDATE LatestID = $sequence");
    JS=code
    10=year
    08=month
    29=date
    0001=the sequence of today first entry
    0002=the sequence of today second entry
    */
    $js="JS";
    $date=date('ymd');
    for($i=1;$i<=10;$i++)
    {
      $entry=str_pad($i,4,0,STR_PAD_LEFT);
      echo $js.$date.$entry."<br>";
    }
//output
/*
JS1008290001
JS1008290002
JS1008290003
JS1008290004
JS1008290005
JS1008290006
JS1008290007
JS1008290008
JS1008290009
JS1008290010
*/

你在找这样的东西吗
    JS=code
    10=year
    08=month
    29=date
    0001=the sequence of today first entry
    0002=the sequence of today second entry
    */
    $js="JS";
    $date=date('ymd');
    for($i=1;$i<=10;$i++)
    {
      $entry=str_pad($i,4,0,STR_PAD_LEFT);
      echo $js.$date.$entry."<br>";
    }
//output
/*
JS1008290001
JS1008290002
JS1008290003
JS1008290004
JS1008290005
JS1008290006
JS1008290007
JS1008290008
JS1008290009
JS1008290010
*/
JS=code
10=年
08=月份
29=日期
0001=今天第一个条目的顺序
0002=今天第二个条目的顺序
*/
$js=“js”;
$date=日期('ymd');

例如,对于($i=1;$i),您有一个用于这些预订条目的数据库,这是我的最佳猜测:

    JS=code
    10=year
    08=month
    29=date
    0001=the sequence of today first entry
    0002=the sequence of today second entry
    */
    $js="JS";
    $date=date('ymd');
    for($i=1;$i<=10;$i++)
    {
      $entry=str_pad($i,4,0,STR_PAD_LEFT);
      echo $js.$date.$entry."<br>";
    }
//output
/*
JS1008290001
JS1008290002
JS1008290003
JS1008290004
JS1008290005
JS1008290006
JS1008290007
JS1008290008
JS1008290009
JS1008290010
*/
//sequences
$datenow = date("Y-m-d H:i:s");
//i suppose u has load mysql db somewhere before this code
$q = mysql_query("SELECT COUNT(*) FROM yourtablename WHERE bookingdate = '$datenow'");
//how many squences today
$sequencedtoday = mysql_result($q, 0, 0);
//generate code:
$code = 'JS';
$ymd = date('ymd');
$squence = $squencedtoday+1;
$squence = str_pad($squence,4,0,STR_PAD_LEFT);
//return
echo $code.$ymd.' '.$squence;
//return: JS100829 0001

如果您使用的是MyISAM或BDB引擎,您可以创建一个复杂的自动增量序列。
说明:对于MyISAM和BDB表,可以在多列索引中的辅助列上指定自动增量。在这种情况下,自动增量列的生成值计算为

    JS=code
    10=year
    08=month
    29=date
    0001=the sequence of today first entry
    0002=the sequence of today second entry
    */
    $js="JS";
    $date=date('ymd');
    for($i=1;$i<=10;$i++)
    {
      $entry=str_pad($i,4,0,STR_PAD_LEFT);
      echo $js.$date.$entry."<br>";
    }
//output
/*
JS1008290001
JS1008290002
JS1008290003
JS1008290004
JS1008290005
JS1008290006
JS1008290007
JS1008290008
JS1008290009
JS1008290010
*/
MAX(auto_increment_column) + 1 WHERE prefix=given-prefix.
请记住:当自动增量列被定义为多列索引的最后一列时,确实会重复使用从序列顶部删除的值。

您到底面临着什么问题?我可以告诉您
echo$code、$year、$month、$date、,$sequence;
但我认为这不是您想要听到的如何创建序列id?所以当前日期的自动递增系统总是一样的,让我困惑的是如何生成每天开始的序列的自动递增?所以如果一天发生了变化,它将在0001Hmm之前返回,可能不是您想要的,请参见问题注释。不要使用mysql_num_rows()计算行数!这是mysql的COUNT()作业。