【发布时间】:2013-12-30 10:33:33
【问题描述】:
您好,我有一个这样的 PHP 脚本:
<?php
require('includes/db-core.php');//My DB info
$dbh = mf_connect_db();//Creates a connection object
$newObjs = array();
for($i=0;$i<6;$i++){
array_push($newObjs,"data".$i);
}
try {
for($i=0; i<sizeof($newObjs); $i++){
$stmt2 = $dbh->prepare("INSERT INTO companies_table (data1, data2, data3, data4, data5, data6) VALUES (?, ?, ?, ?, ?, ?)");
$stmt2->bindParam(1, $newObjs[$i]);
$stmt2->bindParam(2, $newObjs[$i]);
$stmt2->bindParam(3, $newObjs[$i]);
$stmt2->bindParam(4, $newObjs[$i]);
$stmt2->bindParam(5, $newObjs[$i]);
$stmt2->bindParam(6, $newObjs[$i]);
$stmt2->execute();
}
echo "New Row successfully added";
}catch( Exception $e ){
echo 'Query error : ', $e->getMessage();
}
?>
我收到以下错误:查询错误:SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'data1' cannot be null,但数据插入正确,我不知道为什么会这样(错误 + 插入)。
我的表的所有字段都不为空。
没有人注意到我的这个错误:
try {
for($i=0; i<sizeof($newObjs); $i++){
我忘记了 $ 符号,真的很抱歉,但这有助于学习一点:感谢所有提供答案的用户,但下次最好先检查帖子是否有错误。
谢谢
【问题讨论】:
-
var_dump $newObjs[$i] 告诉我们你有什么?
-
@AwladLiton,不需要,因为它显然可以从上面的循环中获得。它将是
data1 , data2 .... data6 -
是的,我检查过了,它是 data1 ....