Hi,

My transaction table looks like this.It includes following fields.
tran_ID,account_type,account_number,transaction_type,amount,Date

The records regarding each transaction is stored in this table. I want to know how i retrieve the data from this table into another form or table.The coding is what i want...

My actual purpose would be to allow cash supervisor to update records. But first it needs to retrieve data. Then only he can check things and update if necessary.

Thanks,
Heshan.

Recommended Answers

All 8 Replies

save this as hielo.php and try it:

<?php
define('TABLE_NAME','accounts');

$dbConnection= mysql_connect("localhost","username","password") or die( mysql_error() );

$fields=array(	'tran_ID'=>array('label'=>'Transction ID','format'=>"%d")
				,'account_type'=>array('label'=>'Account Type','format'=>"'%s'")
				,'account_number'=>array('label'=>'Account Number','format'=>"'%s'")
				,'transaction_type'=>array('label'=>'Transaction Type','format'=>"'%s'")
				,'amount'=>array('label'=>'Amount','format'=>"'%s'")
				,'Date'=>array('label'=>'Date','format'=>"'%s'")
				);

mysql_select_db("dbName") or die( mysql_error() );

if( isset($_POST['update']) && !empty($_POST['update']) )
{
	updateRecord($_POST['tran_ID'], $dbConnection);
}
elseif( isset($_POST['edit']) && !empty($_POST['edit']) )
{
	showSelectedRecord($_POST['tran_ID'], $dbConnection);
}
else
{
	showAllRecords($dbConnection);
}
mysql_close();
exit();

function showAllRecords($dbConnection)
{
  $sql = sprintf("SELECT `tran_ID`,`account_type`,`account_number`,`transaction_type`,`amount`,`Date` FROM `%s`",TABLE_NAME);
  $result=mysql_query($sql) or die( mysql_error() );
  $total=mysql_num_rows($result);

  if( 0==$total )
  {
    echo "<p>No records found.</p>";
  }
  else
  {
    $row=mysql_fetch_assoc($result);
	$id=$row['tran_ID'];

    echo '<table>';
    echo '<thead><tr><th>' . implode( '</th><th>', $keys) . '</th></tr></thead><tbody>';
	unset($row['tran_ID']);
    do{
      echo '<tr><td>' . implode('</td><td>',$row) . '</td><td><form method="post" action="' . $_SERVER['PHP_SELF'] . '"><input type="hidden" name="tran_ID" value="' . $id . '"/><input type="submit" name="edit" value="Edit"/></form></tr>';
    }while( $row=mysql_fetch_assoc($result) );
    echo '</tbody></table>';
  }
return $total;
}

function showSelectedRecord( $id,$dbConnection )
{
  global $fields;
  
  $sql = sprintf("SELECT `account_type`,`account_number`,`transaction_type`,`amount`,`Date` FROM `%s` WHERE `tran_ID`=%d", TABLE_NAME, mysql_real_escape_string($id));
  $result=mysql_query($sql) or die( mysql_error() );
  $total=mysql_num_rows($result);
  if(1==$total)
  {
  	$row=mysql_fetch_assoc();
	echo sprintf('<form method="%s" action="%s">','post',$_SERVER['PHP_SELF']);
	foreach($row as $k=$v){
		echo sprintf('<div><label for="%s">%s</label>: <input type="text" name="%s" value="%s"/></div>', $k, $fields[$k]['label'], $k, htmlentities($v, ENT_QUOTES) );
	}
	echo '<div><input type="hidden" name="tran_ID" value="'.$id.'"/><input type="submit" name="update" value="Update" /></div>';
	echo '</form>';
  }
  elseif(0==$total)
  {
    echo '<p>Unable to find a record with tran_ID='.$id.'</p>';
  }
  else
  {
    echo '<p>Unable to find a UNIQUE record with tran_ID='.$id.'. Instead, a total of '.$total.' records were found!</p>';
  }
}

function updateRecord($id, $dbConnection)
{
  global $fields;
  $sql="";
  unset($fields[$id]);
  foreach($fields as $k=>$v)
  {
    $f=",`%s`=".$fields[$k]['format'];
  	$sql.=sprintf($f, $k, mysql_real_escape_string($_POST[$k]) );
  }
  $sql=sprintf("UPDATE `%s` SET %s WHERE `tran_ID`=%d", TABLE_NAME, substr($sql,1), mysql_real_escape_string($id) );
  mysql_query($sql) or die(mysql_error());
  echo '<p>Record updated successfully - <a href="'.$_SERVER['PHP_SELF'].'">Start over</a></p>';
}
?>

Hello, i have similar problem.

In OOP PHP, i got 2 classes: Database and News. In News class i call DB function and get result (multidimensional 2D array). Now i have to display that array in index.php. I call News class (lets say get_news() function) from index.php but cant retrieve data and always get message: "Cannot use object of type News as array in ..."

Could someone help

My code is something like this:

database.php

class DB{
function news($user_id){
$q="select all from news where author id='$id'";
$result=$db->query($q);
for($i=0; $i<$r->num_rows; $i++){
   $arr[]=$result->fetch_array;
}
return $arr;               //works fine!
}}

news.php

class News{
function get_news($user_id){
global $db;
$news=$db->news($user_id);
print_r($news);        // works fine!
return $news;
}}

index.php // PROBLEM - HOW TO DISPLAY DATA FROM NEWS.PHP

$post=new News;
$post->get_news($user_id);
...

i tried
for($i=0; $i<5; $i++){
  foreach($post[$i] as $k=>$v){
      echo $v;                    //get error: "Cannot use object of type News as array in ..."
   }
}

newprimitive, please stop hijacking someone else's thread and open your own. I'll gladly help

sorry, i thought that a new thread, that is quite similar to this one, would be characterized as some kind of spamming.

@ hielo, it gives a parse error regarding this line.

foreach($row as $k=$v){

I already made a code to update the error transactions like this.

<?php

//getting the faulty transaction
$query = "SELECT * from transaction_table WHERE tran_ID=$_POST['tran_ID']";
mysql_query($query) or die(mysql_error());
$row=mysql_fetch_assoc($result);


//Correcting the account type table
if(strtolower($row['transaction_type'])=="deposit"){
            $operator = "-";
      }else{
            $operator = "+";
      }
      $query= "UPDATE `".$row['account_type']."` SET `balance`=(`balance`".$operator.$row['amount'].") WHERE `ID`='".$row['account_number']."'";
      mysql_query($query) or die(mysql_error());
	  

//deleting the faulty transaction
$query = "DELETE from transaction_table WHERE tran_ID=$_POST['tran_ID']";
mysql_query($query) or die(mysql_error());

?>

Therefore if i have retrieval data code it will be of. So i can change the erroneous record and clicks on " update" button

sorry, it should be an "arrow" not an "equal": foreach($row as $k => $v)

It works, thanks a lot...

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.