david212 Posted October 18, 2011 Share Posted October 18, 2011 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: 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 Quote Link to comment https://forums.phpfreaks.com/topic/249331-need-help-with-duplicated-data/ Share on other sites More sharing options...
ManiacDan Posted October 18, 2011 Share Posted October 18, 2011 This code should only print once. Are you including it more than once? Have you attempted to debug this using debug statements at all? Quote Link to comment https://forums.phpfreaks.com/topic/249331-need-help-with-duplicated-data/#findComment-1280258 Share on other sites More sharing options...
david212 Posted October 18, 2011 Author Share Posted October 18, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/249331-need-help-with-duplicated-data/#findComment-1280288 Share on other sites More sharing options...
requinix Posted October 18, 2011 Share Posted October 18, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/249331-need-help-with-duplicated-data/#findComment-1280309 Share on other sites More sharing options...
ManiacDan Posted October 18, 2011 Share Posted October 18, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/249331-need-help-with-duplicated-data/#findComment-1280320 Share on other sites More sharing options...
litebearer Posted October 18, 2011 Share Posted October 18, 2011 showing my ignorance; however, by NOT grouping, wouldn't it display the sum for the number of records ie if there are 3 records print it 3 times if 9 records 9 timess? Quote Link to comment https://forums.phpfreaks.com/topic/249331-need-help-with-duplicated-data/#findComment-1280326 Share on other sites More sharing options...
requinix Posted October 18, 2011 Share Posted October 18, 2011 No. Without a GROUP BY clause, aggregate functions operate on the entire table. So just one value. Quote Link to comment https://forums.phpfreaks.com/topic/249331-need-help-with-duplicated-data/#findComment-1280333 Share on other sites More sharing options...
david212 Posted October 19, 2011 Author Share Posted October 19, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/249331-need-help-with-duplicated-data/#findComment-1280404 Share on other sites More sharing options...
litebearer Posted October 19, 2011 Share Posted October 19, 2011 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 />"; Quote Link to comment https://forums.phpfreaks.com/topic/249331-need-help-with-duplicated-data/#findComment-1280408 Share on other sites More sharing options...
david212 Posted October 19, 2011 Author Share Posted October 19, 2011 Hi Tried and the same , printing 3 times and below is the screenshot of my 3 data and if i put the 4th time the payment it will be displayed 4 times. How to avoid it Quote Link to comment https://forums.phpfreaks.com/topic/249331-need-help-with-duplicated-data/#findComment-1280410 Share on other sites More sharing options...
litebearer Posted October 19, 2011 Share Posted October 19, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/249331-need-help-with-duplicated-data/#findComment-1280411 Share on other sites More sharing options...
breakpoint Posted October 19, 2011 Share Posted October 19, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/249331-need-help-with-duplicated-data/#findComment-1280418 Share on other sites More sharing options...
breakpoint Posted October 19, 2011 Share Posted October 19, 2011 /* while($row = mysql_fetch_array($result)){ echo "<p align=\"left\">Total ".$row['SUM(amount)']."</p>"; echo "<br />"; } */ echo "just a test" The just a test has been printed also 3 times similar to the total amount Quote Link to comment https://forums.phpfreaks.com/topic/249331-need-help-with-duplicated-data/#findComment-1280480 Share on other sites More sharing options...
breakpoint Posted October 19, 2011 Share Posted October 19, 2011 Topic solved I was printing the result on the incorrect place! Moders, please close the topic as solved, the author is away for a long time! Thanks to all Quote Link to comment https://forums.phpfreaks.com/topic/249331-need-help-with-duplicated-data/#findComment-1280493 Share on other sites More sharing options...
gizmola Posted October 19, 2011 Share Posted October 19, 2011 Pebkac Quote Link to comment https://forums.phpfreaks.com/topic/249331-need-help-with-duplicated-data/#findComment-1280580 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.