我从DB获得DRSYSCODE的值,现在我需要在插入时自动增加这个值
我现在在插入时得到这种格式的值MOB000571,我需要增加到MOB000572,因为这是使用下面的代码,但无法实现它,作为回报,得到的值只有1,请在这里帮助我
$sql1="SELECT DRSYSCODE FROM DOCTORDETAILS ORDER BY DRSYSCODE DESC LIMIT 1";
$query=mysql_query($sql1);
if (!$sql1) { // add this check.
die('Invalid query: ' . mysql_error());
}
$output_array2=array();
while($row=mysql_fetch_assoc($query))
{
$ids=$row['DRSYSCODE'];
}
echo $ids;
if($ids){
$su=1;
echo $num =$ids+$su;
$unique=$num;
}
echo $unique;发布于 2017-09-21 12:02:38
不要使用$num =$ids+$su;,而要使用以下内容:
$num = 'MOB' . str_pad($su + substr($ids, 3), 6, '0', STR_PAD_LEFT);发布于 2017-09-21 11:58:08
就像这样,
$pieces = explode("MOB", $string);
$autoinc_add = 1+$pieces[1];
$your_latest_autoincrement_value = str_pad($autoinc_add, 6, '0', STR_PAD_LEFT);
echo $your_latest_autoincrement_value;发布于 2017-09-21 12:24:10
您的问题是,您正试图将1,一个整数添加到文本字符串中。您需要将文本字符串拆分为文本部分,数字部分首先分解为两个独立的变量(除非“暴徒”总是前三个字符,或者DRSYSCODE中始终有三个字符,后面跟着六个数字),然后将1添加到数字部分,然后在插入之前再次将它们连接在一起。例如,如果DRSYSCODE始终为AAAxxxxxx (“A”为字符,“x”为数字),则可以执行以下操作:
if($ids){
$alpha = substr($ids, 0, 3); // Get the first three characters and put them in $alpha
$numeric = substr($ids, 3, 6); // Get the following six digits and put them in $numeric
$numeric++; // Add one to $numeric
$numeric = str_pad($numeric, 6, '0', STR_PAD_LEFT); // Pad $numeric with leading zeros
$newids = $alpha.$numeric; // Concatenate the two variables into $newids
}我希望这能帮到你。我在您的代码中看到了可以简化它的其他地方,但这不是您问题的一部分。祝你一切顺利。
https://stackoverflow.com/questions/46343127
复制相似问题