简体   繁体   English

多数组插入数据库

[英]Multiple array insert into database

I'm using the following arrays: 我正在使用以下数组:

$name = array("Veldspar","Scordite","Pyroxeres","Plagioclase","Omber","Kernite","Jaspet","Hemorphite","Hedbergite","Gneiss","Dark Ochre","Crokite","Spodumain","Bistot","Arkanor","Mercoxit");
$typeids= array(1230,1228,1224,18,1227,20,1226,1231,21,1229,1232,1225,19,1223,22,11396);
$url="http://api.eve-central.com/api/marketstat?usesystem=30000142&typeid=".join('&typeid=',$typeids);
$pricexml=file_get_contents($url);
$xml=new SimpleXMLElement($pricexml);

foreach($typeids as $typeid)
{
    $item=$xml->xpath('/evec_api/marketstat/type[@id='.$typeid.']');
    $price= (float) $item[0]->buy->max;
    $price=round($price,2);

   $query1 = "INSERT INTO data (Price) VALUES ('$price');";

   $q1 = mysqli_query($conn,$query1) or die ('Error posting data');
    echo $typeids[$index].$name[$index].$price[$index]."\n";
}
foreach($typeids as $index => $value)
{
   $query = "INSERT INTO data (typeID, Name) VALUES ('$typeids[$index]','$name[$index]');";

   $q = mysqli_query($conn,$query) or die ('Error posting data');
    echo $typeids[$index].$name[$index]."\n";

}

Its for a game I play, Eve online. 对于我玩的游戏,《夏娃在线》。 What I'm trying to accomplish is get the price/itemID/Name of the item and inject that into my database. 我要完成的工作是获取商品的价格/商品ID /名称,并将其注入我的数据库中。 I can get all 3 items between my 2 foreach loops. 我可以在两个foreach循环之间获得所有3个项目。 However when I go to put it in the database its creating 2 sets of rows. 但是,当我将其放入数据库时​​,它会创建2组行。

+--------+-------------+-------+
| typeID | Name        | Price |
+--------+-------------+-------+
|      0 |             |    15 |
|      0 |             |    27 |
|      0 |             |    55 |
|      0 |             |    58 |
|      0 |             |    91 |
|      0 |             |   227 |
|      0 |             |   434 |
|      0 |             |   740 |
|      0 |             |   708 |
|      0 |             |   914 |
|      0 |             |  1505 |
|      0 |             |  3202 |
|      0 |             |  1600 |
|      0 |             |  2900 |
|      0 |             |  3180 |
|      0 |             | 11800 |
|   1230 | Veldspar    |     0 |
|   1228 | Scordite    |     0 |
|   1224 | Pyroxeres   |     0 |
|     18 | Plagioclase |     0 |
|   1227 | Omber       |     0 |
|     20 | Kernite     |     0 |
|   1226 | Jaspet      |     0 |
|   1231 | Hemorphite  |     0 |
|     21 | Hedbergite  |     0 |
|   1229 | Gneiss      |     0 |
|   1232 | Dark Ochre  |     0 |
|   1225 | Crokite     |     0 |
|     19 | Spodumain   |     0 |
|   1223 | Bistot      |     0 |
|     22 | Arkanor     |     0 |
|  11396 | Mercoxit    |     0

I've tried combing all 3 things into 1 foreach loop but it wasn't honoring the API call to get the price, which is why I resulted to 2 different foreach loops. 我尝试将所有3件事组合到1个foreach循环中,但是这并不兑现API调用来获取价格,这就是为什么我导致2个不同的foreach循环。

My current solution in mind is to make it so the rows are combined, is there some sort of SQL command I can run for the price foreach loop that it will just add it to the top row and go down? 我当前的解决方案是使行合并,是否可以为price foreach循环运行某种SQL命令,它将仅将其添加到顶部行并向下移动?

I think this should do it in one loop: 我认为这应该在一个循环中完成:

foreach($typeids as $index => $typeid)
{
    $item=$xml->xpath('/evec_api/marketstat/type[@id='.$typeid.']');
    $price= (float) $item[0]->buy->max;
    $price=round($price,2);

    $query1 = "INSERT INTO data (typeID, Name, Price) VALUES ('$typeid', '$name[$index]', '$price');";

    $q1 = mysqli_query($conn,$query1) or die ('Error posting data');
    echo $typeids[$index].$name[$index].$price[$index]."\n";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM