如何从数据库中同时在两个输入字段上显示相同的序列号

how to display same serial no on two input fields simaltaneously from database

我在同一页上有两个 html 表格,在每个表格的顶部都有一个输入字段,旨在为每个表格提供一个序列号。 我正在使用 phpdesktop chrome 与 html5、php 和 sqlite3 在输入字段中显示来自 orders.db 数据库的 6 位编号 这是我正在使用的代码 forms.php

<?php
session_start();
class DBUtil extends SQLite3 {
function __construct() {
    //Open db
    $this->open("orders.db");
}
function createTableIfNotExists() {
    //create table if not exists already
    $sql = "CREATE TABLE IF NOT EXISTS order_sequence (" . "    next_seq INT NOT NULL" . ")";
    $result = $this->exec($sql);
    return $result; //Whether table created successfully or not

}
function updateNextSeqNumber($nextSeq) {
    //update next sequnce number in db
    $sql = "UPDATE order_sequence SET next_seq = " . $nextSeq;
    $result = $this->exec($sql);
    return $result;
}
function insertFirstTime() {
    //insert first time
    $sql = "INSERT INTO order_sequence (next_seq) VALUES (1)";
    $result = $this->exec($sql);
    return $result;
}
function getNextSeq() {
    //get next sequence number
    $sql = "SELECT next_seq FROM order_sequence";
    $result = $this->query($sql);
    $nextSeq = 1;
    if ($row = $result->fetchArray(SQLITE3_ASSOC)) {
        $nextSeq = $row["next_seq"];
    } else {
        $this->insertFirstTime(); //First sequence number, so that next update queries will be always successfull

    }
    $this->updateNextSeqNumber($nextSeq + 1);
    return $nextSeq;
 }
}
function getNextSequnceNumber() {
//Create new object of utility class
$db = new DBUtil();
if (!$db) {
    die("Connection failed: " . $db->lastErrorMsg());
}
//Check if connected
if ($db->createTableIfNotExists()) {
    $nextSeq = $db->getNextSeq();
    return formatToSixCharacters($nextSeq);
} else {
    die("Error: " . $db->lastErrorMsg());
}
//close connection finally
$db->close();
}
function formatToSixCharacters($num) {
$numStr = $num . "";
if (strlen($numStr) < 6) {
    $zeros = 6 - strlen($numStr);
    for ($i = 1;$i <= $zeros;$i++) {
        $numStr = "0" . $numStr;
    }
 }
 return $numStr;
 }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta content="text/html; charset=utf-8" http-equiv=Content-Type>
<title></title>
</head>
<body>
<!-- form-2 -->
<div class="grid">
    <div class="grid-m1">
        <div class="grid-c1">
            <h2 class="form-no">(form-1)</h2>
            <input id="division-no" class="serial-no division-no" type="text" name="fname" value="<?php echo getNextSequnceNumber() ?>" maxlength="6" size="6">
        </div>
    </div>  
</div>
<!-- form-2 -->
<div class="grid">
    <div class="grid-m1">
        <div class="grid-c1">
            <h2 class="form-no">(form-2)</h2>
            <input id="division-no" class="serial-no division-no" type="text" name="fname" value="<?php echo getNextSequnceNumber() ?>" maxlength="6" size="6">
        </div>
    </div>  
</div>  
</body>
</html>

上面的代码工作正常,除了一个问题,问题是它在 form-1 输入字段中显示 000001,然后加 1 并显示 000002 在 form-2 中,但我希望它在两种形式上显示相同的序列号,例如 000001 在 form-1 上以及 000001 在 form-2 上就是这样! 因为我对 php 和 sqlite 知之甚少,所以我无法弄清楚。 任何帮助将不胜感激。

ps:我不想更改代码的性质,因为每次我单击该页面的 link 时,输入字段中的序列号都会增加一个。

每次调用 getNextSequnceNumber() 时,您都在递增该值。您想将序列号存储在一个变量中一次,然后在多个地方使用该变量。

