致命错误:uncaughtpdoexception:列不能为null(但它不是null…)

7cjasjjr  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(349)

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

错误

  1. [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
  2. [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
  3. Stack trace:
  4. # 0 /home1/antonfa1/public_html/trading-history/process.php(48): PDOStatement->execute(Array)
  5. # 1 {main}
  6. thrown in /home1/antonfa1/public_html/trading-history/process.php on line 48

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

脚本

  1. include 'includes/connect.php';
  2. $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)';
  3. $file = fopen($_SESSION['file'], 'r');
  4. while (!feof($file)) {
  5. $values = [];
  6. foreach (fgetcsv($file) as $key => $value) {
  7. array_push($values, $value);
  8. }
  9. echo $values[0] . '<br>';
  10. $stat = $conn->prepare($stri);
  11. $stat->execute([
  12. 'date' => $values[0],
  13. 'time' => $values[1],
  14. 'datetime' => $values[2],
  15. 'transaction' => $values[3],
  16. 'symbol' => $values[4],
  17. 'status' => $values[5],
  18. 'quantity' => $values[6],
  19. 'filled' => $values[7],
  20. 'price' => $values[8],
  21. 'value' => $values[9],
  22. ]);
  23. }

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

回声

  1. 2018-10-02
  2. 2018-10-02
  3. 2018-10-02
  4. 2018-10-02
  5. 2018-10-02
  6. 2018-10-02
  7. 2018-10-02
  8. 2018-10-02
  9. 2018-10-02
  10. 2018-10-02
  11. 2018-10-02
  12. 2018-10-02
  13. 2018-10-02
  14. 2018-10-02

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

rwqw0loc

rwqw0loc1#

你的问题是 fgetcsv() 退货 false 当它到达文件末尾时。。。
如果提供的句柄无效,fgetcsv()将返回null;如果出现其他错误(包括文件结尾),则返回false。
你可以用 fgetcsv() 和你用的方法一样 feof() 只在有记录要查找时循环。
例如

  1. $stri = <<<_SQL
  2. INSERT INTO trades (
  3. trade_date,
  4. trade_time,
  5. trade_datetime,
  6. trade_transaction,
  7. trade_symbol,
  8. trade_status,
  9. trade_quantity,
  10. trade_filled,
  11. trade_price,
  12. trade_value
  13. ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
  14. _SQL; // note the positional placeholders
  15. $stat = $conn->prepare($stri); // prepare once, not in a loop
  16. $file = fopen($_SESSION['file'], 'r');
  17. while($values = fgetcsv($file)) { // EOF will return false and exit the loop
  18. if (count($values) === 10) {
  19. echo $values[0] . '<br>';
  20. $stat->execute($values);
  21. } else {
  22. echo 'Skipping invalid record';
  23. }
  24. }
  25. fclose($file);

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

展开查看全部
gopyfrb3

gopyfrb32#

您的pdo参数需要这样传递:

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

你错过了 : 部分密钥传递给 PDOStatement::execute

相关问题