gpt4 book ai didi

php - 将数组插入MySQL数据库

转载 作者:行者123 更新时间:2023-11-29 08:06:02 26 4
gpt4 key购买 nike

这确实不应该那么困难,但是这里的解决方案都不适合我。

这是我的代码片段。假设 $result 包含该数组没有问题。问题在于插入它。

这段代码给了我一个相当有用的错误:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '://www.asos.com/Dico-Copenhagen/DCo-Copenhagen-Suede-Fold-Over-Ankle-Boot/Prod/p' at line 1

PHP:

$result = array();

foreach ($product_urls as $i => $product_url)
{
$result[] = array(
'product_url' => $product_url,
'shop_name' => $shop_name,
'photo_url' => $photo_url[$i],
'was_price' => $was_price[$i],
'now_price' => $now_price[$i]
);
}

$sql = array();

foreach($result as $row)
{
$sql[] = "(" . mysqli_real_escape_string($con, $row['product_url']) . ", " .$row['shop_name'] . ")";
}

mysqli_query($con, 'INSERT INTO shopaholic (product_url, shop_name) VALUES '.implode(', ', $sql)) or die(mysqli_error($con));

编辑

这段代码:

echo 'INSERT INTO shopaholic (product_url, shop_name) VALUES ' . implode(', ', $sql);

输出为:

 INSERT INTO shopaholic (product_url, shop_name) VALUES
