我在数据库中有一列名为trackingcode
的列,当它插入数据库时它会生成3个随机数,是否有一种方法或代码可以使它按顺序生成数字? eg. 001, 002 and so on...
我考虑过使其成为数据库的主键,这样我就可以自动对其进行增量,但是如果有一种方法可以通过编码来实现,那就太好了。
这是插入代码:
$connect = mysqli_connect("localhost", "root", "", "pcrequest");
if(isset($_POST["reqname"]))
{
$length = 3;
$trackingcode = 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.'");
';
}
$connect = mysqli_connect("localhost", "root", "", "pcrequest");
if(isset($_POST["reqname"]))
{
$length = 3;
$trackingcode = str_pad($trackingcode, 3, '0', STR_PAD_LEFT);
$trackingcode++;
$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 = '';
答案 0 :(得分:0)
无需更新sql数据库就可以实现类似目的的一种方法是,检索上一个跟踪代码,然后使用类似的东西生成下一个:
@Configuration
class ManagementWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.requestMatchers(
EndpointRequest.to(HealthEndpoint.class, InfoEndpoint.class))
.permitAll().anyRequest().authenticated().and().formLogin().and()
.httpBasic();
}
}
您应该考虑将索引添加到跟踪代码列以提高效率
希望这对您有帮助
答案 1 :(得分:0)
使用它,希望对您有所帮助。
$connect = mysqli_connect("localhost", "root", "", "pcrequest");
if(isset($_POST["reqname"]))
{
$length = 3;
$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 != '')
{
$trackingQuery = mysqli_query($con,"SELECT Max(trackingcode) as maxtrack FROM `request`");
if( mysqli_num_rows($trackingQuery) > 0)
{
$Newtracking = mysqli_fetch_assoc($trackingQuery)['maxtrack'] + 1 ;
}
else{
$Newtracking = 1;
}
$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("'.$Newtracking.'", "'.$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.'");
';
}
}