-1

データベースにデータを挿入する際に問題が発生しました:

define('SECURE', true);
include "storescripts/connect_to_mysql.php";

    $txn_id = 123456789101234567;
    $payer_email = "irgendwas@gmx.de";
    $mc_gross = "amount";

    $sql = "SELECT COUNT(*) AS count FROM `trans` WHERE `txn_id` = $txn_id";
    $q = mysqli_query($mysqli, $sql);
    $f = mysqli_fetch_array($q);

    if($f['count'] > 0) {
        echo "Transaction already processed"; 
    } else { 
        $insert = mysqli_query($mysqli, "INSERT INTO trans (`txn_id`, `payer_email`,`mc_gross`) 
                                                    VALUES ($txn_id,$payer_email,$mc_gross)");
        if($insert = 1) {
            echo "inserted";
        } else {
            echo "not inserted";
        }
    }

その結果、「挿入されました」と表示されますが、データベースにデータがありません。誰か助けてください。バグはどこですか?

編集:これは私のテーブルです:

define('SECURE', true);
require "connect_to_mysql.php";  
$sqlCommand = "CREATE TABLE trans (
             id int(11) NOT NULL auto_increment,
             txn_id varchar(255) NOT NULL,
             payer_email varchar(255) NOT NULL,
             mc_gross int(255) NOT NULL,
             PRIMARY KEY (id),
             UNIQUE KEY (txn_id))";
if ($mysqli->query($sqlCommand)) { 
    echo "Your trans table has been created successfully!"; 
} else { 
    echo "CRITICAL ERROR;".$mysqli->error; 
}
4

2 に答える 2

0

「挿入」される理由は、if が変数を 1 に設定し、結果が true になるためです。比較には double equals を使用します。

右:

if ($insert == 1)

違う:

if ($insert = 1)

あなたのSQLに関する限り、クエリにエラーがあるようです。$txn_id と $payer_email はどちらも varchar であり、文字列であるため引用符を使用する必要があります

于 2013-08-01T18:51:40.350 に答える