0

How do i get the query to insert my row from one table to another? I use the conventional insert select delete . Now the thing that works is deleting the trade. however the values are not updating in the new table. I used a constants in the second table because the tables have unmatched columns , what could be the problem here?

<?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');
?>
Nigel How
  • 53
  • 1
  • 8
  • 1
    You're not checking the return code from `query`. Are you sure it'll throw an exception on failure? http://stackoverflow.com/questions/14578243/turning-query-errors-to-exceptions-in-mysqli – rutter Aug 12 '13 at 18:22
  • I'd suggest echoing out the query and running it in the database directly. Does it work? You're also not checking any return values from your `query()` calls, so you don't know if anything is failing. – andrewsi Aug 12 '13 at 18:22

3 Answers3

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);
alok.kumar
  • 380
  • 3
  • 11
0

I have no idea about constants in sql query but it seems the order of insertion and selection in query as you are using last four as:

takeprofit, dateclose, close,  profitandloss

should be in order

takeprofit, 'null', 'null', profitandloss
Vineet1982
  • 7,730
  • 4
  • 32
  • 67
0

A query that fails does not throw an exception. You have echo that query and execute it in phpmyadmin. Like this you can drill to the bottom.

You must use mysqli_error() to verify if the insert query executed correctly, before deleting the row.

Lorenz Meyer
  • 19,166
  • 22
  • 75
  • 121