难以使用while循环插入正确的数据

时间:2013-08-22 14:39:05

标签: php mysql

enter image description here

enter image description here 好的,这张图片显示了使用while循环从数据库中检索的表。单击X按钮后,所选行将进入另一个名为trade_history1的表。简单吧?但现在,如果您可以查看当前价格和行的盈利/亏损,请说....如果我点击订单号148,整行将出现在trade_history表中。对?但现在它正在做的是他们没有提取正确的当前价格(列)和盈利/亏损(列)。相反,他们总是将当前价格和盈利/亏损的最后一行移动到第148行。有办法解决这个问题吗?

参考代码

while echo for echo

     $query = "SELECT * FROM opentrades where user_id =". $userid; //You don't need a ; like you do in SQL
$result = mysql_query($query);
echo "<table>"; // start a table tag in the HTML
echo "<tr><td>" . "Order Number" . "</td><td>" . "Selection" . "</td><td>" . "Date" . "</td><td>" . "Type" . "</td><td>" . "Size" . "</td><td>" . "Bid Price" . "</td><td>" . "Offer Price" . "</td><td>" ."Stop Loss" . "</td><td>" . "Take Profit" . "</td><td>" . "Current Price" . "</td><td>" ."Profit/Loss(USD)"."</td><td>" ."Close"."</td></tr>" ;  //$row['index'] the index here is a field name

while($row = mysql_fetch_assoc($result)){   //Creates a loop to loop through results


  if ($row['selection']=='eur/usd')// TO RETRIEVE BID AND OFFER FOR EACH ROW
          {
      $bidpricepl=$bid;

      $offerpricepl=$bid1;

      }
  elseif ($row['selection']=='usd/jpy')
      {
      $bidpricepl=$bid2;

      $offerpricepl=$bid3;

      }
  elseif ($row['selection']=='usd/cad')
      {
      $bidpricepl=$bid4;

      $offerpricepl=$bid5;

      }
  elseif ($row['selection']=='eur/jpy')
      {
      $bidpricepl=$bid6;

      $offerpricepl=$bid7;

      }
  elseif ($row['selection']=='eur/chf')
      {
      $bidpricepl=$bid8;

      $offerpricepl=$bid9;

      }
  elseif ($row['selection']=='gbp/usd')
      {
      $bidpricepl=$bid10;

      $offerpricepl=$bid11;

      }
  elseif ($row['selection']=='aud/usd')
      {
      $bidpricepl=$bid12;

      $offerpricepl=$bid13;

      }
  elseif ($row['selection']=='usd/chf')
      {
      $bidpricepl=$bid14;

      $offerpricepl=$bid15;

      }
  if ($row['type']=="buy")
    {
      //$last3charsoffer = substr($row['offerprice'], -6);
      //$offernodecimal = str_replace('.', '', $last3charsoffer);
      //$last3charsoffer1 = substr($offerpricepl, -6);
      //$offernodecimal1 = str_replace('.', '', $last3charsoffer1);

      //$pips2 = ltrim($pips2, '0');
      //$calcpips2=$calcpips/$minipipskiller;
      //$last3charsoffer = substr($row['offerprice'], -6);
      //$offernodecimal = str_replace('.', '', $last3charsoffer);
      //$last3charsoffer1 = substr($offerpricepl, -6);
      //$offernodecimal1 = str_replace('.', '', $last3charsoffer1);
      //$minipipskiller='10';
      //$offeropen=$row['offerprice'];// to define variable
      //$pips=$offerpricepl-$offeropen;// to calculate difference STEP 1
      //$calcpips = str_replace('.', '', $pips); //removing the deci
      //$calcpips = ltrim($calcpips, '0');// remove zeros in front
      //$calcpips2=$calcpips/$minipipskiller;// to divide by 10 to cut mini pips  
      $minipipskiller='10';
      $offeropen=$row['offerprice'];
      $pips=$offerpricepl-$offeropen;
      $closedb=$offerpricepl;
      $pips1=round($pips, 6);
      $pips2 = str_replace('.', '', $pips1);
        if ($pips2<0)
      {
        $pips2 = str_replace('-', '', $pips2);
        $pips2 = ltrim($pips2, '0');
        $pips2 = -1 * abs($pips2);
      }
      else {
        $pips2 = ltrim($pips2, '0');
      }
      $pips3=$pips2/$minipipskiller;
    }// PIP COUNTING
  elseif ($row['type']=="sell")//FOR PIP COUNTING
    {
      //$last3charsbid = substr($row['bidprice'], -6);
      //$bidnodecimal = str_replace('.', '', $last3charsbid);
      //$last3charsbid1 = substr($bidpricepl, -6);
      //$bidnodecimal1 = str_replace('.', '', $last3charsbid1);
      $minipipskiller='10';
      $bidopen=$row['bidprice'];
      $pips=$bidopen-$bidpricepl;
      $closedb=$bidpricepl;
      $pips1=round($pips, 6);
      $pips2 = str_replace('.', '', $pips1);
      if ($pips2<0)
      {
        $pips2 = str_replace('-', '', $pips2);
        $pips2 = ltrim($pips2, '0');
        $pips2 = -1 * abs($pips2);
      }
      else {
        $pips2 = ltrim($pips2, '0');
      }
      $pips3=$pips2/$minipipskiller;
    }
    //echo $pips3;


      $ticksize= "0.0001";// FOR PROFIT AND LOSS
      $lot1 = "100000";
      $sizecalc=$row['size'] * $lot1;

        if ($row['type']=="buy")
      { 
        $profitandloss=$sizecalc*$ticksize*$pips3; //per TRADE
      }
      if ($row['type']=="sell")
      {
        $profitandloss=$sizecalc*$ticksize*$pips3; //per TRADE
      }
      //echo $lot1;
      //echo $ticksize;
      number_format($profitandloss, 2);

        echo "<tr><td>" . $row['trade_id'] .         
                "</td><td>" . $row['selection'] . 
                "</td><td>" . $row['date'] .
                "</td><td>" . $row['type'] .
                "</td><td>" . $row['size'] .
                "</td><td>" . $row['bidprice'] .
                "</td><td>" . $row['offerprice'] .
                "</td><td>" . $row['stoploss'] .
                "</td><td>" . $row['takeprofit'] .
                "</td><td>" . $closedb . 
                "</td><td>" . $profitandloss . 
                "</td><td><a href ='delete.php?id=".
                $row['trade_id']."'>X</a>
               </td></tr>"; 

        }
     function callvariable($closedb,$profitandloss)
        {
          $variable=array('$closedb','$profitandloss');

        return $variable;
        } 

