【问题标题】:Add data into database using JSON(need to avoid duplicate of data php)使用JSON将数据添加到数据库中(需要避免重复数据php)
【发布时间】:2018-03-21 15:53:37
【问题描述】:

这是我的 JSON 数据,我需要将这些数据插入到我的数据库中。然后每5分钟,我会用setinterval检查这个JSON文件中是否有新添加的电台,如果有变化,应该只添加新的。但是我遇到了问题,所有字段都被再次添加。我不需要重复数据,我只想添加新的更新电台。

[

{
    "stationname": "Radio Plus, Mauritius",
    "websiteurl": "www.radioplus.mu",
    "streamurl": "http://s4.voscast.com:8432/;",
    "genre":"POP (Popular Music)",
    "language":"French",
    "country": "Mauritius",
    "description":"Best Music"
},


{
    "stationname": "Radio One",
    "websiteurl": "r1.mu",
    "streamurl": "http://151.80.44.127:8020/stream?type=http&nocache",
    "genre":"POP (Popular Music)",
    "language":"French",
    "country": "Mauritius",
    "description":"24/7 Music Everyday"
},

{
    "stationname": "2RRR FM",
    "websiteurl": "www.2rrr.org",
    "streamurl": "http://110.142.218.7:88/broadwave.m3u?type=.mp3/;s",
    "genre":"Electronic",
    "language":"English",
    "country": "Austria",
    "description":"Good Music all day and night"
},

{
    "stationname": "22G Radio",
    "websiteurl": "www.22gradio.com",
    "streamurl": "http://s6.voscast.com:9224/;stream.mp3",
    "genre":"Electronic",
    "language":"English",
    "country": "India",
    "description":"Good Music in India"
},

{
    "stationname": "80’s / 90’s Radio",
    "websiteurl": "hitconnection.fr",
    "streamurl": "http://streaming.radionomy.com/80-s-90-s-HitConnectionRadio",
    "genre":"Electronic",
    "language":"French",
    "country": "French",
    "description":"This online radio station provides twenty four hour (24h) mix of 80’s / 90’s of music. The station plays all the 80’s / 90’s music you love while striving to keep up with the desires of its listeners."
},

{
    "stationname": "FM Reggae Trade Radio",
    "websiteurl": "www.1.fm",
    "streamurl": "http://sc1c-sjc.1.fm:7078/;",
    "genre":"Raggae",
    "language":"Other",
    "country": "Brazil",
    "description":"Entertainment, Fun and excitement are the specialty of 1.FM Reggae Trade Radio the radio is always live and lively with its energetic presentation, promotion of music and programs. Its a complete package as a full entertainment rich radio broadcasting from Brazil With 1.FM Reggae Trade Radio you can enjoy lots of class leading musical programs."
},

{
    "stationname": "01 Internet of Music",
    "websiteurl": "www.internetofmusic.com",
    "streamurl": "http://radio.internetofmusic.com:8000/live.nsv",
    "genre":"World Music / Beats",
    "language":"English",
    "country": "United States of America",
    "description":"01 Internet of Music is all about good music as they believes “Music in the Answer”. By tuning in to 01 Internet of Music listeners from around the world can enjoy the amazingly entertaining musical programs presented by the talented broadcasting team of 01 Internet of Music."
},

{
    "stationname": "I love Kpop",
    "websiteurl": "www.ilovekpop.wifeo.com",
    "streamurl": "http://streaming.radionomy.com/IloveKpop?type=.mp3",
    "genre":"R&B / Soul",
    "language":"English",
    "country": "Korea North",
    "description":"In Korea there are lots of pop music based radio station available through out the whole country and I love Kpop is one of those popular radios broadcasting live from there. Unlike other radio stations of Korea this radio is always known best for broadcasting pop music along with musical information."
},

{
    "stationname": "2cr Radio",
    "websiteurl": "www.2cr.com.au",
    "streamurl": "http://s6.voscast.com:8016/;",
    "genre":"Pop (Popular music)",
    "language":"Japanese",
    "country": "China",
    "description":"2cr Radio is a very popular radio on China. They play Chines music, it is a 24 hrs online radio. Enjoy 2cr Radio."
},

{
    "stationname": "Akash",
    "websiteurl": "www.2cr.com.au",
    "streamurl": "http://s6.voscast.com:8016/;",
    "genre":"Pop (Popular music)",
    "language":"Japanese",
    "country": "China",
    "description":"2cr Radio is a very popular radio on China. They play Chines music, it is a 24 hrs online radio. Enjoy 2cr Radio."
}

]

