0

Hellow

I want to write data from api in MySql Database, I have api.js file to get the data from the api and a php file to write the data to MySql table;

api.js

$(function()
{
    var $orders = $('#orders');

    $.ajax({
        type:'GET',
        url:'http://datatank.stad.gent/4/cultuursportvrijetijd/gentsefeestenlocaties.json',
        success: function(orders) {
        dataType:'json', // add json datatype to get json
        data: ({name: orders})
        $.each(orders, function(i, order) {
            $orders.append('<li>id: ' + order.id + ',  ' + order.naam);
        });     
        },
});

Following code is my php.file

<?php
define('DB_HOST', '');
define('DB_NAME', '');
define('DB_USER','');
define('DB_PASSWORD','');

$con=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die("Failed to connect to MySQL: " . mysql_error());
$db=mysql_select_db(DB_NAME,$con) or die("Failed to connect to MySQL: " . mysql_error());

$myArray = $_GET['name']; 
echo ($myArray);
print_r ($myArray);
echo ('Mattijs');

foreach ($myArray as $row)
{
    echo($row[0]);
    $query = mysql_query("INSERT INTO `straat`(`ID`, `StraatFeest`) VALUES ('" + $row[0] + "','" + $row[1] + "')") or die(mysql_error());
}
    //$query = mysql_query("INSERT INTO `straat`(`ID`, `StraatFeest`) VALUES ('dd','dd')") or die(mysql_error());
    $row = mysql_fetch_array($query) or die(mysql_error());
?>

myArray is still empty

I don't get data from the ajax File

Thanks

xpy
  • 5,481
  • 3
  • 29
  • 48
  • Where do you output the `$row` ? E.g. with `echo json_encode($row);` ? – Jan Mar 20 '16 at 09:39
  • Is that `$.ajax` code a copying error? `dataType:` and `data:` don't belong in the middle of the `success:` function. – Barmar Mar 20 '16 at 09:53
  • `INSERT` just inserts into the database, it doesn't return any results. What do you expected `mysql_fetch_array($query)` to do? – Barmar Mar 20 '16 at 09:55
  • You can only use `mysql_fetch_array()` with a `SELECT` query. – Barmar Mar 20 '16 at 09:55
  • Thanks for the reply. The insert works, test with dummy data. I want the following array $myArray inserten in a MySQL table but $myarray is still empty – Thijs Vantorre Mar 20 '16 at 10:19

2 Answers2

0

try this

$(function() {
  var $orders = $('#orders');

  $.ajax({
    type: 'GET',
    url: 'http://datatank.stad.gent/4/cultuursportvrijetijd/gentsefeestenlocaties.json',
    dataType: 'json', // add json datatype to get json
    success: function(orders) {
      $.each(orders, function(i, order) {
        $orders.append('<li>id: <span id="id">' + order.id + '</span>,  <span id="naam">' + order.naam + '</span></li>');
      });
    },
  });
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id="orders"></ul>
elreeda
  • 4,525
  • 2
  • 18
  • 45
0

You don't need the api.js, you can fetch json using php as well (also, string concatenations in php aren't done using +, so use . instead):

$myArray = json_decode(file_get_contents('http://datatank.stad.gent/4/cultuursportvrijetijd/gentsefeestenlocaties.json'));
foreach ($myArray as $feest)
{
  $query = mysql_query("INSERT INTO `straat`(`ID`, `StraatFeest`) VALUES (". $feest->id .",'". $feest->naam ."')") or die(mysql_error());
}

You should read this if you need to handle entries with a quote or a special character in it's name or id.

Community
  • 1
  • 1
Sjon
  • 4,989
  • 6
  • 28
  • 46