我在表单上创建了名为trackingcode
的列,每次您插入新数据时该列都会增加。
现在我考虑添加一个前缀,然后添加数字,所以我要做的是:
$Newtracking = mysqli_fetch_assoc($trackingQuery)['REQ' . 'maxtrack'] + 1 ;
REQ
是我想要的前缀,但是没有用。我做错了吗?
现在其他人说我可以在视图上添加前缀。我不能这样做,我也需要在数据库中插入前缀,其原因是因为我有一个搜索栏,可以从数据库中搜索值。
这是整个插入代码:
$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($connect,"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.'");
';
}
这是我的数据库架构:
我数据库中的当前trackingcode
值:
$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 != '')
{
function generate ($value='',$inp=""){
return $value.str_pad($inp,4, "0", STR_PAD_LEFT);
}
$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(mysqli_query($connect,$query)){
$last_id = mysqli_insert_id($connect);
$code = generate("REQ",$last_id);
mysqli_query($connect,"update request set trackingcode='$code' where reqnumber='$last_id'");
}
答案 0 :(得分:0)
尝试使用str_pad()
插入后,获取last insert id。pass并使用类似于此参数的生成功能
generate(prefix,last_insert_id);
例如:generate("REC",1);//REC0001
function generate ($value='',$inp=""){
return $value.str_pad($inp,4, "0", STR_PAD_LEFT);
}
//Insert query
$query = 'INSERT INTO request(req_date, reqname, date, empname, position, account, platform, processor, ram, monitor, phone, phonetype, headset, req_table, req_chair, approval, status) VALUES("'.$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(mysqli_query($connect,$query)){
$last_id = mysqli_insert_id($connect);
$code = generate("REQ",$last_id);
mysqli_query($connect,"update request set trackingcode='$code' where reqnumber='$last_id'");
//exec code : update tablename set trackingcode='$code' where tableprimary_id='$last_id'
}