这是我的添加代码:

<?php
$db = mysqli_connect("localhost","root","","registration");
$filename = "station.json";
$data = file_get_contents($filename);
$array = json_decode($data,true);

foreach($array as $roww){

$stationname = $roww['stationname'];
$sql_name = "SELECT * FROM radiotest WHERE stationname='$stationname'";
$res_name = mysqli_query($db, $sql_name);

}

foreach ($array as $row) {
if (mysqli_num_rows($res_name) > 0) {
return false;

}   else{
         $sql = "INSERT INTO radiotest(stationname,websiteurl,streamurl,genre,language,country,description) 
VALUES ('".$row["stationname"]."', '".$row["websiteurl"]."', '".$row["streamurl"]."', '".$row["genre"]."', '".$row["language"]."', '".$row["country"]."', '".$row["description"]."')";
mysqli_query($db,$sql);
}
}
?>

有人可以帮助我吗?如何避免这种情况? 谢谢!

【问题讨论】:

    标签: php json database


    【解决方案1】:

    好吧,假设streamurl 是唯一的,您可以定义一个getNewStations 方法,该方法仅返回您尚未发送到服务的站点。当然,如果页面被重新加载,并且您收到一个带有已插入服务器脚本中的站的 JSON,它们将被再次插入。

    如果这是一个问题,您可能必须定义一种方法来检查一个站是否已经在数据库中,可能通过检查数据库中是否没有其他具有相同 streamurl 的行。

    在下面的示例中,您将看到stations2 如何包含stations 中的所有电台和一个新电台。当您为该数组调用 getNewStations 时,仅返回最后一个。

    var insertedStations = {};
    
    function getNewStations(stations) {
      var newStations = stations.filter(function(item) { return insertedStations[item.streamurl] === undefined});
      // add to list of inserted stations
      newStations.forEach(function(item) { insertedStations[item.streamurl] = true});
      
      return newStations
    }
    
    var stations = [
      {
        "stationname": "Radio Plus, Mauritius",
        "websiteurl": "www.radioplus.mu",
        "streamurl": "http://s4.voscast.com:8432/;",
        "genre":"POP (Popular Music)",
        "language":"French",
        "country": "Mauritius",
        "description":"Best Music"
      },
      {
        "stationname": "Radio One",
        "websiteurl": "r1.mu",
        "streamurl": "http://151.80.44.127:8020/stream?type=http&nocache",
        "genre":"POP (Popular Music)",
        "language":"French",
        "country": "Mauritius",
        "description":"24/7 Music Everyday"
      }
    ];
    
    var stations2 = [
      {
        "stationname": "Radio Plus, Mauritius",
        "websiteurl": "www.radioplus.mu",
        "streamurl": "http://s4.voscast.com:8432/;",
        "genre":"POP (Popular Music)",
        "language":"French",
        "country": "Mauritius",
        "description":"Best Music"
      },
      {
        "stationname": "Radio One",
        "websiteurl": "r1.mu",
        "streamurl": "http://151.80.44.127:8020/stream?type=http&nocache",
        "genre":"POP (Popular Music)",
        "language":"French",
        "country": "Mauritius",
        "description":"24/7 Music Everyday"
      },
      {
        "stationname": "2cr Radio",
        "websiteurl": "www.2cr.com.au",
        "streamurl": "http://s6.voscast.com:8016/;",
        "genre":"Pop (Popular music)",
        "language":"Japanese",
        "country": "China",
        "description":"2cr Radio is a very popular radio on China. They play Chines music, it is a 24 hrs online radio. Enjoy 2cr Radio."
      }
    ];
    
    var stationsToInsert = getNewStations(stations);
    console.log(stationsToInsert);
    stationsToInsert = getNewStations(stations2);
    console.log(stationsToInsert); // should only log the third station

    【讨论】:

    • 感谢您的评论,但我还是新手..我无法理解您的概念..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-29
    • 2017-07-14
    • 2022-11-14
    相关资源
    最近更新 更多