Ошибка Undefined index

113
07 декабря 2020, 23:50

Есть скрипт, на создание ордеров на продажу и покупку и дальнейшее добавление их в таблицу на сайте через ajax. Ордера на покупку создаются без проблем, но при попытке создать ордер на продажу, в логах появляется ошибка Undefined index: commission на строке $New_val_free = $Wallets_row[$name_val_free] + ($total - $order_row['commission']); Уже несколько часов сижу и не могу понять в чем ошибка. Вот полный код скрипта:

<?php
    ini_set('display_errors','Off');
    session_start();
    include_once('db_connect.php');
    if (isset($_SESSION['userid'])) {
        $sesion_id = $_SESSION['userid'];
    }
    else{
        //тут переход на домашнюю страницу
    }
    function close_new_order($mysqli, $order_id, $ask, $bid){
        $order_sql = $mysqli->query("SELECT `order_id`, `user_id`, `contest_id`, `type_operation`, `name_val`, `name_prod`, `price`/100000000 price, `amount`/100000000 as amount, `total`/100000000 as total, `commission`/100000000 as commision, `dttm_create_order`, `dttm_close_order`, `status` FROM `orders` WHERE order_id='$order_id'");
        $order_row = $order_sql->fetch_assoc();
        if (($order_row['type_operation']=='BUY') && ($order_row['price']>=$ask)) {
            $Wallets = $mysqli->query("SELECT `user_id`, `contest_id`, `balbtc`/100000000 as balbtc, `balibtc`/100000000 as balibtc, `balusd`/100000000 as balusd, `baliusd`/100000000 as baliusd FROM `wallets` WHERE user_id = '".$order_row['user_id']."' AND contest_id = '".$order_row['contest_id']."'");
            $Wallets_row = $Wallets->fetch_assoc();
            $total = $order_row['amount'] * $order_row['price'];
            $name_val_in_orders = "bali" . mb_strtolower($order_row['name_val']);
            $name_prod_free     = "bal" . mb_strtolower($order_row['name_prod']);
            $New_val_in_orders = $Wallets_row[$name_val_in_orders] - ($total + $order_row['commission']);
            $New_prod_free = $Wallets_row[$name_prod_free] + $order_row['amount'];
            $mysqli->query("UPDATE `wallets` SET $name_prod_free=$New_prod_free*100000000, $name_val_in_orders=$New_val_in_orders*100000000 WHERE user_id = '".$order_row['user_id']."' AND contest_id = '".$order_row['contest_id']."'");
            $mysqli->query("UPDATE `orders` SET status=1 WHERE order_id= '".$order_id."'");
            return 301;
        }
        if (($order_row['type_operation']=='SELL') && ($order_row['price']<=$bid)) {
        //  $Wallets = $mysqli->query("SELECT * FROM `wallets` WHERE user_id = '".$order_row['user_id']."' AND contest_id = '".$order_row['contest_id']."'");
        //  $Wallets_row = $Wallets->fetch_assoc();
            $Wallets = $mysqli->query("SELECT `user_id`, `contest_id`, `balbtc`/100000000 as balbtc, `balibtc`/100000000 as balibtc, `balusd`/100000000 as balusd, `baliusd`/100000000 as baliusd FROM `wallets` WHERE user_id = '".$order_row['user_id']."' AND contest_id = '".$order_row['contest_id']."'");
            $Wallets_row = $Wallets->fetch_assoc();

            $total = $order_row['amount'] * $order_row['price'];
            $name_val_free       = "bali" . mb_strtolower($order_row['name_val']);
            $name_prod_in_orders = "bal" . mb_strtolower($order_row['name_prod']);
         //   $New_val_free       = $Wallets_user[$name_val_free] + ($total - $order_row['commission']);
         //   $New_prod_in_orders = $Wallets_user[$name_prod_in_orders] - $order_row['amount'];
              $New_val_free       = $Wallets_row[$name_val_free] + ($total - $order_row['commission']);
              $New_prod_in_orders = $Wallets_row[$name_prod_in_orders] - $order_row['amount'];


            $mysqli->query("UPDATE `wallets` SET $name_prod_in_orders=$New_prod_in_orders*100000000, $name_val_free=$New_val_free*100000000 WHERE user_id = '".$order_row['user_id']."' AND contest_id = '".$order_row['contest_id']."'");
            $mysqli->query("UPDATE `orders` SET status = 1 WHERE order_id = '".$order_id."'");
            return 302;
        }
    }
    function create_orders($mysqli,$user_id,$contest_id,$price,$amount,$name_val,$name_prod,$type_operation, $ask, $bid){
        $Wallets_user = $mysqli->query("SELECT `user_id`, `contest_id`, `balbtc`/100000000 as balbtc, `balibtc`/100000000 as balibtc, `balusd`/100000000 as balusd, `baliusd`/100000000 as baliusd FROM `wallets` WHERE user_id=$user_id AND contest_id = $contest_id");
        $Wallets_user_row = $Wallets_user->fetch_assoc();

        $total = $amount * $price;
        $commission = 0.0001 * $amount * $price;
        if ($type_operation == 'BUY'){
            $name_val_free  = "bal" . mb_strtolower($name_val);
            $name_val_in_orders = "bali" . mb_strtolower($name_val);
            $val_free_new  = ($Wallets_user_row[$name_val_free] - ($total+$commission));
            $val_in_orders_new = ($Wallets_user_row[$name_val_in_orders] + ($total+$commission));
            if (($val_free_new >= 0) && ($val_in_orders_new >= 0)) {
                $DTTM_create_order = time(); 
                $mysqli->query("INSERT INTO `orders` (`user_id`,`contest_id`,`type_operation`,`name_val`,`name_prod`,`price`,`amount`,`total`,`commission`,`dttm_create_order`,`status`) VALUES ('$user_id','$contest_id','$type_operation','$name_val','$name_prod','$price'*100000000,'$amount'*100000000,'$total'*100000000,'$commission'*100000000,'$DTTM_create_order',0)");
                $Id_New_Order=mysqli_insert_id($mysqli);
                $mysqli->query("UPDATE `wallets` SET $name_val_free=$val_free_new*100000000, $name_val_in_orders=$val_in_orders_new*100000000 WHERE user_id=$user_id AND contest_id = $contest_id" ); 
                $resp=close_new_order($mysqli, $Id_New_Order, $ask, $bid);
                $response_code = 101;
                return $response_code;
            }
            else{
                $response_code = -101;
                return $response_code;              
            }
        }
        if ($type_operation == 'SELL'){
            $name_prod_free  = "bal" . mb_strtolower($name_prod);
            $name_prod_in_orders = "bali" . mb_strtolower($name_prod);
            $prod_free_new  = ($Wallets_user_row[$name_prod_free] - $amount);
            $prod_in_orders_nem = ($Wallets_user_row[$name_prod_in_orders] + $amount);
            if (($prod_free_new >= 0) && ($prod_in_orders_nem >= 0)) {
                $DTTM_create_order = time();
                $mysqli->query("INSERT INTO `orders` (`user_id`,`contest_id`,`type_operation`,`name_val`,`name_prod`,`price`,`amount`,`total`,`commission`,`dttm_create_order`,`status`) VALUES ('$user_id','$contest_id','$type_operation','$name_val','$name_prod','$price'*100000000,'$amount'*100000000,'$total'*100000000,'$commission'*100000000,'$DTTM_create_order',0)");
                $Id_New_Order=mysqli_insert_id($mysqli);
                $mysqli->query("UPDATE `wallets` SET $name_prod_free=$prod_free_new*100000000, $name_prod_in_orders=$prod_in_orders_nem*100000000 WHERE user_id=$user_id AND contest_id = $contest_id");
                $resp=close_new_order($mysqli, $Id_New_Order, $ask, $bid);
                $response_code = 102;
                return $response_code; 
            }
            else{
                $response_code = -102;
                return $response_code;              
            }
        }
    }
