简体   繁体   English

PHP foreach 循环并将值添加到 mysql 表

[英]PHP foreach loop and add values to mysql table

I have an array ($array) which is structured like that:我有一个数组($array),它的结构如下:

[auctions] 
       (         
         [0] 
              ( 

                [item]
                        (
                           [id] => 45422
                        )

                [quantity] => 22
                [buyout] => 40
                [bid] => 25
              )

           [1] 
              ( 
                [item]
                         (
                           [id] => 24555
                         )

                [quantity] => 85
                [buyout] =>120
                [bid] => 58
              )

           [2]
         ....

I want to store some of these values in a mysql table.我想将其中一些值存储在 mysql 表中。 Additionally I want a timestamp and if the item->id already exists in my table (as the primary key) the other values should just get updated.另外我想要一个时间戳,如果item->id已经存在于我的表中(作为主键),其他值应该被更新。

[item] -> [id]
[quantity]
[buyout] 
TIMESTAMP

I tried it like that:我是这样试的:

 foreach ($array['auctions'] as $auctiondata) {

 $sql ="INSERT INTO `blackrock` (`item_id`, `actual_prices`, `actual_quantity`, `last_update`) VALUES ('". $auctiondata['item']['id']."', '". $auctiondata['buyout']."', '". $auctiondata['quantity']."', 'CURRENT_TIMESTAMP')
   ON DUPLICATE KEY UPDATE item_id='". $auctiondata['item']['id']."', '". $auctiondata['buyout']."', '". $auctiondata['quantity']."'";

 }

I suggest you to google "php mysql insert" first There are some references to you建议你先google "php mysql insert"有一些参考你

It is no meaning if you update the id when the key duplicated I think you can use REPLACE INTO instead of ON DUPLICATE KEY UPDATE if there are no any operation.如果您在密钥重复时更新 id 没有任何意义我认为如果没有任何操作,您可以使用 REPLACE INTO 而不是 ON DUPLICATE KEY UPDATE。

Please note that the old row will be deleted before it make a new row with the same id and the updated data请注意,在创建具有相同 id 和更新数据的新行之前,旧行将被删除

In addition, curly braces serve good substitution for concatenation PHP is forced to re-concatenate with every '.'此外,花括号可以很好地替代连接 PHP 被迫与每个 '.' 重新连接。 operator, slow.接线员,慢。

w3schools.com - PHP MySQL Insert Data w3schools.com - PHP MySQL 插入数据

PHP Manual - The mysqli class PHP 手册 - mysqli 类

MySQL 8.0 Reference Manual - REPLACE Statement MySQL 8.0 参考手册 - REPLACE 语句

<?php

$conn = new mysqli("localhost", "ur_db_username", "ur_db_password", "ur_schema");

if ($conn->connect_error) {
    //
}

foreach ($array['auctions'] as $auctiondata) { {    

    //`actual_prices` = $auctiondata['buyout'] according to your code provided
    //it is no meaning if you update the id when the key duplicated
    //i think you can use REPLACE instead of ON DUPLICATE KEY UPDATE

    $sql = "REPLACE INTO `blackrock` 
                    (`item_id`, `actual_prices`, `actual_quantity`, `last_update`) 
                VALUES 
                    ('{$auctiondata['item']['id']}', '{$auctiondata['buyout']}', '{$auctiondata['quantity']}', NOW())";

    if ($conn->query($sql) === TRUE) {
        //
    } else {
        //
    }
}

$conn->close();
?>

The above if the code mainly copy from the site I provided You need to implement the error handling上面如果代码主要是从我提供的站点复制过来的 需要实现错误处理

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

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