Click here to Skip to main content
15,910,009 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
User will enter like more than one payment card 1.00,cash 2.00,card 10,00,cash 20.00 etc...After these all values insert into payment_details table one by one along with current date.So after this i need to insert data to another table called moneybox table.
Count of total cash and total card will store into money box group by current date.

payment table looks like

    card 1.00 2018-10-18
    cash 2.00 2018-10-18
    card 10.00 2018-10-18
    cash 20.00 2018-10-18

insert record into "moneybox" table like (only two rows) 

    cash  11.00 2018-10-18 // all cash sum value 11 of 2018-10-18
    card  22.00 2018-10-18 // all card sum value 22 of 2018-10-18
    cash  200.00 2018-10-19
    card  370.00 2018-10-19

always two rows ie; card and cash will be there in money table,going to total of cash and card will be store based on current date.

Now the problem,if user enter more than one payment mode like card:10.00 cash:10.00 card:30:00 cash:20:00.Now inserting to moneybox is happening but card 10.00 only adding.When inserting more than one payment,count all cash amount and all card amount will store in moenybox table.
how to solve this issue?


What I have tried:

Here is my php code for inserting and updating

    $todayDate = date("Y-m-d");
        // flag to check whether inserted for today
    $cashMode = 0; 
    $cardMode = 0;
	
    for ($count = 0; $count < count($getamount); $count++) {
        $payamt_clean          = $getamount[$count];
        $getstorepaymode_clean = $getstorepaymode[$count];
        date_default_timezone_set('Asia/Dubai');
        $created = date("y-m-d H:i:s");    
        
        $query .= 'INSERT INTO payment_details (invoiceID,paymentMode,Amount,created)
                        VALUES ("' . $getinvoiceid . '" , "' . $getstorepaymode_clean . '", "' . $payamt_clean . '", "' . $created . '");
    ';
	
	if($last_moneybox_created_date != $todayDate)   {
                $cashMode = 0;
                $cardMode = 0;
     } 
			
	$cal_closingbalancecash = $last_moneybox_closingbalanacecash - $payamt_clean;
    $cal_closingbalancecard = $last_moneybox_closingbalanacecard - $payamt_clean;
	
    
	
	if($getstorepaymode_clean === "cash" && $cashMode === 0 && $last_moneybox_created_date != $todayDate) {
                echo 'Different Date'; //insert happen based on the type
                $last_moneybox_created_date = $todayDate;
				

                $query .= "INSERT INTO moneybox (type,inflow,date) 
                 VALUES ('cash','$payamt_clean','$todayDate');";   

				$query .= "INSERT INTO moneybox (type,inflow,date) 
                 VALUES ('bank','0.00','$todayDate');"; 
            }
            else if($getstorepaymode_clean === "cash" && $cashMode === 1 && $last_moneybox_created_date == $todayDate) {
                //echo 'Same Date'; //update happen based on type and date
                $query .= "UPDATE moneybox SET 
                        inflow = inflow + $payamt_clean, 
                        closing_balance= opening_balance + inflow - outflow 
                        WHERE type = '$getstorepaymode_clean' and date = '$todayDate';";                                           
            }

		
	if($getstorepaymode_clean === "card" && $cardMode === 0 && $last_moneybox_created_date != $todayDate) {
                echo 'Different Date'; //insert happen based on the type
                $last_moneybox_created_date = $todayDate;

					
				$query .= "INSERT INTO moneybox (type,inflow,date) 
                 VALUES ('cash','0.00','$todayDate');";     
				 
                $query .= "INSERT INTO moneybox (type,inflow,date) 
                 VALUES ('bank','$payamt_clean','$todayDate');";                      
            }
            else if($getstorepaymode_clean === "card" && $cardMode === 1 && $last_moneybox_created_date == $todayDate) {
                //echo 'Same Date'; //update happen based on type and date
                $query .= "UPDATE moneybox SET 
                        inflow = inflow + $payamt_clean, 
                        closing_balance= opening_balance + inflow - outflow 
                        WHERE type = '$getstorepaymode_clean' and date = '$todayDate';";                                           
			}
    }
Posted
Updated 19-Oct-18 22:36pm

1 solution

PHP
$query .= 'INSERT INTO payment_details (invoiceID,paymentMode,Amount,created)
                        VALUES ("' . $getinvoiceid . '" , "' . $getstorepaymode_clean . '", "' . $payamt_clean . '", "' . $created . '");

Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900