0

First i want to insert data that i got from google API into MySQL. I can insert that data i got it into my database. But i got a problem, if the data same it will not replace the old data. The data i want to replace is snippet but if the link is same but the snippet is different it will create new instead replace. I already tried using INSERT IGNORE INTO but it all insert new data not replace it and i using REPLACE INTO but it also same . This is my coding.

<html>  
      <head>  
           <title>Media Monitoring</title> 

     <style>

   .box
   {
    width:750px;
    padding:20px;
    background-color:#fff;
    border:1px solid #ccc;
    border-radius:5px;
    margin-top:100px;
   }
  </style>
      </head>  
      <body>  
        <div class="container box">
          <h3 align="center">Import JSON File Data into Mysql Database in PHP</h3><br />
          <?php
          $connect = mysqli_connect("localhost", "root", "", "accounts"); //Connect PHP to MySQL Database
          $query = '';
          $table_data = '';

          $filename = 'https://www.googleapis.com/customsearch/v1?q=unimap&cx=004123968310343535430%3Aaxml6iel9yo&key=AIzaSyDxPcphnrcN9_dfkRkFTbwkv44m1-HI1Hg&sort=date';
          $data = file_get_contents($filename); //Read the JSON file in PHP
          $array = json_decode($data, true); //Convert JSON String into PHP Array
          //$record =$array['items'][1]["link"];
         // var_dump($record);
          foreach($array['items'] as $row) //Extract the Array Values by using Foreach Loop
          {
           // $query .="SELECT * from go WHERE htmlSnippet ='" 
           $query .= "INSERT INTO go(htmlTitle, Link, htmlSnippet) VALUES ('".$row["htmlTitle"]."', '".$row["link"]."', '".$row["htmlSnippet"]."') ;";  // Make Multiple Insert Query 
           $table_data .= '
            <tr>
       <td>'.$row["htmlTitle"].'</td>
       <td>'.$row["link"].'</td>
       <td>'.$row["htmlSnippet"].'</td>
      </tr>
           '; //Data for display on Web page
          }
          if(mysqli_multi_query($connect, $query)) //Run Mutliple Insert Query
    {
     echo '<h3>Imported JSON Data</h3><br />';
     echo '
      <table class="table table-bordered">
        <tr>
         <th width="45%">Name</th>
         <th width="10%">Gender</th>
         <th width="45%">Designation</th>
        </tr>
     ';
     echo $table_data;  
     echo '</table>';
          }




          ?>
     <br />
         </div>  
      </body>  
 </html> 
Rushdiey
  • 113
  • 1
  • 1
  • 12
  • 2
    Google: UPDATE ON DUPLICATE KEY - and except in the specific case of a paramaterised query, never execute a query inside a loop. – Strawberry May 03 '17 at 10:22
  • I try adding this but its still the same INSERT INTO go(htmlTitle, Link, htmlSnippet) VALUES ('".$row["htmlTitle"]."', '".$row["link"]."', '".$row["htmlSnippet"]."') ON DUPLICATE KEY UPDATE htmlTitle=htmlTitle;".. About the loop because i want get 10 data from the API – Rushdiey May 03 '17 at 10:41

0 Answers0