Hello guys!

I am editing a program regarding billing system done in Php and MySQL and generated using the Code Charge Studio. I have problem in validating a newly entered data. What i want is to check if the billing statement number is existing or not and will not saved after validation. Saving will be done after it has pass all the validation and when save button is click. If it is existing, there will be a message displaying that it will not accept the newly entered data for the fact that it is stored in the database. If the billing statement number is available, it will go next page and later will be saved.

Also please help me on how to erase the data stored in a variable to prevent inserting to database when refresh and Back button is pressed. please help.


Thanks.....

Dani AI

Generated

A compact, reliable pattern for this problem:

Check uniqueness on the server, then avoid double-inserts by using Post/Redirect/Get and by letting the database enforce uniqueness. was on the right track with a database check; the usual mistake that causes confusing output is treating the raw query result as the existence flag instead of counting rows or fetching a value. later confirmed a fix — the steps below show a robust approach any PHP+MySQL app (including CodeCharge-generated pages) can use.

Safe existence check (use prepared statements and COUNT):

$pdo = new PDO($dsn, $user, $pass, $opts);

$stmt = $pdo->prepare('SELECT COUNT(*) FROM billing_statements WHERE bs_no = :bs');
$stmt->execute([':bs' => $billingNumber]);
if ($stmt->fetchColumn() > 0) {
    // already exists: return validation error, do NOT save
} else {
    // number available: continue to confirmation step
}

Prevent duplicate insert on refresh/back and rely on DB uniqueness:

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // validate, then insert inside try/catch
    // after successful insert:
    header('Location: /billing/confirmation.php?bs=' . urlencode($billingNumber));
    exit;
}

Also add a unique constraint so the DB enforces uniqueness even if the app logic fails:

ALTER TABLE billing_statements ADD UNIQUE KEY (bs_no);

Extra tips: store statement numbers in a string column (VARCHAR) to preserve leading zeros; always bind parameters as strings; use transactions and catch duplicate-key errors to provide a friendly message; and put validation logic in the pre-insert/validate event if using CodeCharge Studio. For PDO and header usage, see the PHP manual: PDO and header.

Recommended Answers

All 5 Replies

after pressing submit validate fields i.e.

if ($textboxvalue == "") {

after pressing submit validate fields i.e.

if ($textboxvalue == "") {
  // go back and error
}

then perform an sql request (im guessing you using a database)

"SELECT * FROM databasename WHERE statementnumber = " . $statementnumber . ";";

then check if a result is returned.
if one is then you know you have a result in the database with this number and you can refer the user to whichever page required.

also you will not need to empty any variables as you will always perform this check and if there is a value and they press back it will falg a record is already in the database. Hope this helps

no idea why my first comment is there ?

$fldbsnum = get_param("bsnum");
echo "bsnumber: $fldbsnum";
$queryvalidate = mysql_query("select (bs_number) from billingstatementhistory where bs_number = $fldbsnum");
echo "result: $queryvalidate";

if (mysql_query($queryvalidate) == true) {
echo "The billingstatement exists";
}
else
{
echo"pass";
}

Hello! here is my test code in validating for the existing bsnum. it does not evaluate the first condition even the newly entered bsnum is existing. pls help me........

The output is:

bsnumber: 000001
result: Resource id #13
pass

Thank you very much for help..... I have made solutions based on the your suggested query.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.