Php mysql insert/update from json

8
October 11, 2018, at 3:50 PM

hello i have a problem insert data to mysql working but i need if not exist data create if exist update all row. Now after each refresh page add same data but not updating.

$conn = mysql_connect("127.0.0.1", "root","") or die(mysql_error());
mysql_select_db("youtuberiai", $conn);
echo "Connected successfully <br /><br />";
$url = 'https://www.googleapis.com/youtube/v3/channels?id=ID&part=snippet%2Cstatistics&key=KEY';
$content = file_get_contents($url);
$json = json_decode($content, true);
foreach($json as $row);
foreach($json['items'] as $row)
{
    $idd = $row['id'];
    $title = $row['snippet']['title'];
    $description = $row['snippet']['description'];
    $viewCount = $row['statistics']['viewCount'];
    $subscriberCount = $row['statistics']['subscriberCount'];
    $sql = "INSERT INTO vartotojai(idd, title, description, viewCount, subscriberCount) VALUES('$idd', '$title','$description','$viewCount','$subscriberCount') 
    ON DUPLICATE KEY UPDATE idd='$idd', title='$title', description='$description', viewCount='$viewCount', subscriberCount='$subscriberCount';
    ";
    mysql_query("SET NAMES utf8");
    if(!mysql_query($sql,$conn))
    {
        die('Error : ' . mysql_error());
    }
}
Answer 1

make the id as unique. Then only duplicate query gets the role. If the value in that filed already exist, then then that field will be updated.

READ ALSO
$_POST is empty in push notification response (PHP)

$_POST is empty in push notification response (PHP)

Can anybody tell me how to get the response data from a push notification responseI am currently only able to get data from headers, but $_POST is an empty array

25
Email Open Rate Tracker - Differences in email client

Email Open Rate Tracker - Differences in email client

I've implemented a very common approach to email open rate tracking using a 1x1px transparent image:

33
Redirect route to language prefix in Symfony 4

Redirect route to language prefix in Symfony 4

Since version 41, Symfony now handles multilingual routing without the need of an external plugin (https://symfony

22
How to persist an entity related to an already existing one?

How to persist an entity related to an already existing one?

I have seen a lot of examples about cascading with doctrine (including the doc) but every single example is about creating eg a new user, a new comment and saving both

41