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

标签 php mysql pdo prepared-statement fgetcsv

每次运行我的 PHP 脚本时,我都会遇到 fatal error 。尽管如此,所有数据仍然上传到数据库。当回显$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 真的是无效参数吗?这是可能导致此“错误”错误的原因吗?我昨天没有收到这个警告:/

echo

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

所有的数据点都在那里,没有一个是 NULL...

最佳答案

您的问题是 fgetcsv() 在到达文件末尾时返回 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);

如果使用位置占位符,则不需要为准备好的语句构造关联数组。

关于php - fatal error : Uncaught PDOException: Column cannot be null (BUT it isn't null. ..),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52619159/

相关文章:

php - 对特定元素发出 ajax 请求的最有效方法

php - 我如何获取数据库中字段名(名称)中的所有数据并将其存储在变量数组中?在 PHP 和 MYSQL 中

php - 检查2个表中是否存在数据

php - 如何从 UserFrosting 的另一个类中访问配置变量?

PHP MySQL : Saving PDF to Database

PHP 多维数组 foreach

php - 使用CodeIgniter进行Sql插入、选择

php - 如何正确地内爆数组以在 mySQL 中实现 REGEXP?

php - 一次插入多行。这怎么能缩短呢?

php - MySql WHERE 子句是否有通配符?