0

如何获取查询以将我的行从一个表插入到另一个表?我使用常规的插入选择删除。现在起作用的是删除交易。但是新表中的值没有更新。我在第二个表中使用了一个常量,因为这些表有不匹配的列,这可能是什么问题?

<?php 
//If(!isset($trade_id)){
        $trade_id= $_GET['id'];

//}

require_once('connect.php');
$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` (session_id, trade_id, selection, date, type, size, bidprice, offerprice, stoploss, takeprofit,dateclose,close, profitandloss)
    SELECT session_id, trade_id, selection, date, type, size, bidprice, offerprice, stoploss, takeprofit, profitandloss,'null','null'
    FROM `opentrades`
    WHERE `trade_id` = " . $trade_id);
    $mysqli->query("DELETE FROM `opentrades` WHERE `trade_id` = " . $trade_id);

    // 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();

}


        // if we successfully delete this, we
$mysqli->close();

// bid price,offer price, size,type,
header('location:js.php');
?>
4

3 回答 3

0

失败的查询不会引发异常。您已回显该查询并在 phpmyadmin 中执行它。像这样你可以钻到底部。

mysqli_error()在删除行之前,您必须使用来验证插入查询是否正确执行。

于 2013-08-12T18:46:08.350 回答
0

我不知道 sql 查询中的常量,但它似乎是查询中的插入和选择顺序,因为您使用最后四个作为:

takeprofit, dateclose, close,  profitandloss

应该是有序的

takeprofit, 'null', 'null', profitandloss
于 2013-08-12T18:23:45.347 回答
0
  use following code
        mysqli_execute();instead of mysqli_query();



            $mysqli->execute("INSERT INTO `trade_history1` (session_id, trade_id, selection, date, type, size, bidprice, offerprice, stoploss, takeprofit,dateclose,close, profitandloss)
SELECT session_id, trade_id, selection, date, type, size, bidprice, offerprice, stoploss, takeprofit, profitandloss,'null','null'
FROM `opentrades`
WHERE `trade_id` = " . $trade_id);
$mysqli->execute("DELETE FROM `opentrades` WHERE `trade_id` = " . $trade_id);
于 2013-08-12T18:22:20.870 回答