1

i have two html forms on same page and on top of each form there is an input field that is intended to give each form a serial no. i am using phpdesktop chrome with html5, php, and sqlite3 to show a 6 digit no from orders.db database in an input field here is my code that i am using 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>

and this above code is working fine except one problem and the problem is that it shows 000001 in form-1 input field and then add 1 and shows 000002 in form-2 but i want it to show same serial no on both forms e.g 000001 on form-1 and also 000001 on form-2 thats it! since i know little about php and sqlite i cann't figure it out. any help will be really appreciated.

ps: i don't want to change the nature of the code because every time i clicked on the link to that page it increases serial no by one in the input field.

Aqeel Arcade
  • 47
  • 1
  • 7

1 Answers1

1

Each time you're calling getNextSequnceNumber() you are incrementing the value. You want to store the sequence number in a variable once and then use that variable in multiple places.

Also, the way your code is setup, it will increment on every page load.

...
<?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>

EDIT

<?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>
waterloomatt
  • 3,662
  • 1
  • 19
  • 25
  • yes i want it to increment on every page load but i also want it to show same on both fields. can you please help me with working example cause i tried but no success. – Aqeel Arcade May 01 '17 at 12:06
  • @AqeelArcade - what is not working? Did you try the code I posted? Notice the input's value is referencing the variable and not calling the method anymore: value="" – waterloomatt May 01 '17 at 15:01
  • thanks for reply but your given code only reverse the case now it show **000002** on first input and **000001** on 2nd input field. please provide me a working fiddle example because i need it urgently. i want to show same no on both fields and after every refresh it also increases. thanks – Aqeel Arcade May 01 '17 at 21:29
  • [Working fiddle](http://www.tutorialspoint.com/php_mysql_online.php?PID=0Bw_CjBb95KQMUmFwcGhxbS1GYTA) - please mark my answer if it's been useful to you. – waterloomatt May 02 '17 at 01:41