Jump to content

Recommended Posts

Hello. I have here a php code getting information from mysql but when printing the result is duplicated, or more.

 

<?php 
$query = "SELECT SUM(amount) FROM user_detail"; 

$result = mysql_query($query) or die(mysql_error());

// Print out result
while($row = mysql_fetch_array($result)){
echo "<p align=\"left\">Total ".$row['SUM(amount)']."</p>";

}

?>

 

and then I get result like this:

 

8hekx.jpg

 

you can see there are 3 result and I need to print only one time. I use for sql connection other file with :

 

include_once("./includes/config.php");

 

Need help to solve this :) Thanks in advance

Link to comment
https://forums.phpfreaks.com/topic/249331-need-help-with-duplicated-data/
Share on other sites

No, i'm including it only one time. Sincerely. I don't know why it is printed more than 2 times. On the october payment I entered payment 3 times and maybe for this reason it is printing in this way, but I would like to print all my payments (even if there are more than 1 within the month) only one time. I have not used debug statment.

Sincerely. I don't know why it is printed more than 2 times.

Sincerely, it's because you have something (which you haven't posted) that prints the value multiple times. There is absolutely no reason why what you posted will do what you say it does.

I don't know why it is printed more than 2 times.

You showed it printing once.  You said you wanted it once.  Now you're saying "2 times."  Clearly you're doing something that prints this more than once.  The code you showed is only capable of printing this value once.

 

-Dan

here you have entire code:

 

<?PHP  session_start();
include_once("./includes/config.php");
include_once("./check_session.php");
if($_REQUEST['strAmount']!="")
{
	$strInsert="INSERT INTO `user_detail` (
											`user_id` ,
											`amount` ,
											`payment_method:` ,
											`Invoice_number`,
											`detail_date`
											)
											VALUES (
											'".$_SESSION['user_id']."',
											'".$_REQUEST['strAmount']."',
											'".$_REQUEST['strPaymentMethod']."',
											'".$_REQUEST['strInvoiceNumber']."',
											'".date('Y-m-d')."'
											)";
	mysql_query($strInsert);	
}

if($_SESSION['username']=="administrator")
   {
	$where="";			
   }
   else
   {

	$where="where user_id='".$_SESSION['user_id']."'";
   }
$strSelectoverDue="select * from user_detail $where ";
$objSelectoverDue=mysql_query($strSelectoverDue);
while($rsSelectoverDue=mysql_fetch_array($objSelectoverDue))
{
	$InvoiceDate=$rsSelectoverDue['detail_date'];
	$InvoiceDate = explode("-",$InvoiceDate);
	$mdyInvoiceDate=date('m-d-Y',mktime(0, 0, 0,$InvoiceDate[1]+1,$InvoiceDate[2],$InvoiceDate[0]));
	$mdyInvoiceDate = explode("-",$mdyInvoiceDate);
	$mdyInvoiceDate=$mdyInvoiceDate[2]."-".$mdyInvoiceDate[0]."-".$mdyInvoiceDate[1]; 
	$date=date("Y-m-d");
	if($mdyInvoiceDate<$date)
	{ 
		$strUpdate="update user_detail set overdue=1 where user_detail_id='".$rsSelectoverDue['user_detail_id']."'";
		mysql_query($strUpdate);
	}

}

include_once("header.php");?>
<script type="text/javascript" src="./include/common.js"></script>
<div id="content">

<div class="right"> 
<table border="1" cellpadding="0" align="center" cellspacing="0" width="100%" style="border-collapse: collapse" bordercolor="#E0E0E0" height="380">				
<tr valign="top" align="center">
<td>
	<form name="frmUsers" action="" method="post">
	<input type="hidden" name="intMemberId" id="intMemberId" value="" >
	<input type="hidden" name="action" id="action" value="" >
	<?php if($_SESSION['username']!="administrator")
			{?>
        <table width="98%"  border="1" align="center" cellpadding="2" cellspacing="2">
	<?PHP if(isset($_REQUEST['strMsg'])){?>
	<tr>
		<td colspan="3" align="center" class="arial_black_regular"><span class="arial12_red"><?PHP echo $_REQUEST['strMsg'];?></span></td>			
	</tr>
	<?PHP }
	$strSelect="select * from  user_master where user_id='".$_SESSION['user_id']."'";
	$objSelect=mysql_query($strSelect);
	$rsSelect=mysql_fetch_array($objSelect);
	$strDate =$rsSelect['date'];
	$strDate = explode("-",$strDate);
	$month_name = date( 'F', mktime(0, 0, 0, $strDate[1]) );

	 ?>
	<tr>
	  	<td width="33%" align="left" class="arial_black_regular"> <strong>username</strong></td>
		<td width="33%" align="left" class="arial_black_regular"> <?php echo $_SESSION['username'];?></td>
		<td width="33%" align="left" class="arial_black_regular"> <B>current month <?php echo $month_name;?></B></td>
     	</tr>  
	<tr>
		<td width="179" align="left" class="arial_black_regular">payment formა:</td>
		<td width="200" height="25" align="left" class="arial_black_regular"><input type="text" name="strPaymentMethod" /></td>
		<td width="200" height="25" align="left" class="arial_black_regular"></td>
	 </tr>   
         <tr>
		<td width="179" align="left" class="arial_black_regular">amount:</td>
		<td width="200" height="25" align="left" class="arial_black_regular"><input type="text" name="strAmount" /></td>
		<td width="200" height="25" align="left" class="arial_black_regular"></td>
	 </tr>    	 
	<tr>
		<td width="179" align="left" class="arial_black_regular">transaction code:</td>
		<td width="200" height="25" align="left" class="arial_black_regular"><input type="text" name="strInvoiceNumber" /></td>
		<td width="200" height="25" align="left" class="arial_black_regular"><input type="submit"  value="შენახვა"/></td>
	 </tr>
        </table>
       <?php } ?>
        <br />
        <h3>Payment History</h3><br />
        <table width="100%"  border="1" align="center" cellpadding="2" cellspacing="2">	
        <tr>
