Hi all,

I want to perform cash transactions such as cash deposits and cash withdrawals in my project.Here i have created a page called transaction.php to fulfill those requirements.But there were few error which i need your help to sought it out..

account (account_number, account_type, fd_period, account_balance, account_interest )

transacion (tran_id, to_account_number, from_account_number, transaction_type, transaction_amount, transaction_date )

<?php

$connect=mysql_connect("localhost","root","");
mysql_select_db("bank",$connect) or die ("could not select database");

if(isset($_POST['submit'])){
$query= "SELECT `full_name` FROM account_details WHERE `account_number`='".$_POST['account_number']."'";
      $result = mysql_query($query) or die(mysql_error());
      $row = mysql_fetch_assoc($result);
      if(mysql_num_rows($result)==1 and $row['account_balance']<$_POST['transaction_amount'] and strtolower($_POST['transaction_type'])=="withdrawal"){
          echo "Insufficient balance";
      }elseif(mysql_num_rows($result)==1){

      if(strtolower($_POST['transaction_type'])=="deposit"){
$query = "INSERT INTO transaction (to_account_number) VALUES ('".$_POST['to_account_number']."')";
	  mysql_query($query) or die (mysql_error());
            $operator = "+";
      }else{
            $operator = "-";
$query = "INSERT INTO transaction (from_account_number) VALUES ('".$_POST['from_account_number']."')";
	  mysql_query($query) or die (mysql_error());		
      }
$query= "UPDATE account SET `account_balance`=(`account_balance`".$operator.$_POST['transaction_amount'].") WHERE `account_number`='".$_POST[               'account_number']."'";
mysql_query($query) or die(mysql_error());
   
$query = "INSERT INTO  transaction (transaction_type, transaction_amount, transaction_date) 
	            VALUES('".$_POST['transaction_type']."','".$_POST['transaction_amount']."','".$_POST['transaction_date']."')";
	  
mysql_query($query) or die(mysql_error());
            echo $row['full_name'].",<br> your transaction has been successfully processed";
      }else{
            echo "invalid account number";
      }
}
 
?>

Undefined index: account_balance in C:\wamp\www\MySite\php files\transactions.php on line 14

