用于使用带有随机数的重复键更新的PHP脚本

时间:2013-11-26 06:06:27

标签: php

我有一个学生表格的PHP脚本。

在重复密钥更新功能上使用d。

我在保存记录后自动分配唯一的主键2用户。

我的查询2分配号码是这样的。

$length=(10000000,99999999);
$random=substr(uniqid,0,$length);

简短的保存和更新查询..

$save="insert into student(Reg_number,username ,password,surname and so on)
VALUES('$random','$username','$password','$surname' nd so on)
ON DUPLICATE KEY UPDATE username='$username' ";

$result=mysql_query($save)or die(mysql_error());

这是我的问题,因为我在保存记录后自动分配随机唯一数字。

哪个工作完美,但是当我尝试更新相同的记录时,会生成一个新的随机数,因此它会保存为新记录。因此,重复的密钥更新永远不会实现,因为随机是新生成的,它将会再次保存。

但如果我指定了身份证号码,那么就说

Insert into student(reg_number, username, password)
Values(1234ac, $username, $password)
On duplicate key update Username = $username.

这个查询工作正常,因为数字是固定的,它看到了当前的记录。但不知道如何使用随机数。

0 个答案:

没有答案