此外,您的代码设置方式会在每次页面加载时递增。

...
<?php 
$sequenceNumber = getNextSequnceNumber(); 
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta content="text/html; charset=utf-8" http-equiv=Content-Type>
<title></title>
</head>
<body>
<!-- form-2 -->
<div class="grid">
    <div class="grid-m1">
        <div class="grid-c1">
            <h2 class="form-no">(form-1)</h2>
            <input id="division-no" class="serial-no division-no" type="text" name="fname" value="<?php echo $sequenceNumber; ?>" maxlength="6" size="6">
        </div>
    </div>  
</div>
<!-- form-2 -->
<div class="grid">
    <div class="grid-m1">
        <div class="grid-c1">
            <h2 class="form-no">(form-2)</h2>
            <input id="division-no" class="serial-no division-no" type="text" name="fname" value="<?php echo $sequenceNumber; ?>" maxlength="6" size="6">
        </div>
    </div>  
</div>  
</body>
</html>

编辑

<?php
session_start();

class DBUtil extends SQLite3
{
    function __construct()
    {
        //Open db
        $this->open("orders.db");
    }

    function createTableIfNotExists()
    {
        //create table if not exists already
        $sql = "CREATE TABLE IF NOT EXISTS order_sequence (" . "    next_seq INT NOT NULL" . ")";
        $result = $this->exec($sql);
        return $result; //Whether table created successfully or not

    }

    function updateNextSeqNumber($nextSeq)
    {
        //update next sequnce number in db
        $sql = "UPDATE order_sequence SET next_seq = " . $nextSeq;
        $result = $this->exec($sql);
        return $result;
    }

    function insertFirstTime()
    {
        //insert first time
        $sql = "INSERT INTO order_sequence (next_seq) VALUES (1)";
        $result = $this->exec($sql);
        return $result;
    }

    function getNextSeq()
    {
        //get next sequence number
        $sql = "SELECT next_seq FROM order_sequence";
        $result = $this->query($sql);
        $nextSeq = 1;
        if ($row = $result->fetchArray(SQLITE3_ASSOC)) {
            $nextSeq = $row["next_seq"];
        } else {
            $this->insertFirstTime(); //First sequence number, so that next update queries will be always successfull

        }
        $this->updateNextSeqNumber($nextSeq + 1);
        return $nextSeq;
    }
}

function getNextSequnceNumber()
{
    //Create new object of utility class
    $db = new DBUtil();
    if (!$db) {
        die("Connection failed: " . $db->lastErrorMsg());
    }
    //Check if connected
    if ($db->createTableIfNotExists()) {
        $nextSeq = $db->getNextSeq();
        return formatToSixCharacters($nextSeq);
    } else {
        die("Error: " . $db->lastErrorMsg());
    }
    //close connection finally
    $db->close();
}

function formatToSixCharacters($num)
{
    $numStr = $num . "";
    if (strlen($numStr) < 6) {
        $zeros = 6 - strlen($numStr);
        for ($i = 1; $i <= $zeros; $i++) {
            $numStr = "0" . $numStr;
        }
    }
    return $numStr;
}

$sequenceNumber = getNextSequnceNumber();

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta content="text/html; charset=utf-8" http-equiv=Content-Type>
    <title></title>
</head>
<body>
<!-- form-2 -->
<div class="grid">
    <div class="grid-m1">
        <div class="grid-c1">
            <h2 class="form-no">(form-1)</h2>
            <input id="division-no" class="serial-no division-no" type="text" name="fname"
                   value="<?php echo $sequenceNumber; ?>" maxlength="6" size="6">
        </div>
    </div>
</div>
<!-- form-2 -->
<div class="grid">
    <div class="grid-m1">
        <div class="grid-c1">
            <h2 class="form-no">(form-2)</h2>
            <input id="division-no" class="serial-no division-no" type="text" name="fname"
                   value="<?php echo $sequenceNumber; ?>" maxlength="6" size="6">
        </div>
    </div>
</div>
</body>
</html>