(http://www.asos.com/Dico-Copenhagen/DCo-Copenhagen-Suede-Fold-Over-Ankle-Boot/Prod/pgeproduct.aspx?iid=1364346&cid=16903&sh=0&pge=0&pgesize=36&sort=-1&clr=Grey%2fblack,
ASOS),
(http://www.asos.com/Pollini/Pollini-for-Louise-Gray-Heeled-Sandals/Prod/pgeproduct.aspx?iid=1530692&cid=16903&sh=0&pge=0&pgesize=36&sort=-1&clr=White%2fpink%2fnavy,
ASOS),
(http://www.asos.com/Pollini/Pollini-for-Michael-van-der-Ham-Velvet-Heeled-Sandals/Prod/pgeproduct.aspx?iid=1530715&cid=16903&sh=0&pge=0&pgesize=36&sort=-1&clr=Yellow,
ASOS),
(http://www.asos.com/Pollini/Pollini-Colour-Block-Heeled-Sandals/Prod/pgeproduct.aspx?iid=2059051&cid=16903&sh=0&pge=0&pgesize=36&sort=-1&clr=Green%2fgrey,
ASOS),
(http://www.asos.com/B-Store/B-Store-Salavatore-12-Exclusive-Wedge-Sandals/Prod/pgeproduct.aspx?iid=2262866&cid=16903&sh=0&pge=0&pgesize=36&sort=-1&clr=Blue,
ASOS),
(http://www.asos.com/ASOS/ASOS-Lace-Dress-With-Dropped-Waist/Prod/pgeproduct.aspx?iid=2435629&cid=16903&sh=0&pge=0&pgesize=36&sort=-1&clr=Black,
ASOS),
(http://www.asos.com/ASOS/ASOS-Shift-Dress-With-Pleated-Dropped-Waist/Prod/pgeproduct.aspx?iid=2547064&cid=16903&sh=0&pge=0&pgesize=36&sort=-1&clr=Green,
ASOS),
(http://www.asos.com/ASOS/ASOS-Skater-Dress-With-Lace-Angel-Sleeves/Prod/pgeproduct.aspx?iid=2593859&cid=16903&sh=0&pge=0&pgesize=36&sort=-1&clr=Blue,
ASOS),
(http://www.asos.com/ASOS/ASOS-Cropped-Utility-Jacket/Prod/pgeproduct.aspx?iid=2639913&cid=16903&sh=0&pge=0&pgesize=36&sort=-1&clr=Khaki,
ASOS),
(http://www.asos.com/ASOS/ASOS-Bodycon-Dress-In-Stripe-Print/Prod/pgeproduct.aspx?iid=2677844&cid=16903&sh=0&pge=0&pgesize=36&sort=-1&clr=Print,
ASOS),
(http://www.asos.com/ASOS/ASOS-Ridley-High-Waist-Ultra-Skinny-Jeans-In-Pink-Marble-Wash/Prod/pgeproduct.aspx?iid=2688103&cid=16903&sh=0&pge=0&pgesize=36&sort=-1&clr=Pink,
ASOS),
(http://www.asos.com/ASOS/ASOS-Fringe-Denim-Gilet/Prod/pgeproduct.aspx?iid=2690930&cid=16903&sh=0&pge=0&pgesize=36&sort=-1&clr=Indigo,
ASOS),
(http://www.asos.com/ASOS/ASOS-Western-Jacket-with-Bird-Embroidery/Prod/pgeproduct.aspx?iid=2695926&cid=16903&sh=0&pge=0&pgesize=36&sort=-1&clr=Light+wash+stone, ASOS),
(http://www.asos.com/MiH-Jeans/Mih-Jeans-Breathless-Painted-Spot-Skinny-Jeans/Prod/pgeproduct.aspx?iid=2701072&cid=16903&sh=0&pge=0&pgesize=36&sort=-1&clr=Mid+wash,
ASOS),
(http://www.asos.com/Lazy-Oaf/Lazy-Oaf-Exclusive-Letter-Body-Con-Dress/Prod/pgeproduct.aspx?iid=2706215&cid=16903&sh=0&pge=0&pgesize=36&sort=-1&clr=White%2fblack,
ASOS),
(http://www.asos.com/Lazy-Oaf/Lazy-Oaf-Exclusive-Chain-Print-Body-Con-Dress/Prod/pgeproduct.aspx?iid=2706256&cid=16903&sh=0&pge=0&pgesize=36&sort=-1&clr=Black,
ASOS),
(http://www.asos.com/ASOS/ASOS-Brady-Low-Rise-Slim-Boyfriend-Jeans-in-Bleach-Wash/Prod/pgeproduct.aspx?iid=2707895&cid=16903&sh=0&pge=0&pgesize=36&sort=-1&clr=Bleach,
ASOS),
(http://www.asos.com/ASOS/ASOS-Elgin-Supersoft-Skinny-Jeans-in-Washed-Peony/Prod/pgeproduct.aspx?iid=2715194&cid=16903&sh=0&pge=0&pgesize=36&sort=-1&clr=Peony,
ASOS),
(http://www.asos.com/ASOS-Petite/ASOS-PETITE-Exclusive-Maxi-Dress-In-Textured-Stripe/Prod/pgeproduct.aspx?iid=2727450&cid=16903&sh=0&pge=0&pgesize=36&sort=-1&clr=Nude+pink,
ASOS),
(http://www.asos.com/Citizens-of-Humanity/Citizens-Of-Humanity-Rose-Print-Avedon-Skinny-Jeans/Prod/pgeproduct.aspx?iid=2740983&cid=16903&sh=0&pge=0&pgesize=36&sort=-1&clr=Light+blue,
ASOS),
(http://www.asos.com/ASOS/ASOS-A-Line-Skirt-in-Leather-Look/Prod/pgeproduct.aspx?iid=2744422&cid=16903&sh=0&pge=0&pgesize=36&sort=-1&clr=Black,
ASOS),
(http://www.asos.com/Current/Elliott/Current/Elliott-Tribal-Fringed-Skinny-Jeans/Prod/pgeproduct.aspx?iid=2753356&cid=16903&sh=0&pge=0&pgesize=36&sort=-1&clr=Tribal,
ASOS),
(http://www.asos.com/ASOS/ASOS-Elgin-Skinny-Jeans-in-Acid-Wash-Stripe-with-Rips/Prod/pgeproduct.aspx?iid=2779459&cid=16903&sh=0&pge=0&pgesize=36&sort=-1&clr=Multi,
ASOS),
(http://www.asos.com/ASOS/ASOS-Stud-Skinny-Waist-Belt/Prod/pgeproduct.aspx?iid=2819373&cid=16903&sh=0&pge=0&pgesize=36&sort=-1&clr=Black,
ASOS),
(http://www.asos.com/People-Tree/People-Tree-by-Zandra-Rhodes-Jungle-Trail-Statement-Dress/Prod/pgeproduct.aspx?iid=2824591&cid=16903&sh=0&pge=0&pgesize=36&sort=-1&clr=Black,
ASOS),
(http://www.asos.com/ASOS/ASOS-Swing-Dress-with-Dropped-Waist/Prod/pgeproduct.aspx?iid=2855063&cid=16903&sh=0&pge=0&pgesize=36&sort=-1&clr=Green,
ASOS),
(http://www.asos.com/ASOS/ASOS-Brady-Slim-Boyfriend-Jeans-in-Dark-Washed-Indigo/Prod/pgeproduct.aspx?iid=2868426&cid=16903&sh=0&pge=0&pgesize=36&sort=-1&clr=Dark+vintage+blue,
ASOS),
(http://www.asos.com/ASOS/ASOS-Premium-Link-Necklace/Prod/pgeproduct.aspx?iid=2876009&cid=16903&sh=0&pge=0&pgesize=36&sort=-1&clr=Gold,
ASOS),
(http://www.asos.com/Federation/Federation-Slant-Skirt/Prod/pgeproduct.aspx?iid=2878511&cid=16903&sh=0&pge=0&pgesize=36&sort=-1&clr=Wash+out,
ASOS),
(http://www.asos.com/ASOS/ASOS-Pencil-Dress-With-Lace-Panels-And-Plunge-Neck/Prod/pgeproduct.aspx?iid=2888606&cid=16903&sh=0&pge=0&pgesize=36&sort=-1&clr=Jade,
ASOS),
(http://www.asos.com/ASOS-Curve/ASOS-CURVE-Mixed-Plate-Waist-Belt/Prod/pgeproduct.aspx?iid=2888978&cid=16903&sh=0&pge=0&pgesize=36&sort=-1&clr=Blue,
ASOS),
(http://www.asos.com/ASOS-Curve/ASOS-CURVE-Wide-Waist-Belt/Prod/pgeproduct.aspx?iid=2889088&cid=16903&sh=0&pge=0&pgesize=36&sort=-1&clr=Black,
ASOS),
(http://www.asos.com/ASOS/ASOS-Arrow-Multi-Plate-Waist-Belt/Prod/pgeproduct.aspx?iid=2892493&cid=16903&sh=0&pge=0&pgesize=36&sort=-1&clr=Black,
ASOS),
(http://www.asos.com/ASOS/ASOS-Stud-And-Gem-Plate-Waist-Belt/Prod/pgeproduct.aspx?iid=2892645&cid=16903&sh=0&pge=0&pgesize=36&sort=-1&clr=Black,
ASOS),
(http://www.asos.com/ASOS-Petite/ASOS-PETITE-Fringe-Denim-Gilet/Prod/pgeproduct.aspx?iid=2898386&cid=16903&sh=0&pge=0&pgesize=36&sort=-1&clr=Blue,
ASOS),
(http://www.asos.com/ASOS/ASOS-Shift-Dress-with-Zip-Front/Prod/pgeproduct.aspx?iid=2901965&cid=16903&sh=0&pge=0&pgesize=36&sort=-1&clr=Green,
ASOS)

最佳答案

改变

$sql[] = "(" . mysqli_real_escape_string($con, $row['product_url']) . ", " .$row['shop_name'] . ")";

$sql[] = "('" . mysqli_real_escape_string($con, $row['product_url']) . "', '".$row['shop_name'] . "')";

关于php - 将数组插入MySQL数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22731554/

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