0

目的のテーブルに挿入してから削除する方法を使用しようとしましたselect * from XXXが、新しいテーブルに挿入して古いテーブルのテーブルを削除できないようです。

trade_idas を読み取ります1,2,3,4が、削除できないようです。 4行目にエラーがあり、 trade_id が定義されていない理由を取得できないようです。

クエリに何か問題がありますか?

削除.php

<?php

        // we need to know the student id so that we can delete the right student
        $tradeid= $_GET['trade_id'];

        // the file that contains your database credentials like username and password
        require_once('connect.php');

        // see Lecture Webp_Week13_14_Using_PHPandMySQL(updating).pptx Slide 4 aka Step 1
        $mysqli = new mysqli($database_hostname, $database_username, $database_password, $database_name) or exit("Error connecting to database"); 

        // Slide 5 aka Step 2
        $stmt = $mysqli->multi_query("INSERT INTO `trade_history1` (session_id, trade_id, selection, date, type, size, bidprice, offerprice, stoploss, takeprofit, profitandloss, dateclose, close)
        SELECT session_id, trade_id, selection, date, type, size, bidprice, offerprice, stoploss, takeprofit, profitandloss, dateclose, close
        FROM `opentrades`
        WHERE `trade_id` = ?;

          DELETE FROM `opentrades` WHERE `trade_id` = ?;
          COMMIT;"); 

        // Slide 6 aka Step 3 the bind params must correspond to the ?
        $stmt->bind_param("i", $tradeid); // 1 ? so we use i. we use i because  id is INT

        // Slide 7 aka Step 4
        $successfullyDeleted = $stmt->execute(); 

        // Slide 8 aka Step 5
        // we won't check the delete result here.

        // Slide 9 aka Step 6 and 7
        $stmt->close();

        $mysqli->close();

        // if we successfully delete this, we 
        if ($successfullyDeleted) {
            $_SESSION['message'] = 'Successfully deleted';
        } else {
            $_SESSION['message'] = 'Unable to delete';
        }

        header('Location: js.php');

?>

コードの重要な部分

JS.php

while($row = mysql_fetch_array($result)){   //Creates a loop to loop through results
    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>" . $row['profitandloss'] . "</td><td><a href ='delete.php?id=".$row['trade_id']."'>X</a></td></tr>";  //$row['index'] the index here is a field name
}
4

1 に答える 1

1

mysqli::multi_query 、データベースに対してクエリ (1 つまたは複数のクエリ) を実行します。mysqli_stmt::bind_paramで使用され、 mysqli_stmt::executeで実行される準備済みステートメント ( mysqli::prepareなど)は作成しません。さらに、準備されたステートメントのクエリ パラメータは、単一の SQL ステートメントで構成されている必要があります。

この方法でトランザクションを実行する必要があります (準備されたステートメントはありません!) ( PHP + MySQL トランザクションの例から取得):

$tradeid= filter_var($_GET['trade_id'], FILTER_SANITIZE_NUMBER_INT);
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->begin_transaction();

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

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

または準備済みステートメントを使用する ( 「準備済みステートメントを PHP のトランザクションと組み合わせて使用​​するにはどうすればよいですか?」から):

$tradeid= $_GET['trade_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->begin_transaction();

    // A set of queries; if one fails, an exception should be thrown
    $stmt =  $mysqli->stmt_init();
    $stmt = $stmt->prepare("INSERT INTO `trade_history1` (session_id, trade_id, selection, date, type, size, bidprice, offerprice, stoploss, takeprofit, profitandloss, dateclose, close)
    SELECT session_id, trade_id, selection, date, type, size, bidprice, offerprice, stoploss, takeprofit, profitandloss, dateclose, close
    FROM `opentrades`
    WHERE `trade_id` = ?");
    $stmt->bind_param("i", $tradeid);
    $stmt->execute();

    $mysqli->query("DELETE FROM `opentrades` WHERE `trade_id` = ?");
    $stmt->bind_param("i", $tradeid);
    $stmt->execute();

    // 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();
...
于 2013-08-11T19:36:35.427 に答える