简体   繁体   中英

How to generate a next series number and compare first one in php?

code:

if(isset($_POST['submit']))
{
    $number = 1000;
    $number++;
    $unique = strval($number);
    $product_id = "LTC_".$unique;
    $query = "insert into stock(`product_id`)values('".$product_id."')";
    //echo $query;

    $result = mysqli_query($con,$query);
    if($result==true)
    {
        $msg = "<div class='alert alert-success'>Record Save Successfully</div>";
    }
    else
    {
        $msg = "<div class='alert alert-danger'>Unable to Save Please Try Again !!!</div>";
    }
}

In this code I have a variable ie $product_id . Now what am I doing here I am insert product_id value in a table suppose first value of product_id is 1001 next will be 1002 . Now I want to generate a new number in a series way and If I delete any product_id it will never ever repeat again. So, How can I do this ?

try this

$sql = mysqli_query($con,"select max(product_id) as product_id from stock");
$fet = mysqli_fetch_assoc($sql);
$ides = $fet['product_id'];
$number = 1000;
$number++;
$unique = strval($number);
if($unique==$ides || $unique!=$ides)
{
    $new_id = ++$ides;
}
if(isset($_POST['submit']))
{
    $product_id = $new_id;
    //your custom code here
}

$unique数字存储在表中,然后始终从数据库中读取它,然后对其递增,制作新的$product_id然后将$unique的新值存储到该表中。

尝试简单地使用自动递增功能,而不应用任何唯一约束。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM