pdo-insert-on-duplicate-key-update只插入和更新6行

xvw2m8pv  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(302)

我使用foreach循环获取数据并插入到数据库中。
当我使用插入。。。更新时只插入6行而不是31行。
如果我使用just insert而不使用on update,它将插入所有31条记录。
数据库看起来像这样

代码

foreach ($data as $key => $object) {
$name = $object->name;
$macd = $object->macd;
$rsi = $object->rsi;
$heikin = $object->heikin;
$atp =  $object->atp;
$ebd = $object->ebd;
$date = $object->date;

$stmt = $dbh->prepare("INSERT INTO daily_analyse (name, macd, rsi, heikin, atp, ebd, date) VALUES (:name, :macd, :rsi, :heikin, :atp, :ebd, :date) 
    ON DUPLICATE KEY UPDATE name = VALUES(name), macd = VALUES(macd), rsi = VALUES(rsi), heikin = VALUES(heikin), atp = VALUES(atp), ebd = VALUES(ebd), date = VALUES(date)");

$stmt->bindParam(':name', $name);
$stmt->bindParam(':macd', $macd);
$stmt->bindParam(':rsi', $rsi);
$stmt->bindParam(':heikin', $heikin);
$stmt->bindParam(':atp', $atp);
$stmt->bindParam(':ebd', $ebd);
$stmt->bindParam(':date', $date);
$stmt->execute(); 
}

我得到这个错误
错误:异常“pdoexception”,消息为“sqlstate[23000]:完整性约束冲突:1062/home/test中键“date”的重复条目“7/13/2018”。php:39 stack trace:#0/home/test.php(39):pdostatement->execute()#1{main}
样本数据

User1, -9.4709688279058, 65.485783538714, 15.102926302645, 2071.5210433061, -39.057303370785, 7/13/2018
User2, -7.0387293434146, 74.369738650677, 22.950927744359, 2346.1245293301, 16.224382022472, 7/13/2018
User3, -12.262210050659, 65.794347000194, 11.766792820541, 2087.7169870647, -39.007303370786, 7/12/2018
User4, -18.563885914778, 40.972854885722, 1.970264705054, -2486.2857541652, -75.724044943821, 7/13/2018
User5, -9.2538092463374, 66.801519101633, 14.957188794467, 1993.7038310694, -10.75202247191, 7/12/2018
User6, -15.751961165625, 50.113579295874, 12.384504645753, 2253.4898090767, -70.362696629214, 7/13/2018
User7, -29.527566236053, 21.183011205795, -2.7274771575057, -2078.4759312902, -154.75247191011, 7/13/2018
User8, -3.0264511275172, 54.994147779482, 14.876326517899, 2331.1682663662, -32.582359550562, 7/13/2018
User9, -20.750921244733, 59.249132289786, 23.670372635365, 2962.710928428, -91.891797752808, 7/13/2018
User10, -27.932548522575, 38.891513214047, 6.66911663577, -2248.6323934174, -122.47876404494, 7/13/2018
User11, 11.174244482895, 59.805125598132, -1.1108473312506, 1850.8007659455, 59.156629213483, 7/13/2018
User12, 1.2763039433539, 78.985341938355, 46.380098528749, 1866.0820879785, 85.741797752809, 7/12/2018
User13, -12.190747954362, 40.292814821546, -0.10434851940227, -1684.3961859993, -30.296179775281, 7/9/2018
User14, -28.25064775909, 39.279813443065, 25.471390836079, -2123.8553395308, -18.702921348315, 7/13/2018
User15, -2.827259580743, 55.010730092194, 19.287458523068, -1406.9831291156, -0.43696629213491, 7/10/2018
User16, 38.375553401501, 45.453224372335, -30.299834073068, 1705.4531510303, 276.07516853933, 7/11/2018
User17, -44.801920924378, 29.437382210702, -20.391524146904, -2884.5435805497, -271.42382022472, 7/13/2018
User18, 15.656232109745, 38.605958218247, -41.332053270361, -1355.0607008364, 205.70741573034, 7/4/2018
User19, -44.254022779533, 19.151829421512, -26.835155085751, -2587.9411560619, -255.7193258427, 7/12/2018
User20, 2.9727641147826, 29.352899914689, -16.633731345117, -1948.6545928488, NULL, 7/12/2018
User21, 1.1718433594694, 19.683564157951, -25.528676878314, -1899.547504175, -39.080449438202, 7/13/2018
User22, 4.0600834491879, 27.138221463165, -13.363551832987, -1972.3885366358, -7.8215730337072, 7/12/2018
User23, 3.1214979522206, 56.633836476145, -0.68555760015715, -1873.6385494506, -20.035280898876, 7/13/2018
User24, -5.0592893026469, 21.016429549475, -17.33575393937, -1874.2297341504, NULL, 7/12/2018
User25, 0.80284564931117, 40.410740242703, -14.901225176099, -1753.5601686416, -22.829325842696, 7/13/2018
User26, -15.705912374503, 39.709891663578, -16.397693300268, -1926.2009706647, -72.818539325843, 7/13/2018
User27, 10.351871940793, 55.678722919971, -2.0356607222616, 1904.4840650739, 57.554044943819, 7/13/2018
User28, -0.44915732665004, 25.662058472862, -28.277310224057, -1904.2582194672, -36.877303370787, 7/13/2018
User29, -3.205388610008, 39.411367056573, -12.692193718864, -1921.599865055, -47.022359550562, 7/13/2018
User30, NULL, NULL, 446.25092254639, NULL, NULL, 7/13/2018
User31, NULL, NULL, -215.745, NULL, NULL, 7/13/2018

不插入此用户

User20, 2.9727641147826, 29.352899914689, -16.633731345117, -1948.6545928488, NULL, 7/12/2018
User24, -5.0592893026469, 21.016429549475, -17.33575393937, -1874.2297341504, NULL, 7/12/2018
User31, NULL, NULL, -215.745, NULL, NULL, 7/13/2018
lhcgjxsq

lhcgjxsq1#

解决了的
过了一段时间,读了很多评论,在stackoverflow和google上搜索主题,我就解决了这个问题。
我更改了列类型,仅将数字的默认值设置为null,并添加了新的唯一索引。
现在所有的工作,包括进口日期,甚至之前是不可能的。
下面是我的数据库现在的样子

谢谢所有帮助过这件事的人。

相关问题