gpt4 book ai didi

php - 检查 mysql 行是否存在时,foreach 中的嵌套 if/then 语句失败

转载 作者:行者123 更新时间:2023-11-29 11:43:34 25 4
gpt4 key购买 nike

以下代码希望获取 json feed 并将其作为新记录插入到 mysql 数据库中,或者如果该记录已存在,则仅更新现有记录中的几个选择字段。我无法让代码覆盖完整的现有记录,因为现有记录中的字段可能已被用户修改,并且我不能在更新时丢失修改。

该代码几乎对每条记录都执行 IF 和 ELSE。我认为 foreach 循环的逻辑或语法有问题。 99.99% 的记录已经存在。

非常感谢任何帮助。

$response = json_decode($response, true);

// Set specified data from response
$data = $response['data'];

// Loop through the data array

mysql_connect('localhost',$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

foreach ($data as $item)
{

$result = mysql_query("SELECT 1 FROM prosp_rawdata WHERE productId = '".$item['productId']."' LIMIT 1");

if (mysql_fetch_row($result)) {
mysql_query("UPDATE SET `affiliate_url` = '".$item['affiliate_url']."', `image_url` = '".$item['image_url']."', `price` = '".$item['price']."', `price_sale` = '".$item['price_sale']."', `percentOff` = '".$item['percentOff']."', `merchant` = '".$item['merchant']."', `dateupdate` = NOW() WHERE `prosp_rawdata`.`productId` = '".$item['productId']."'");

echo "\n\n record exists: ";
echo $item['productId'];

}

else{
mysql_query("INSERT INTO prosp_rawdata (catalogId,productId,date,affiliate_url,image_url,keyword,keywords,description,category,price,price_sale,percentOff,merchant,brand,upc) VALUES ('".$item['catalogId']."', '".$item['productId']."', NOW(), '".$item['affiliate_url']."', '".$item['image_url']."', '".$item['keyword']."', '".$item['keywords']."', '".$item['description']."', '".$item['category']."', '".$item['price']."', '".$item['price_sale']."', '".$item['percentOff']."', '".$item['merchant']."', '".$item['brand']."', '".$item['upc']."')");
}

echo "\n\n NEW RECORD: ";
echo $item['productId'];

}
}
mysql_close();

输出示例:

 NEW RECORD: a78d543c5c758ded5aea731f3ec83e79

record exists: dae594b2082ef6002b658bbb638cc885

NEW RECORD: dae594b2082ef6002b658bbb638cc885

record exists: 50372f83b7a06ee2c27a2a773719d58d

NEW RECORD: 50372f83b7a06ee2c27a2a773719d58d

record exists: 568967b1ec527322419613d27d8e47c2

NEW RECORD: 568967b1ec527322419613d27d8e47c2

record exists: da2fb0cc5a0be2457c4cbb9e9d08c502

NEW RECORD: da2fb0cc5a0be2457c4cbb9e9d08c502

最佳答案

缺乏表格导致阅读起来很困难。但看来你的“echo ”在“else”括号之外。他们将一直以这种方式被调用。

尝试更改为:

else{
mysql_query("INSERT INTO prosp_rawdata (catalogId,productId,date,affiliate_url,image_url,keyword,keywords,description,category,price,price_sale,percentOff,merchant,brand,upc) VALUES ('".$item['catalogId']."', '".$item['productId']."', NOW(), '".$item['affiliate_url']."', '".$item['image_url']."', '".$item['keyword']."', '".$item['keywords']."', '".$item['description']."', '".$item['category']."', '".$item['price']."', '".$item['price_sale']."', '".$item['percentOff']."', '".$item['merchant']."', '".$item['brand']."', '".$item['upc']."')");
echo "\n\n NEW RECORD: ";
echo $item['productId'];
}

关于php - 检查 mysql 行是否存在时,foreach 中的嵌套 if/then 语句失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35343487/

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