Fatal error: Uncaught PDOException: Column cannot be null (BUT it isn't null...)

Fatal error: Uncaught PDOException: Column cannot be null (BUT it isn't null...)

每当我的 PHP 脚本是 运行 时,我都会遇到致命错误。尽管如此,所有数据仍然上传到数据库。当回显$values[0]时,没有NULL值作为错误状态,一切正常。我很困惑。

错误

[02-Oct-2018 19:59:54 America/Vancouver] PHP Warning:  Invalid argument supplied for foreach() in /home1/antonfa1/public_html/trading-history/process.php on line 22
[02-Oct-2018 19:59:54 America/Vancouver] PHP Fatal error:  Uncaught PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'trade_date' cannot be null in /home1/antonfa1/public_html/trading-history/process.php:48
Stack trace:
#0 /home1/antonfa1/public_html/trading-history/process.php(48): PDOStatement->execute(Array)
#1 {main}
  thrown in /home1/antonfa1/public_html/trading-history/process.php on line 48

奇怪的是,我昨天好像没有收到这个警告和错误,所以我把我之后所做的所有修改都注释掉了,但问题仍然存在。

脚本

include 'includes/connect.php';
$stri = 'INSERT INTO trades (trade_date, trade_time, trade_datetime, trade_transaction, trade_symbol, trade_status, trade_quantity, trade_filled, trade_price, trade_value) VALUES (:date, :time, :datetime, :transaction, :symbol, :status, :quantity, :filled, :price, :value)';

$file = fopen($_SESSION['file'], 'r');

while (!feof($file)) {
    
  $values = [];
  
  foreach (fgetcsv($file) as $key => $value) {
    array_push($values, $value);
  }
  
  echo $values[0] . '<br>';
  
  $stat = $conn->prepare($stri);
  $stat->execute([
    'date' => $values[0],
    'time' => $values[1],
    'datetime' => $values[2],
    'transaction' => $values[3],
    'symbol' => $values[4],
    'status' => $values[5],
    'quantity' => $values[6],
    'filled' => $values[7],
    'price' => $values[8],
    'value' => $values[9],
  ]);
}

fgetcsv($file) as $key => $value真的是一个无效的参数吗?这是可能导致此“错误”错误的原因吗?我昨天没有收到这个警告:/

回声

2018-10-02
2018-10-02
2018-10-02
2018-10-02
2018-10-02
2018-10-02
2018-10-02
2018-10-02
2018-10-02
2018-10-02
2018-10-02
2018-10-02
2018-10-02
2018-10-02

所有数据点都在那里,none 是 NULL...

你的PDO参数需要这样传递:

$statement->execute([
    ':foo' => 1,
    ':bar' => '2001-01-01',
    ':baz' => 42
 ]);

您缺少传递给 PDOStatement::execute

的密钥的 : 部分

您的问题是 fgetcsv() returns false 当它到达文件末尾时...

fgetcsv() returns NULL if an invalid handle is supplied or FALSE on other errors, including end of file.

您可以像使用 feof() 一样使用 fgetcsv(),只在有记录要查找时循环。

例如

$stri = <<<_SQL
INSERT INTO trades (
  trade_date, 
  trade_time, 
  trade_datetime, 
  trade_transaction, 
  trade_symbol, 
  trade_status, 
  trade_quantity, 
  trade_filled, 
  trade_price, 
  trade_value
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
_SQL; // note the positional placeholders

$stat = $conn->prepare($stri); // prepare once, not in a loop
$file = fopen($_SESSION['file'], 'r');

while($values = fgetcsv($file)) { // EOF will return false and exit the loop
    if (count($values) === 10) {
        echo $values[0] . '<br>';
        $stat->execute($values);
    } else {
        echo 'Skipping invalid record';
    }
}
fclose($file);

如果使用位置占位符,则不需要为预处理语句构造关联数组。