使用PHP增加MySQL数据库的价值

时间:2022-11-27 18:41:02

I have this function but it is not working 100% and I am unsure why. Currently I load the invoice template and it shows 0001.

我有这个功能,但它没有100%工作,我不确定为什么。目前我加载发票模板,它显示0001。

If I add the invoice, it is added to the database and what its meant to do is that if the latest row is not equal to 0001 then make it 0002 and so forth, same if it was 1872 would do next number up from latest invoice in MySQL.

如果我添加发票,它会被添加到数据库中,它的意思是如果最新的行不等于0001,那么将其设为0002等等,如果它是1872,则会从最新发票开始下一个数字在MySQL中。

PHP

define('INVOICE_INITIAL_VALUE','0001');    

// Connect to the database
        $mysqli = new mysqli(DATABASE_HOST, DATABASE_USER, DATABASE_PASS, DATABASE_NAME);

        // output any connection error
        if ($mysqli->connect_error) {
            die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);
        }

        $query = "SELECT invoice FROM invoices ORDER BY invoice DESC LIMIT 1";

        // mysqli select query
        $result = $mysqli->query($query);

        // get number of returned results, if 0 action it below
        $row_cnt = $result->num_rows;

        //Invoice length
        $invoice = $row['invoice'];

        if($row_cnt == "0"){
            echo INVOICE_INITIAL_VALUE;
        } else {
            // check if the initial invoice ID has already been used and if not start from that value else from the next value
            if($invoice != INVOICE_INITIAL_VALUE) {
                echo ++$invoice;
            }
        }

        // Frees the memory associated with a result
        $result->free();

        // close connection 
        $mysqli->close();

Updated like this:

更新如下:

if ($result = $mysqli->query($query)) {

        $row_cnt = $result->num_rows;

        /* fetch object array */
        while ($row = $result->fetch_row()) {
            $invoice = $row['invoice'];
        }

        if($row_cnt == "0"){
            echo INVOICE_INITIAL_VALUE;
        } else {
            // check if the initial invoice ID has already been used and if not start from that value else from the next value
            if($invoice != INVOICE_INITIAL_VALUE) {
                echo ++$invoice;
            }
        }

        // Frees the memory associated with a result
        $result->free();

        // close connection 
        $mysqli->close();
    }

2 个解决方案

#1


the right of doing it would be to convert the string into an integer. and then convert it back with leading 0's. like the following example:

这样做的权利是将字符串转换为整数。然后用前导0转换回来。像下面的例子:

$num = '0001'; // string('0001')
$invoiceNumber = intval($num) + 1; // integer(2)
$invoice = str_pad($invoiceNumber, 4, '0', STR_PAD_LEFT); // string('0002')

so you should change it in your code to:

所以你应该在你的代码中将它改为:

else if($invoice != INVOICE_INITIAL_VALUE) {
    $num = intval($invoice) + 1;
    $invoice = str_pad($num, 4, '0', STR_PAD_LEFT);
    echo $invoice;
}

although you should save the data as integer and just present it with leading zeros.

虽然您应该将数据保存为整数,并将其显示为前导零。

#2


In addition to doing Dan Revah suggested, you should correct your logic of the if statement.

除了Dan Revah建议之外,你应该纠正你的if语句的逻辑。

if($invoice != INVOICE_INITIAL_VALUE) {

In the above line, you are checking if $invoice is not equal to 1, only then you are adding 1 to it.

在上面一行中,您要检查$ invoice是否不等于1,然后才会向其中添加1。

So, when $invoice is equal to 1, you are not adding 1. That's one of the reasons why you are getting 001.

因此,当$ invoice等于1时,您不会添加1.这就是您获得001的原因之一。

#1


the right of doing it would be to convert the string into an integer. and then convert it back with leading 0's. like the following example:

这样做的权利是将字符串转换为整数。然后用前导0转换回来。像下面的例子:

$num = '0001'; // string('0001')
$invoiceNumber = intval($num) + 1; // integer(2)
$invoice = str_pad($invoiceNumber, 4, '0', STR_PAD_LEFT); // string('0002')

so you should change it in your code to:

所以你应该在你的代码中将它改为:

else if($invoice != INVOICE_INITIAL_VALUE) {
    $num = intval($invoice) + 1;
    $invoice = str_pad($num, 4, '0', STR_PAD_LEFT);
    echo $invoice;
}

although you should save the data as integer and just present it with leading zeros.

虽然您应该将数据保存为整数,并将其显示为前导零。

#2


In addition to doing Dan Revah suggested, you should correct your logic of the if statement.

除了Dan Revah建议之外,你应该纠正你的if语句的逻辑。

if($invoice != INVOICE_INITIAL_VALUE) {

In the above line, you are checking if $invoice is not equal to 1, only then you are adding 1 to it.

在上面一行中,您要检查$ invoice是否不等于1,然后才会向其中添加1。

So, when $invoice is equal to 1, you are not adding 1. That's one of the reasons why you are getting 001.

因此,当$ invoice等于1时,您不会添加1.这就是您获得001的原因之一。