SQL

     <?php 

require_once('connect.php');
        require_once 'refreshable.php';
         require_once "../core/init.php"; 

            require_once 'query.php';

        $trade_id= $_GET['id'];
           date_default_timezone_set("Asia/Singapore");
        $date = date('m/d/Y h:i:s a', time());
        $timezone = date_default_timezone_get();

       $userid=$user_data['user_id'];
        $var=callvariable($closedb,$profitandloss);




$mysqli = new mysqli($database_hostname, $database_username, $database_password, $database_name) or exit("Error connecting to database");
try {
    // First of all, let's begin a transaction
    $mysqli->autocommit(FALSE);

    // A set of queries; if one fails, an exception should be thrown
    $mysqli->query("INSERT INTO `trade_history1` (user_id, trade_id, selection, date, type, size, bidprice, offerprice, stoploss, takeprofit,dateclose,close,profitandloss)
    SELECT user_id, trade_id, selection, date, type, size, bidprice, offerprice, stoploss, takeprofit, 'null','null','null'
    FROM `opentrades`
    WHERE `trade_id` = " . $trade_id);
    $mysqli-> query("UPDATE `trade_history1` SET `dateclose` = CURRENT_TIMESTAMP,
                                             `close` = '{$closedb}',
                                             `profitandloss` = '{$profitandloss}'
                                            WHERE `trade_id`= ".$trade_id);


    $mysqli->query("DELETE FROM `opentrades` WHERE `trade_id` = '$trade_id'");
    $mysqli->query("UPDATE `users`  SET `balance` = '{$balance}' WHERE `user_id` =".$userid);



    // If we arrive here, it means that no exception was thrown
    // i.e. no query has failed, and we can commit the transaction
    $mysqli->commit();
    $_SESSION['message'] = 'Successfully deleted';
} catch (Exception $e) {
    // An exception has been thrown
    // We must rollback the transaction
    $_SESSION['message'] = 'Unable to delete';
    $mysqli->rollback();

}


$mysqli->close();

header('location:test.php');
?>

0 个答案:

没有答案