<?php  if($_SESSION['username']=="administrator"){?>
<td width="20%" align="left" class="arial_black_regular">user</td><?php }?>
		<td width="20%" align="left" class="arial_black_regular">month</td>
		<td width="20%" align="left" class="arial_black_regular">date</td>
            <td width="20%" align="left" class="arial_black_regular">amount</td>
            <td width="20%" align="left" class="arial_black_regular">transacton code</td>
		<td width="20%" align="left" class="arial_black_regular">overdue</td>
	 </tr>
         
         <?php if($_SESSION['username']!="administrator")
		   {
	 			$where="and user_master.user_id='".$_SESSION['user_id']."'";
		   }
		   else
		   {
		   		$where="";
		   }
			?>
         
         <?php $strSelectDetail="select * from user_detail,user_master where user_master.user_id=user_detail.user_id $where";
	 		$objSelectDetail=mysql_query($strSelectDetail);
			while(@$rsSelectDetail=mysql_fetch_array($objSelectDetail))
			{  $strDate =$rsSelectDetail['detail_date'];
				$strDate = explode("-",$strDate);
				$month_name_detail = date( 'F', mktime(0, 0, 0, $strDate[1]) );


				if($rsSelectDetail['overdue']==1)
					$overdue="Yes";
				else
					$overdue="No";

				if($_SESSION['username']=="administrator" && $overdue=="Yes")
					$reminder='<a href="reminder.php?id='.$rsSelectDetail['user_detail_id'].'">Reminder</a>';
				else
					$reminder="";
				?>
<?php 
$query = "SELECT SUM(amount) FROM user_detail"; 

$result = mysql_query($query) or die(mysql_error());

// Print out result
while($row = mysql_fetch_array($result)){
echo "<p align=\"left\">Total ".$row['SUM(amount)']."</p>";
echo "<br />";
}

?>
                 <tr>
<?php  if($_SESSION['username']=="administrator"){?><td><?php echo $rsSelectDetail['username'];?></td><?php }?>
                    <td><?php echo $month_name_detail;?></td>
                    <td><?php echo $rsSelectDetail['detail_date'];?></td>
                    <td><?php echo $rsSelectDetail['amount'];?></td>
                    <td><?php echo $rsSelectDetail['Invoice_number'];?></td>
                    <td><?php echo $overdue."    ".$reminder;?></td>
                 </tr>
                 <?php }?>
        </table>
	</form>                               			
</td>
</tr>
</table>
</div>

<?PHP include_once("left.php")?>

<div style="clear: both;"> </div>

</div>
<?PHP include_once("footer.php")?>
</div>

</body>
</html>

 

I have one submit button for the fields and if user pays more than one time within a month it displays the same time :S

try changing this...

while($row = mysql_fetch_array($result)){
echo "<p align=\"left\">Total ".$row['SUM(amount)']."</p>";
echo "<br />";
}

 

to this...

$row = mysql_fetch_array($result));
echo "<p align=\"left\">Total ".$row['SUM(amount)']."</p>";
echo "<br />";

try commenting out the while loop as follows

/*
while($row = mysql_fetch_array($result)){
echo "<p align=\"left\">Total ".$row['SUM(amount)']."</p>";
echo "<br />";
}
*/
echo "just a test"

 

Also, isn't the total suppose to equal the payments? ie 34 plus 15 plus 10? that is 59 not 79. Hmmmm

try commenting out the while loop as follows

/*
while($row = mysql_fetch_array($result)){
echo "<p align=\"left\">Total ".$row['SUM(amount)']."</p>";
echo "<br />";
}
*/
echo "just a test"

 

Also, isn't the total suppose to equal the payments? ie 34 plus 15 plus 10? that is 59 not 79. Hmmmm

 

Well, the thing is that 79 is the total number extracted from the column amount (total) for this user there is a payment of 59 but there is also other user-me, so for me the payment is 20. If fact we are testing a code, there 2 users, me and david212. He have introduced for his october payment 34+15+10 and I have introduced for my payment only 20 ( as i remember). So, we want to display the total amount of existing payment to all users. But when user enter payment 2 or  3 times within a month, the result is displayed 2 or 3 times and more

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.