我在数据库中创建了一个名为trackingcode
的字段,这是它生成的代码的示例:
现在,我想在生成诸如此类的内容时开始使用TS-001
...
示例:
TS-001
TS-002
TS-003
TS-004
SO ON...
我不确定从哪里开始,我尝试将trackingcode
设置为TS-000,然后将mysql列更改为AUTO_INCREMENT,但是没有用。
无论如何,这是我生成随机数的默认代码:
<?php
//insert.php
$connect = mysqli_connect("localhost", "root", "", "pcrequest");
if(isset($_POST["reqname"]))
{
$length = 3;
$trackingcode = 'TS-' . substr(str_shuffle("0123456789"), 0, $length);
$req_date = $_POST["req_date"];
$reqname = $_POST["reqname"];
$date = $_POST["date"];
$empname = $_POST["empname"];
$position = $_POST["position"];
$account = $_POST["account"];
$platform = $_POST["platform"];
$processor = $_POST["processor"];
$ram = $_POST["ram"];
$monitor = $_POST["monitor"];
$phone = $_POST["phone"];
$phonetype = $_POST["phonetype"];
$headset = $_POST["headset"];
$req_table = $_POST["req_table"];
$req_chair = $_POST["req_chair"];
$approval = $_POST["approval"];
$status = $_POST["status"];
$query = '';
for($count = 0; $count<count($reqname); $count++)
{
$req_date_clean = mysqli_real_escape_string($connect, $req_date[$count]);
$reqname_clean = mysqli_real_escape_string($connect, $reqname[$count]);
$date_clean = mysqli_real_escape_string($connect, $date[$count]);
$empname_clean = mysqli_real_escape_string($connect, $empname[$count]);
$position_clean = mysqli_real_escape_string($connect, $position[$count]);
$account_clean = mysqli_real_escape_string($connect, $account[$count]);
$platform_clean = mysqli_real_escape_string($connect, $platform[$count]);
$processor_clean = mysqli_real_escape_string($connect, $processor[$count]);
$ram_clean = mysqli_real_escape_string($connect, $ram[$count]);
$monitor_clean = mysqli_real_escape_string($connect, $monitor[$count]);
$phone_clean = mysqli_real_escape_string($connect, $phone[$count]);
$phonetype_clean = mysqli_real_escape_string($connect, $phonetype[$count]);
$headset_clean = mysqli_real_escape_string($connect, $headset[$count]);
$req_table_clean = mysqli_real_escape_string($connect, $req_table[$count]);
$req_chair_clean = mysqli_real_escape_string($connect, $req_chair[$count]);
$approval_clean = mysqli_real_escape_string($connect, $approval[$count]);
$status_clean = mysqli_real_escape_string($connect, $status[$count]);
if($req_date_clean != '' && $reqname_clean != '' && $date_clean != '' && $empname_clean != '' && $position_clean != '' && $account_clean != '' && $platform_clean != '' && $processor_clean != '' && $ram_clean != '' && $monitor_clean != '' && $phone_clean != '' && $phonetype_clean != '' && $headset_clean != '' && $req_table_clean != '' && $req_chair_clean != '' && $approval_clean != '' && $status_clean != '')
{
$query .= '
INSERT INTO request(trackingcode, req_date, reqname, date, empname, position, account, platform, processor, ram, monitor, phone, phonetype, headset, req_table, req_chair, approval, status)
VALUES("'.$trackingcode.'", "'.$req_date_clean.'", "'.$reqname_clean.'", "'.$date_clean.'", "'.$empname_clean.'", "'.$position_clean.'", "'.$account_clean.'", "'.$platform_clean.'", "'.$processor_clean.'", "'.$ram_clean.'", "'.$monitor_clean.'", "'.$phone_clean.'", "'.$phonetype_clean.'", "'.$headset_clean.'", "'.$req_table_clean.'", "'.$req_chair_clean.'", "'.$approval_clean.'", "'.$status_clean.'");
';
}
}
if($query != '')
{
if(mysqli_multi_query($connect, $query))
{
echo 'Successfuly Sent!';
}
else
{
echo 'Error';
}
}
else
{
echo 'All fields are required!';
}
}
?>
答案 0 :(得分:0)
为什么不简单地使用具有自动增量的经典索引,并在id之前使用PHP甚至更好的MySQL设置字符串“ TS-”?
db-fiddle working example here
在对话框中的以下评论中,编辑:
SELECT CONCAT("TS-", LPAD(id_test, CHAR_LENGTH((SELECT MAX(id_test) FROM TEST)), '0')) AS trackingcode FROM TEST
db-fiddle working example of an adaptative solution
因此,这是实现目标的最快,最干净的方法。
当您的行数少于10行时,tracking_codes将为“ TS-01”,“ TS-02”等...如果您的行数少于100行,则它们将为“ TS-001”,TS -002“等...