//Приём переменных
$contest_id     = $_POST['Contest'];
$price          = $_POST['Price'];
$amount         = $_POST['Amount'];
$name_val       = $_POST['Val'];
$name_prod      = $_POST['Prod'];
$type_operation = $_POST['Type'];

$Prices_bd = $mysqli->query("SELECT * FROM `coinInfo` WHERE coinName='$name_prod'");
$Prices    = $Prices_bd->fetch_assoc();
$Responce=create_orders($mysqli,$sesion_id,$contest_id,$price,$amount,$name_val,$name_prod,$type_operation, $Prices['ask'], $Prices['bid']);
Answer 1

Добавьте s в SQL запросе:

//                             vv
`commission`/100000000 as commission
READ ALSO
Как передать id в ajax по клику

Как передать id в ajax по клику

Делаю корзину, много товаров, хотел прикрутить ajax на тык по кнопке, не могу понять как передать значение $cartItem->rowId для удаления выбранного...

159
Websocket is not defined / WORKERMAN

Websocket is not defined / WORKERMAN

Делаю вебсокеты на php с workermanЗапускается на сервере успешно, но прослушать не могу

151
Взаимодействие webview приложения и бд

Взаимодействие webview приложения и бд

Создаю cordova wibview приложение и мне нужно подключить к нему бд, как это сделать?

152
Заменить фразу по слову

Заменить фразу по слову

Данная конструкция заменяет лишь одно слово из фразы:

144