if(mysql_num_rows($result)==1 and $row['account_balance']<$_POST['transaction_amount'] and strtolower($_POST['transaction_type'])=="withdrawal"){

Undefined index: to_account_number in C:\wamp\www\MySite\php files\transactions.php on line 19

$query = "INSERT INTO transaction (to_account_number) VALUES ('".$_POST['to_account_number']."')";

Dani AI

Generated

Short diagnosis and what caused the errors observed here: the undefined account_balance came from selecting only full_name (so $row['account_balance'] did not exist) — pointed this out. The later persistent "invalid account number" happened because the join query lost the filter by the submitted account number, so the result set did not match the expectation of exactly one row (as suggested, check the query in phpMyAdmin). 's suggestion to dump $_POST is a good early check to confirm form field names like to_account_number / from_account_number are actually sent.

Recommended fixes and a safer workflow

  • Always validate and sanitize incoming fields (presence, numeric amounts, allowed transaction types) before DB work.
  • Read both full_name and account_balance in a single, filtered query (join the two tables and include WHERE account.account_number = :acct) so $row['account_balance'] is defined only when that account exists.
  • Move away from deprecated mysql_* calls and use prepared statements (PDO or mysqli). Use database transactions and row-level locks for balance updates to avoid race conditions.

Example transaction flow (PDO, prepared statements, SELECT ... FOR UPDATE)

try {
  $pdo->beginTransaction();
  // SELECT a.account_balance, ad.full_name FROM account a JOIN account_details ad ...
  // fetch row FOR UPDATE, check existence and balance
  // UPDATE account SET account_balance = account_balance + :delta WHERE account_number = :acct
  // INSERT into transaction (set to_account_number OR from_account_number as appropriate)
  $pdo->commit();
} catch (Exception $e) {
  $pdo->rollBack();
  throw $e;
}

Practical cautions and quick checklist

  • Use SELECT ... FOR UPDATE inside a transaction when checking and updating balances.
  • Compute a single signed delta (positive for deposit, negative for withdrawal) and apply it in one UPDATE.
  • Ensure form field names match the INSERT column names; guard against SQL injection with prepared statements.
    Further reading: PDO prepared statements, PDO transactions, and InnoDB locking/read behavior (SELECT ... FOR UPDATE) at the MySQL manual (see InnoDB locking reads).

Recommended Answers

All 8 Replies

the first error is due to this:-

$query= "SELECT `full_name` FROM account_details WHERE `account_number`='".$_POST['account_number']."'";

here you are only selecting the full name field only...and also check whether you want to select data from account or account_details

instead of this it should be

$query= "SELECT `full_name`,`account_number` FROM account_details WHERE `account_number`='".$_POST['account_number']."'";

And for ypur second error just check the spelling of variable in table or in the query.Just run the query in phpmyadmin and check whether it is working fine or not....

The same error again.

Here my account_balance comes from my "account" table.
full_name is from "account_details"table. How can i use it in the coding?

Regarding my second error,

I want to pass data to my "to_account_number" column when the user selects "deposit" as the transaction type.
I want to pass data to my "from_account_number" column when the user selects "withdrawal" as the transaction type.

-For 1st error do this...
use join to read data from 2 tables ....For more information click here

Member Avatar for Member #120589

heshanm - you've started about 5 threads on this issue now. Perhaps it would be an idea to keep to the same thread until the issue is finally resolved. All these different threads tend to rework covered ground. Just a thought.

@ ardav,

I thought, sometimes it is a mess for others to see all codings in a sigle thread when there were so many replies. That's why i thought to solve the issue step by step.Anyway your suggestion is welcomed.

@ IIM,

Thanks for the link.Do you have any ideas regarding my second issue? I was bit confused about that.

in the begining of your code, you check the post values and see whether you are getting expected variables, and their values or not

<?php
echo "<pre>";
print_r($_POST);
echo "</pre>";
$connect=mysql_connect("localhost","root","");
.
.
.

I have changed the coding little bit and i found the error. It comes from this line.

}else if(mysql_num_rows($result)==1){

As a result it is ALWAYS give the output as "Invalid account number"

<?php
$connect=mysql_connect("localhost","root","");
mysql_select_db("bank",$connect) or die ("could not select database");

if(isset($_POST['submit'])){
$query = "SELECT account_details.full_name, account.account_balance ".
 "FROM account_details, account ".
	"WHERE account_details.account_number = account.account_number";
	
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result);
	  
if(mysql_num_rows($result)==1 and $row['account_balance']<$_POST['transaction_amount'] and strtolower($_POST['transaction_type'])=="withdrawal"){
          echo "Insufficient balance";
      }else if(mysql_num_rows($result)==1){

      if(strtolower($_POST['transaction_type'])=="deposit"){
	 
      $operator = "+";
      }else{
            $operator = "-";
	
	  }
$query= "UPDATE account SET `account_balance`=(`account_balance`".$operator.$_POST['transaction_amount'].") WHERE `account_number`='".$_POST['account_number']."'";
mysql_query($query) or die(mysql_error());
   
$query = "INSERT INTO  transaction (transaction_type, transaction_amount, transaction_date) 
	            VALUES('".$_POST['transaction_type']."','".$_POST['transaction_amount']."','".$_POST['transaction_date']."')";
	  
mysql_query($query) or die(mysql_error());

echo $row['full_name'].",<br> your transaction has been successfully processed";
}else{
       echo "invalid account number";
      }
}
 
?>

"SELECT account_details.full_name, account.account_balance ".
"FROM account_details, account ".
"WHERE account_details.account_number = account.account_number";

Check this query manually in phpMyAdmin. If 0 row returns, the problem is no account in table.

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.