1

I have a code which is works fine, but the data cannot save to the database. I want to insert cost, currency_rate, profit_rate and pprice to database through Ajax. Here are the code of javascript and update.php, I have tried to modify the code to save in my Mysql server, but it didn't success. Can someone help with this?

javascript

$(".profitRate").change(function() {
    var myArray = [];
    //find closest table->next table
    var elem = $(this).closest('table').next('table');
    var action = elem.find('tr').data('action');
    console.log(action)

    var profitRate = Number($("#profitRate").val());
    //looping 
    elem.find('tr').each(function() {
        //get cost
        var cost = $(this).find('input[name=cost]').val();
        //get curency rate
        var currecy_rate = $(this).find('select[name=currency_rate]').val();
        //calculate profit
        var profit_total = Math.round(cost * profitRate * currecy_rate)
        $(this).find('input[name=pprice]').val(profit_total)
        //add to json object
        auto_array = {};
        auto_array["cost"] = cost;
        auto_array["currecy_rate"] = currecy_rate;
        auto_array["pprice"] = profit_total;
        myArray.push(auto_array) //push to array
    });
    console.log(myArray)

    form_data = elem.find('tr').data('action');
    $.ajax({
        data: {
            action: action,
            form_data: form_data,
        },
        url: 'update.php',
        type: 'post',
        beforeSend: function() {

        },
        success: function(data) {
            if(data == 1){

            }
        }
    });
})

update.php

<?php

if ($_POST['action'] == 'update_price') {
    parse_str($_POST['form_data'], $my_form_data);

    $id = $my_form_data['id']; 
    $cost = $my_form_data['cost']; 
    $profit_rate = $my_form_data['profit_rate']; 
    $currency_rate = $my_form_data['currency_rate']; 
    $pprice = $my_form_data['pprice'];

    $sql = $query = $finalquery = $sqlresult = '';

    if ($cost){
        $sql.="cost='$cost',";
    }
    
    if ($profit_rate){
        $sql.="profit_rate='$profit_rate',";
    }

    if ($currency_rate){
        $sql.="currency_rate='$currency_rate',";
    }   

    if ($pprice){
        $sql.="pprice='$pprice',";

        $finalquery = rtrim($sql,',');
        $query="UPDATE `gp_info` SET $finalquery where id=$id";

        $sqlresult=mysql_query($query);
        if($sqlresult){
            $reback=1;
        }else{
            $reback=0;
        }
        echo $reback;
    }
}
lewis4u
  • 14,256
  • 18
  • 107
  • 148
  • You should edit your question to include your code for update.php , but don't include your real user and password. Also add what database server type and version you're writing to. – Dave S Jul 11 '20 at 07:53
  • you didn't assign any value for `form_data` . – Swati Jul 11 '20 at 08:14
  • You need to throw away this PHP code and start from scratch. Read about SQL injection vulnerabilities and how to use query parameters in PHP. – Tomalak Jul 11 '20 at 10:04
  • sorry, I have tried,but I don't know how to assign value for form_data? any hints? – Henrik Kwok Jul 11 '20 at 10:41
  • you can send `myArray` because it will have all values which you needed . Also check [this](https://stackoverflow.com/questions/10955017/sending-json-to-php-using-ajax) to send json data to php. – Swati Jul 11 '20 at 11:50

0 Answers0