gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 23:44:03 25 4
gpt4 key购买 nike

每次运行我的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com